-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeout.go
92 lines (78 loc) · 1.93 KB
/
timeout.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
package hltscl
import (
"context"
"sync"
"time"
)
const (
defaultBackoffFactor = 1.5
defaultRecoveryFactor = 1.2
defaultRecoveryInterval = 10 * time.Second
)
type timeoutController struct {
mu sync.RWMutex
ctx context.Context
// Configuration
baseTimeout time.Duration
minTimeout time.Duration
backoffFactor float64
recoveryFactor float64
// Current state
currentTimeout time.Duration
lastSuccess time.Time
// Recovery settings
recoveryInterval time.Duration
}
func newTimeoutController(
ctx context.Context,
baseTimeout,
minTimeout time.Duration,
) *timeoutController {
tc := &timeoutController{
baseTimeout: baseTimeout,
minTimeout: minTimeout,
currentTimeout: baseTimeout,
backoffFactor: defaultBackoffFactor, // Will reduce timeout by multiplying by 1/x
recoveryFactor: defaultRecoveryFactor, // Will increase timeout by multiplying by x
recoveryInterval: defaultRecoveryInterval,
lastSuccess: time.Now(),
ctx: ctx,
}
go func() {
ticker := time.NewTicker(tc.recoveryInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
tc.attemptRecovery()
case <-tc.ctx.Done():
return
}
}
}()
return tc
}
func (tc *timeoutController) attemptRecovery() {
tc.mu.Lock()
defer tc.mu.Unlock()
// If we haven't had a success in a while, don't recover
if time.Since(tc.lastSuccess) > tc.recoveryInterval*2 {
return
}
tc.currentTimeout = min(time.Duration(float64(tc.currentTimeout)*tc.recoveryFactor), tc.baseTimeout)
}
func (tc *timeoutController) GetTimeout() time.Duration {
tc.mu.RLock()
defer tc.mu.RUnlock()
return tc.currentTimeout
}
func (tc *timeoutController) reportSuccess() {
tc.mu.Lock()
tc.lastSuccess = time.Now()
tc.mu.Unlock()
}
func (tc *timeoutController) ReportFailure() {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.currentTimeout = max(time.Duration(float64(tc.currentTimeout)/tc.backoffFactor), tc.minTimeout)
}