-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
74 lines (62 loc) · 1.67 KB
/
options.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
package cfdns
import (
"net/http"
"time"
"golang.org/x/time/rate"
"github.com/simplesurance/cfdns/log"
"github.com/simplesurance/cfdns/log/niltarget"
)
type Option func(*settings)
type settings struct {
ratelim *rate.Limiter
logger *log.Logger
httpClient *http.Client
logSuccess bool
requestTimeout time.Duration
}
func applyOptions(opts ...Option) *settings {
ret := settings{
ratelim: rate.NewLimiter(rate.Every(defaultRequestInterval), 1),
logger: log.New(niltarget.New()), // by default log messages are suppressed
httpClient: http.DefaultClient,
requestTimeout: 30 * time.Second,
}
for _, opt := range opts {
opt(&ret)
}
return &ret
}
func WithRateLimiter(ratelim *rate.Limiter) Option {
return func(s *settings) {
s.ratelim = ratelim
}
}
func WithLogger(logger *log.Logger) Option {
return func(s *settings) {
s.logger = logger
}
}
func WithHTTPClient(c *http.Client) Option {
return func(s *settings) {
s.httpClient = c
}
}
// WithRequestTimeout configures how long to wait for an HTTP request.
// The default is 1 minute. Setting a value of 0 will make it use the
// default behavior of the HTTP client being used, that might be
// waiting forever.
func WithRequestTimeout(timeout time.Duration) Option {
return func(s *settings) {
s.requestTimeout = timeout
}
}
// WithLogSuccessfulResponses allows logging full request and response
// send to CloudFlare in case of successful response. Debug log must also
// be enabled.
//
// Error responses will always be logged if debug log is enabled.
func WithLogSuccessfulResponses(enable bool) Option {
return func(s *settings) {
s.logSuccess = enable
}
}