-
Notifications
You must be signed in to change notification settings - Fork 731
/
retry_client.go
132 lines (110 loc) Β· 3.87 KB
/
retry_client.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clients
import (
"context"
"golang.org/x/time/rate"
"k8s.io/apimachinery/pkg/api/meta"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
// defaultRefillRate is the default rate at which potential calls are
// added back to the "bucket" of allowed calls.
defaultRefillRate = 5
// defaultLimitSize is the default starting/max number of potential calls
// per second. Once a call is used, it's added back to the bucket at a rate
// of defaultRefillRate per second.
defaultLimitSize = 5
)
// RetryClient wraps a client to provide rate-limiter respecting retry behavior.
type RetryClient struct {
Limiter *rate.Limiter
client.Client
}
func NewRetryClient(c client.Client) *RetryClient {
return &RetryClient{
Client: c,
Limiter: rate.NewLimiter(rate.Limit(defaultRefillRate), defaultLimitSize),
}
}
// retry will run the provided function, retrying if it fails due to rate limiting.
// If context is canceled, it will return early.
func retry(ctx context.Context, limiter *rate.Limiter, f func() error) error {
for {
if err := ctx.Err(); err != nil {
return err
}
err := f()
if meta.IsNoMatchError(err) {
select {
case <-ctx.Done():
return ctx.Err()
default:
_ = limiter.Wait(ctx)
continue
}
}
return err
}
}
func (c *RetryClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.Get(ctx, key, obj)
})
}
func (c *RetryClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.List(ctx, list, opts...)
})
}
func (c *RetryClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.Create(ctx, obj, opts...)
})
}
func (c *RetryClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.Delete(ctx, obj, opts...)
})
}
func (c *RetryClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.Update(ctx, obj, opts...)
})
}
func (c *RetryClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.Patch(ctx, obj, patch, opts...)
})
}
func (c *RetryClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
return retry(ctx, c.Limiter, func() error {
return c.Client.DeleteAllOf(ctx, obj, opts...)
})
}
func (c *RetryClient) Status() client.StatusWriter {
return &RetryStatusWriter{StatusWriter: c.Client.Status(), Limiter: c.Limiter}
}
type RetryStatusWriter struct {
client.StatusWriter
Limiter *rate.Limiter
}
func (c *RetryStatusWriter) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return retry(ctx, c.Limiter, func() error {
return c.StatusWriter.Update(ctx, obj, opts...)
})
}
func (c *RetryStatusWriter) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
return retry(ctx, c.Limiter, func() error {
return c.StatusWriter.Patch(ctx, obj, patch, opts...)
})
}