-
Notifications
You must be signed in to change notification settings - Fork 153
/
client.go
64 lines (51 loc) · 1.87 KB
/
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
/*
Copyright 2023 The Flux authors
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 loader
import (
"context"
"time"
"github.com/go-logr/logr"
"github.com/hashicorp/go-retryablehttp"
ctrl "sigs.k8s.io/controller-runtime"
)
// NewRetryableHTTPClient returns a new retrying HTTP client for loading
// artifacts. The client will retry up to the given number of times before
// giving up. The context is used to log errors.
func NewRetryableHTTPClient(ctx context.Context, retries int) *retryablehttp.Client {
httpClient := retryablehttp.NewClient()
httpClient.RetryWaitMin = 5 * time.Second
httpClient.RetryWaitMax = 30 * time.Second
httpClient.RetryMax = retries
httpClient.Logger = newLoggerForContext(ctx)
return httpClient
}
func newLoggerForContext(ctx context.Context) retryablehttp.LeveledLogger {
return &errorLogger{log: ctrl.LoggerFrom(ctx)}
}
// errorLogger is a wrapper around logr.Logger that implements the
// retryablehttp.LeveledLogger interface while only logging errors.
type errorLogger struct {
log logr.Logger
}
func (l *errorLogger) Error(msg string, keysAndValues ...interface{}) {
l.log.Info(msg, keysAndValues...)
}
func (l *errorLogger) Info(msg string, keysAndValues ...interface{}) {
// Do nothing.
}
func (l *errorLogger) Debug(msg string, keysAndValues ...interface{}) {
// Do nothing.
}
func (l *errorLogger) Warn(msg string, keysAndValues ...interface{}) {
// Do nothing.
}