-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconstructor.go
51 lines (42 loc) · 1.24 KB
/
constructor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <vitaly.isaev@myoffice.team>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/
package memlimiter
import (
"time"
"github.com/go-logr/logr"
"github.com/newcloudtechnologies/memlimiter/backpressure"
"github.com/newcloudtechnologies/memlimiter/stats"
)
// NewServiceFromConfig - main entrypoint for MemLimiter.
func NewServiceFromConfig(
logger logr.Logger,
cfg *Config,
options ...Option,
) (Service, error) {
var (
serviceStatsSubscription stats.ServiceStatsSubscription
backpressureOperator backpressure.Operator
)
for _, op := range options {
switch t := (op).(type) {
case *serviceStatsSubscriptionOption:
serviceStatsSubscription = t.val
case *backpressureOperatorOption:
backpressureOperator = t.val
}
}
// make defaults
if serviceStatsSubscription == nil {
serviceStatsSubscription = stats.NewSubscriptionDefault(logger, time.Second)
}
if backpressureOperator == nil {
backpressureOperator = backpressure.NewOperator(logger)
}
if cfg == nil {
return newServiceStub(serviceStatsSubscription), nil
}
return newServiceImpl(logger, cfg, serviceStatsSubscription, backpressureOperator)
}