-
Notifications
You must be signed in to change notification settings - Fork 343
/
fadein.go
228 lines (190 loc) · 5.06 KB
/
fadein.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package fadein
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
snet "github.com/zalando/skipper/net"
"github.com/zalando/skipper/routing"
)
const (
// Deprecated, use filters.FadeInName instead
FadeInName = filters.FadeInName
// Deprecated, use filters.EndpointCreatedName instead
EndpointCreatedName = filters.EndpointCreatedName
)
type (
fadeIn struct {
duration time.Duration
exponent float64
}
endpointCreated struct {
when time.Time
which string
}
detectedFadeIn struct {
when time.Time
duration time.Duration
lastActive time.Time
}
postProcessor struct {
endpointRegisty *routing.EndpointRegistry
// "http://10.2.1.53:1234": {t0 60s t0-10s}
detected map[string]detectedFadeIn
}
)
// NewFadeIn creates a filter spec for the fade-in filter.
func NewFadeIn() filters.Spec {
return fadeIn{}
}
func (fadeIn) Name() string { return filters.FadeInName }
func (fadeIn) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) == 0 || len(args) > 2 {
return nil, filters.ErrInvalidFilterParameters
}
var f fadeIn
switch v := args[0].(type) {
case int:
f.duration = time.Duration(v * int(time.Millisecond))
case float64:
f.duration = time.Duration(v * float64(time.Millisecond))
case string:
d, err := time.ParseDuration(v)
if err != nil {
return nil, err
}
f.duration = d
case time.Duration:
f.duration = v
default:
return nil, filters.ErrInvalidFilterParameters
}
f.exponent = 1
if len(args) == 2 {
switch v := args[1].(type) {
case int:
f.exponent = float64(v)
case float64:
f.exponent = v
default:
return nil, filters.ErrInvalidFilterParameters
}
}
return f, nil
}
func (fadeIn) Request(filters.FilterContext) {}
func (fadeIn) Response(filters.FilterContext) {}
// NewEndpointCreated creates a filter spec for the endpointCreated filter.
func NewEndpointCreated() filters.Spec {
var ec endpointCreated
return ec
}
func (endpointCreated) Name() string { return filters.EndpointCreatedName }
func endpointKey(scheme, host string) string {
return fmt.Sprintf("%s://%s", scheme, host)
}
func (endpointCreated) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 2 {
return nil, filters.ErrInvalidFilterParameters
}
e, ok := args[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
s, h, err := snet.SchemeHost(e)
if err != nil {
return nil, err
}
ec := endpointCreated{which: endpointKey(s, h)}
switch v := args[1].(type) {
case int:
ec.when = time.Unix(int64(v), 0)
case float64:
ec.when = time.Unix(int64(v), 0)
case string:
t, err := time.Parse(time.RFC3339, v)
if err != nil {
return nil, err
}
ec.when = t
case time.Time:
ec.when = v
default:
return nil, filters.ErrInvalidFilterParameters
}
// mitigate potential flakiness caused by clock skew. When the created time is in the future based on
// the local clock, we ignore it.
now := time.Now()
if ec.when.After(now) {
log.Errorf(
"Endpoint created time in the future, fading in without endpoint creation time: %v. Potential clock skew.",
ec.when,
)
ec.when = time.Time{}
}
return ec, nil
}
func (endpointCreated) Request(filters.FilterContext) {}
func (endpointCreated) Response(filters.FilterContext) {}
type PostProcessorOptions struct {
EndpointRegistry *routing.EndpointRegistry
}
// NewPostProcessor creates post-processor for maintaining the detection time of LB endpoints with fade-in
// behavior.
func NewPostProcessor(options PostProcessorOptions) routing.PostProcessor {
return &postProcessor{
endpointRegisty: options.EndpointRegistry,
detected: make(map[string]detectedFadeIn),
}
}
func (p *postProcessor) Do(r []*routing.Route) []*routing.Route {
now := time.Now()
for _, ri := range r {
if ri.Route.BackendType != eskip.LBBackend {
continue
}
ri.LBFadeInDuration = 0
ri.LBFadeInExponent = 1
endpointsCreated := make(map[string]time.Time)
for _, f := range ri.Filters {
switch fi := f.Filter.(type) {
case fadeIn:
ri.LBFadeInDuration = fi.duration
ri.LBFadeInExponent = fi.exponent
case endpointCreated:
endpointsCreated[fi.which] = fi.when
}
}
if ri.LBFadeInDuration <= 0 {
continue
}
for i := range ri.LBEndpoints {
ep := &ri.LBEndpoints[i]
key := endpointKey(ep.Scheme, ep.Host)
detected := p.detected[key].when
if detected.IsZero() || endpointsCreated[key].After(detected) {
detected = now
}
if p.endpointRegisty != nil {
metrics := p.endpointRegisty.GetMetrics(ep.Host)
if endpointsCreated[key].After(metrics.DetectedTime()) {
metrics.SetDetected(endpointsCreated[key])
}
}
p.detected[key] = detectedFadeIn{
when: detected,
duration: ri.LBFadeInDuration,
lastActive: now,
}
}
}
// cleanup old detected, considering last active
for key, d := range p.detected {
// this allows tolerating when a fade-in endpoint temporarily disappears:
if d.lastActive.Add(d.duration).Before(now) {
delete(p.detected, key)
}
}
return r
}