-
Notifications
You must be signed in to change notification settings - Fork 10
/
ratelimited.go
54 lines (44 loc) · 1.17 KB
/
ratelimited.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
package tailscalesd
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
var errStaleResults = errors.New("stale discovery results")
// RateLimitedDiscoverer wraps a Discoverer and limits calls to it to be no more
// frequent than once per Frequency, returning cached values if more frequent
// calls are made.
type RateLimitedDiscoverer struct {
Wrap Discoverer
Frequency time.Duration
mu sync.RWMutex // protects following members
earliest time.Time
last []Device
}
func (c *RateLimitedDiscoverer) refreshDevices(ctx context.Context) ([]Device, error) {
rateLimitedRequestRefreshses.Inc()
devices, err := c.Wrap.Devices(ctx)
if err != nil {
rateLimitedStaleResults.Inc()
return devices, fmt.Errorf("%w: %v", errStaleResults, err)
}
c.mu.Lock()
defer c.mu.Unlock()
c.last = devices
c.earliest = time.Now().Add(c.Frequency)
return devices, nil
}
func (c *RateLimitedDiscoverer) Devices(ctx context.Context) ([]Device, error) {
rateLimitedRequests.Inc()
c.mu.RLock()
expired := time.Now().After(c.earliest)
last := make([]Device, len(c.last))
_ = copy(last, c.last)
c.mu.RUnlock()
if expired {
return c.refreshDevices(ctx)
}
return last, nil
}