/
endpointregistry.go
128 lines (99 loc) · 2.45 KB
/
endpointregistry.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package routing
import (
"sync"
"sync/atomic"
"time"
"github.com/zalando/skipper/eskip"
)
const defaultLastSeenTimeout = 1 * time.Minute
// Metrics describe the data about endpoint that could be
// used to perform better load balancing, fadeIn, etc.
type Metrics interface {
DetectedTime() time.Time
SetDetected(detected time.Time)
LastSeen() time.Time
SetLastSeen(lastSeen time.Time)
InflightRequests() int64
IncInflightRequest()
DecInflightRequest()
}
type entry struct {
detected atomic.Value // time.Time
lastSeen atomic.Value // time.Time
inflightRequests atomic.Int64
}
var _ Metrics = &entry{}
func (e *entry) DetectedTime() time.Time {
return e.detected.Load().(time.Time)
}
func (e *entry) LastSeen() time.Time {
return e.lastSeen.Load().(time.Time)
}
func (e *entry) InflightRequests() int64 {
return e.inflightRequests.Load()
}
func (e *entry) IncInflightRequest() {
e.inflightRequests.Add(1)
}
func (e *entry) DecInflightRequest() {
e.inflightRequests.Add(-1)
}
func (e *entry) SetDetected(detected time.Time) {
e.detected.Store(detected)
}
func (e *entry) SetLastSeen(ts time.Time) {
e.lastSeen.Store(ts)
}
func newEntry() *entry {
result := &entry{}
result.SetDetected(time.Time{})
result.SetLastSeen(time.Time{})
return result
}
type EndpointRegistry struct {
lastSeenTimeout time.Duration
now func() time.Time
// map[string]*entry
data sync.Map
}
var _ PostProcessor = &EndpointRegistry{}
type RegistryOptions struct {
LastSeenTimeout time.Duration
}
func (r *EndpointRegistry) Do(routes []*Route) []*Route {
now := r.now()
for _, route := range routes {
if route.BackendType == eskip.LBBackend {
for _, epi := range route.LBEndpoints {
metrics := r.GetMetrics(epi.Host)
if metrics.DetectedTime().IsZero() {
metrics.SetDetected(now)
}
metrics.SetLastSeen(now)
}
}
}
removeOlder := now.Add(-r.lastSeenTimeout)
r.data.Range(func(key, value any) bool {
e := value.(*entry)
if e.LastSeen().Before(removeOlder) {
r.data.Delete(key)
}
return true
})
return routes
}
func NewEndpointRegistry(o RegistryOptions) *EndpointRegistry {
if o.LastSeenTimeout == 0 {
o.LastSeenTimeout = defaultLastSeenTimeout
}
return &EndpointRegistry{
data: sync.Map{},
lastSeenTimeout: o.LastSeenTimeout,
now: time.Now,
}
}
func (r *EndpointRegistry) GetMetrics(key string) Metrics {
e, _ := r.data.LoadOrStore(key, newEntry())
return e.(*entry)
}