-
Notifications
You must be signed in to change notification settings - Fork 4
/
service_stub.go
83 lines (66 loc) · 1.74 KB
/
service_stub.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
/*
* 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 (
"sync/atomic"
"github.com/newcloudtechnologies/memlimiter/middleware"
"github.com/newcloudtechnologies/memlimiter/stats"
"github.com/newcloudtechnologies/memlimiter/utils/breaker"
)
var _ Service = (*serviceStub)(nil)
// serviceStub doesn't perform active memory management, it just caches the latest statistics.
type serviceStub struct {
latestStats atomic.Value
statsSubscription stats.ServiceStatsSubscription
breaker *breaker.Breaker
}
func (s *serviceStub) loop() {
defer s.breaker.Dec()
for {
select {
case record := <-s.statsSubscription.Updates():
s.latestStats.Store(record)
case <-s.breaker.Done():
return
}
}
}
func (s *serviceStub) Middleware() middleware.Middleware {
// TODO: return stub
return nil
}
func (s *serviceStub) GetStats() (*stats.MemLimiterStats, error) {
if val := s.latestStats.Load(); val != nil {
//nolint:forcetypeassert
ss := val.(stats.ServiceStats)
out := &stats.MemLimiterStats{
Controller: &stats.ControllerStats{
MemoryBudget: &stats.MemoryBudgetStats{
RSSActual: ss.RSS(),
},
},
}
return out, nil
}
return nil, nil
}
func (s *serviceStub) Quit() {
s.breaker.Shutdown()
s.statsSubscription.Quit()
}
func newServiceStub(statsSubscription stats.ServiceStatsSubscription) Service {
if statsSubscription == nil {
return &serviceStub{
breaker: breaker.NewBreaker(),
}
}
out := &serviceStub{
statsSubscription: statsSubscription,
breaker: breaker.NewBreakerWithInitValue(1),
}
go out.loop()
return out
}