-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeouts.go
More file actions
46 lines (39 loc) · 1.77 KB
/
Copy pathtimeouts.go
File metadata and controls
46 lines (39 loc) · 1.77 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
package codebook
import (
"errors"
"time"
)
type Timeouts struct {
// ReloadInterval specifies how often should the cache be reloaded.
// This duration is each time randomized by `Randomizer` attribute.
ReloadInterval time.Duration `mapstructure:"reload_interval"`
// ReloadDelay specifies how long should the cache block the next reload after last reload.
// Useful when the cache is reloaded very often due to many invalidations (`InvalidateAll`
// function calls) and data loading is slow or expensive.
// When `InvalidateAll` function is called during this time, the cache is not immediatelly reloaded,
// but the reload is postponed until `ReloadDelay` duration passes. Second and other `InvalidateAll`
// calls during this "blocking" period are being ignored.
// When `InvalidateAll` function is called outside of this "blocking" time, the cache is reloaded
// immediatelly.
// The next reload time is then set as ReloadInterval +/- ReloadInterval * Randomizer.
ReloadDelay time.Duration `mapstructure:"reload_delay"`
// Randomizer specifies how much the timeouts/durations should be randomized.
// Allowed values are from 0 to 1 (included).
// Value 0 means no randomization, 0.1 means 10% randomization, etc.
// is treated as 1.
// e.g. real cache reload interval is set as `ReloadInterval` +/- `ReloadInterval` * `Randomizer`.
// All durations are being randomized each time they are set.
Randomizer float64 `mapstructure:"randomizer"`
}
func (t *Timeouts) check() error {
if t.ReloadDelay > t.ReloadInterval {
return errors.New("ReloadDelay must be less than or equal to ReloadInterval")
}
if t.Randomizer < 0 {
return errors.New("Randomizer cannot be negative")
}
if t.Randomizer > 1 {
return errors.New("Randomizer cannot be greater than 1")
}
return nil
}