-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice_impl.go
90 lines (75 loc) · 2.45 KB
/
service_impl.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright (c) New Cloud Technologies, Ltd. 2013-2022.
* Author: Vitaly Isaev <[email protected]>
* License: https://github.com/newcloudtechnologies/memlimiter/blob/master/LICENSE
*/
package memlimiter
import (
"github.com/go-logr/logr"
"github.com/newcloudtechnologies/memlimiter/backpressure"
"github.com/newcloudtechnologies/memlimiter/controller"
"github.com/newcloudtechnologies/memlimiter/controller/nextgc"
"github.com/newcloudtechnologies/memlimiter/middleware"
"github.com/newcloudtechnologies/memlimiter/stats"
"github.com/newcloudtechnologies/memlimiter/utils/config/prepare"
"github.com/pkg/errors"
)
var _ Service = (*serviceImpl)(nil)
type serviceImpl struct {
middleware middleware.Middleware
backpressureOperator backpressure.Operator
statsSubscription stats.ServiceStatsSubscription
controller controller.Controller
logger logr.Logger
}
func (s *serviceImpl) Middleware() middleware.Middleware { return s.middleware }
func (s *serviceImpl) GetStats() (*stats.MemLimiterStats, error) {
controllerStats, err := s.controller.GetStats()
if err != nil {
return nil, errors.Wrap(err, "controller tracker")
}
backpressureStats, err := s.backpressureOperator.GetStats()
if err != nil {
return nil, errors.Wrap(err, "backpressure tracker")
}
return &stats.MemLimiterStats{
Controller: controllerStats,
Backpressure: backpressureStats,
}, nil
}
func (s *serviceImpl) Quit() {
s.logger.Info("terminating MemLimiter service")
s.controller.Quit()
s.statsSubscription.Quit()
}
// newServiceImpl - main entrypoint for MemLimiter.
func newServiceImpl(
logger logr.Logger,
cfg *Config,
statsSubscription stats.ServiceStatsSubscription,
backpressureOperator backpressure.Operator,
) (Service, error) {
if err := prepare.Prepare(cfg); err != nil {
return nil, errors.Wrap(err, "prepare config")
}
if statsSubscription == nil {
return nil, errors.New("nil tracker subscription passed")
}
logger.Info("starting MemLimiter service")
c, err := nextgc.NewControllerFromConfig(
logger,
cfg.ControllerNextGC,
statsSubscription,
backpressureOperator,
)
if err != nil {
return nil, errors.Wrap(err, "new controller from config")
}
return &serviceImpl{
middleware: middleware.NewMiddleware(logger, backpressureOperator),
backpressureOperator: backpressureOperator,
statsSubscription: statsSubscription,
controller: c,
logger: logger,
}, nil
}