-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.go
More file actions
147 lines (119 loc) · 2.95 KB
/
Copy pathcache.go
File metadata and controls
147 lines (119 loc) · 2.95 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sensonet
// Copied from https://github.com/evcc-io/evcc
import (
"errors"
"math"
"sync"
"time"
"github.com/asaskevich/EventBus"
"github.com/benbjohnson/clock"
)
var bus = EventBus.New()
// ErrMustRetry indicates that a rate-limited operation should be retried
var ErrMustRetry = errors.New("must retry")
// ErrTimeout is the error returned when a timeout happened.
// Modeled after context.DeadlineError
var ErrTimeout = errors.New("timeout")
const (
reset = "reset"
backoffDuration = 5 * time.Second
)
func ResetCached() {
bus.Publish(reset)
}
// cached wraps a getter with a cache
type cached[T any] struct {
mux sync.Mutex
clock clock.Clock
updated time.Time
retried time.Time
cache time.Duration
backoffCounter int
g func() (T, error)
val T
err error
}
// Cached wraps a getter with a cache
func Cached[T any](g func() (T, error), cache time.Duration) func() (T, error) {
c := ResettableCached(g, cache)
return c.Get
}
// Cacheable is the interface for a resettable cache
type Cacheable[T any] interface {
Get() (T, error)
Reset()
}
var _ Cacheable[int64] = (*cached[int64])(nil)
// ResettableCached wraps a getter with a cache. It returns a `Cacheable`.
// Instead of the cached getter, the `Get()` and `Reset()` methods are exposed.
func ResettableCached[T any](g func() (T, error), cache time.Duration) *cached[T] {
clock := clock.New()
c := &cached[T]{
clock: clock,
cache: cache,
g: g,
}
_ = bus.Subscribe(reset, c.Reset)
return c
}
func (c *cached[T]) Get() (T, error) {
c.mux.Lock()
defer c.mux.Unlock()
if c.mustUpdate() {
c.val, c.err = c.g()
c.updated = c.clock.Now()
c.retried = c.clock.Now()
if c.err == nil {
c.backoffCounter = 0
}
}
return c.val, c.err
}
func (c *cached[T]) Reset() {
c.mux.Lock()
c.updated = time.Time{}
c.retried = time.Time{}
c.mux.Unlock()
}
func (c *cached[T]) mustUpdate() bool {
return c.clock.Since(c.updated) > c.cache ||
errors.Is(c.err, ErrMustRetry) ||
c.err != nil && c.shouldRetryWithBackoff()
}
// shouldRetryWithBackoff returns true when exponential back-off duration has elapsed since last retry
func (c *cached[T]) shouldRetryWithBackoff() bool {
if c.clock.Since(c.retried) > backoffDuration*time.Duration(math.Pow(2, float64(c.backoffCounter))) {
c.backoffCounter++
return true
}
return false
}
// Value is a cacheable value that can expire
type Value[T any] struct {
mux sync.RWMutex
clock clock.Clock
updated time.Time
cache time.Duration
val T
}
func NewValue[T any](cache time.Duration) *Value[T] {
return &Value[T]{
clock: clock.New(),
cache: cache,
}
}
func (v *Value[T]) Get() (T, error) {
v.mux.RLock()
defer v.mux.RUnlock()
if v.clock.Since(v.updated) > v.cache {
var zero T
return zero, ErrTimeout
}
return v.val, nil
}
func (v *Value[T]) Set(val T) {
v.mux.Lock()
defer v.mux.Unlock()
v.val = val
v.updated = v.clock.Now()
}