-
Notifications
You must be signed in to change notification settings - Fork 5
/
option.go
119 lines (108 loc) · 3.24 KB
/
option.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
package pcache
import (
"fmt"
"net/http"
"time"
)
const (
defaultRefreshIn = 2 * time.Minute
defaultTTL = 10 * time.Minute
)
type config struct {
httpClient *http.Client
preload bool
refreshIn time.Duration
sources []ProviderSource
ttl time.Duration
}
// Option is a function that sets a value in a config.
type Option func(*config) error
// getOpts creates a config and applies Options to it.
func getOpts(opts []Option) (config, error) {
cfg := config{
httpClient: http.DefaultClient,
preload: true,
refreshIn: defaultRefreshIn,
ttl: defaultTTL,
}
for i, opt := range opts {
if err := opt(&cfg); err != nil {
return config{}, fmt.Errorf("option %d failed: %s", i, err)
}
}
return cfg, nil
}
// WithPreload enables or disabled cache preload. Preload performs an initial
// refresh of the cache to populate it. This results in faster lookup times
// when information for multiple providers will be needed, even for a few
// providers. Generally, preload should always be enabled, but can be disabled
// if there are specific situations that require it.
//
// Default is true (enabled).
func WithPreload(preload bool) Option {
return func(cfg *config) error {
cfg.preload = preload
return nil
}
}
// WithClient sets the HTTP client to use when WithSourceURL is used.
func WithClient(c *http.Client) Option {
return func(cfg *config) error {
if c != nil {
cfg.httpClient = c
}
return nil
}
}
// WithRefreshInterval sets the minimul time interval to wait between automatic
// cache refreshes. Once the interval has elapsed since the last refresh, an
// new refresh is started at nest cache Get. If set to 0, then automatic
// refresh is disabled.
//
// Default is 2 minutes.
func WithRefreshInterval(interval time.Duration) Option {
return func(cfg *config) error {
cfg.refreshIn = interval
return nil
}
}
// WithSource adds one or more new provider information sources for the cache
// to pull provider information from. If multiple sources provide the
// information for the same providers, then the provider record with the most
// recent LastAdvertisementTime is uesd.
func WithSource(src ...ProviderSource) Option {
return func(cfg *config) error {
cfg.sources = append(cfg.sources, src...)
return nil
}
}
// WithSourceURL adds one or more new HTTP sources of provider information. It
// is a convenient alternative to calling NewHTTPSource and WithSource for each
// source URL. To use an alternative HTTP client with each source, call
// WithClient first.
func WithSourceURL(urls ...string) Option {
return func(cfg *config) error {
for _, u := range urls {
httpSrc, err := NewHTTPSource(u, cfg.httpClient)
if err != nil {
return err
}
cfg.sources = append(cfg.sources, httpSrc)
}
return nil
}
}
// WithTTL sets the time that provider information remains in the cache after
// it is not longer available from any of the original sources. The time should
// be long enough to cover temporary unavailability of sources.
//
// This is also the amount of time that a negative cache entry will remain in
// the cache before being removed at the next refresh.
//
// Default is 10 minutes.
func WithTTL(ttl time.Duration) Option {
return func(cfg *config) error {
cfg.ttl = ttl
return nil
}
}