-
Notifications
You must be signed in to change notification settings - Fork 10
/
timeout.go
233 lines (184 loc) · 6.69 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
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
229
230
231
232
233
package provider
import (
"context"
"math"
"time"
"github.com/puppetlabs/leg/timeutil/pkg/clockctx"
"github.com/puppetlabs/vault-plugin-secrets-oauthapp/v3/pkg/oauth2ext/devicecode"
)
type TimeoutAlgorithm interface {
Timeout(ctx context.Context, tok *Token) (time.Duration, bool)
}
func contextWithTimeout(ctx context.Context, alg TimeoutAlgorithm, tok *Token) (context.Context, context.CancelFunc) {
timeout, ok := alg.Timeout(ctx, tok)
if !ok {
return context.WithCancel(ctx)
}
return clockctx.WithTimeout(ctx, timeout)
}
type ConstantTimeoutAlgorithm struct {
timeout time.Duration
}
var _ TimeoutAlgorithm = &ConstantTimeoutAlgorithm{}
func (cta *ConstantTimeoutAlgorithm) Timeout(ctx context.Context, tok *Token) (time.Duration, bool) {
return cta.timeout, true
}
func NewConstantTimeoutAlgorithm(timeout time.Duration) *ConstantTimeoutAlgorithm {
return &ConstantTimeoutAlgorithm{
timeout: timeout,
}
}
type TimeToExpiryPiecewiseTimeoutMapping struct {
Test func(d time.Duration, ok bool) bool
Algorithm TimeoutAlgorithm
}
type TimeToExpiryPiecewiseTimeoutAlgorithm struct {
mappings []TimeToExpiryPiecewiseTimeoutMapping
}
var _ TimeoutAlgorithm = &TimeToExpiryPiecewiseTimeoutAlgorithm{}
func (ttepta *TimeToExpiryPiecewiseTimeoutAlgorithm) Timeout(ctx context.Context, tok *Token) (time.Duration, bool) {
remaining, ok := timeToExpiry(ctx, tok)
for _, mapping := range ttepta.mappings {
if fn := mapping.Test; fn == nil || fn(remaining, ok) {
return mapping.Algorithm.Timeout(ctx, tok)
}
}
return 0, false
}
func NewTimeToExpiryPiecewiseTimeoutAlgorithm(mappings []TimeToExpiryPiecewiseTimeoutMapping) *TimeToExpiryPiecewiseTimeoutAlgorithm {
return &TimeToExpiryPiecewiseTimeoutAlgorithm{
mappings: mappings,
}
}
type LogarithmicTimeoutAlgorithm struct {
expiryLeewayFactor float64
timeout time.Duration
expiryDelta time.Duration
}
var _ TimeoutAlgorithm = &LogarithmicTimeoutAlgorithm{}
func (lta *LogarithmicTimeoutAlgorithm) Timeout(ctx context.Context, tok *Token) (time.Duration, bool) {
remaining, ok := timeToExpiry(ctx, tok)
if !ok {
return lta.timeout, true
}
// This function will scale by 1 when remaining is exactly at the
// expiryDelta and by the leeway factor when remaining is 0.
//
// Note these values are constant vis-a-vis the fields of the struct, so
// could be calculated early, but it feels clearer here.
start := lta.expiryDelta.Seconds()
target := math.Pow(10, 1.0-lta.expiryLeewayFactor)
factor := (start * target) / (1.0 - target)
// Calcluate the appropriate scale value.
scale := 1 - math.Log10((factor+remaining.Seconds())/(start+factor))
if math.IsNaN(scale) {
// Past asymptote or leeway isn't valid (e.g., is exactly 1.0), set
// scale to leeway factor.
scale = lta.expiryLeewayFactor
} else if scale < 0 {
// Below x-axis, set scale to 0 to prevent returning negative number
// (although this will immediate cause a timeout anyway).
scale = 0
}
return time.Duration(float64(lta.timeout) * scale), true
}
func NewLogarithmicTimeoutAlgorithm(expiryLeewayFactor float64, timeout, expiryDelta time.Duration) *LogarithmicTimeoutAlgorithm {
return &LogarithmicTimeoutAlgorithm{
expiryLeewayFactor: expiryLeewayFactor,
timeout: timeout,
expiryDelta: expiryDelta,
}
}
func NewBoundedLogarithmicTimeoutAlgorithm(expiryLeewayFactor float64, timeout, expiryDelta time.Duration) TimeoutAlgorithm {
return NewTimeToExpiryPiecewiseTimeoutAlgorithm([]TimeToExpiryPiecewiseTimeoutMapping{
{
Test: func(d time.Duration, ok bool) bool { return !ok || d >= expiryDelta },
Algorithm: NewConstantTimeoutAlgorithm(timeout),
},
{
Algorithm: NewLogarithmicTimeoutAlgorithm(expiryLeewayFactor, timeout, expiryDelta),
},
})
}
func timeToExpiry(ctx context.Context, tok *Token) (time.Duration, bool) {
now := clockctx.Clock(ctx).Now()
var expiry time.Time
if tok != nil && tok.Token != nil && tok.AccessToken != "" {
expiry = tok.Expiry
}
if expiry.IsZero() {
return 0, false
}
return expiry.Sub(now), true
}
type publicTimeoutOperations struct {
delegate PublicOperations
alg TimeoutAlgorithm
}
func (pto *publicTimeoutOperations) AuthCodeURL(state string, opts ...AuthCodeURLOption) (string, bool) {
return pto.delegate.AuthCodeURL(state, opts...)
}
func (pto *publicTimeoutOperations) DeviceCodeAuth(ctx context.Context, opts ...DeviceCodeAuthOption) (*devicecode.Auth, bool, error) {
ctx, cancel := contextWithTimeout(ctx, pto.alg, nil)
defer cancel()
return pto.delegate.DeviceCodeAuth(ctx, opts...)
}
func (pto *publicTimeoutOperations) DeviceCodeExchange(ctx context.Context, deviceCode string, opts ...DeviceCodeExchangeOption) (*Token, error) {
ctx, cancel := contextWithTimeout(ctx, pto.alg, nil)
defer cancel()
return pto.delegate.DeviceCodeExchange(ctx, deviceCode, opts...)
}
func (pto *publicTimeoutOperations) RefreshToken(ctx context.Context, t *Token, opts ...RefreshTokenOption) (*Token, error) {
ctx, cancel := contextWithTimeout(ctx, pto.alg, t)
defer cancel()
return pto.delegate.RefreshToken(ctx, t, opts...)
}
type privateTimeoutOperations struct {
*publicTimeoutOperations
delegate PrivateOperations
}
func (pto *privateTimeoutOperations) AuthCodeExchange(ctx context.Context, code string, opts ...AuthCodeExchangeOption) (*Token, error) {
ctx, cancel := contextWithTimeout(ctx, pto.alg, nil)
defer cancel()
return pto.delegate.AuthCodeExchange(ctx, code, opts...)
}
func (pto *privateTimeoutOperations) ClientCredentials(ctx context.Context, opts ...ClientCredentialsOption) (*Token, error) {
ctx, cancel := contextWithTimeout(ctx, pto.alg, nil)
defer cancel()
return pto.delegate.ClientCredentials(ctx, opts...)
}
func (pto *privateTimeoutOperations) TokenExchange(ctx context.Context, t *Token, opts ...TokenExchangeOption) (*Token, error) {
ctx, cancel := contextWithTimeout(ctx, pto.alg, t)
defer cancel()
return pto.delegate.TokenExchange(ctx, t, opts...)
}
type TimeoutProvider struct {
delegate Provider
alg TimeoutAlgorithm
}
var _ Provider = &TimeoutProvider{}
func (tp *TimeoutProvider) Version() int {
return tp.delegate.Version()
}
func (tp *TimeoutProvider) Public(clientID string) PublicOperations {
return &publicTimeoutOperations{
delegate: tp.delegate.Public(clientID),
alg: tp.alg,
}
}
func (tp *TimeoutProvider) Private(clientID, clientSecret string) PrivateOperations {
priv := tp.delegate.Private(clientID, clientSecret)
return &privateTimeoutOperations{
publicTimeoutOperations: &publicTimeoutOperations{
delegate: priv,
alg: tp.alg,
},
delegate: priv,
}
}
func NewTimeoutProvider(delegate Provider, alg TimeoutAlgorithm) *TimeoutProvider {
return &TimeoutProvider{
delegate: delegate,
alg: alg,
}
}