* add siteID to cache Get * indirect option setters * add mongo cache with tests, add Key and Flusher * lint: minor warns * workaround for cache parallel test * repeater in mongo cache * missing repeater vendor * fix nop cache * add cache mongo benchmark * wired mongo cache, single opts group for mongo * disable goconst * stop cache repeated on not found error * use local mongo for tests in travis
42 lines
849 B
Go
42 lines
849 B
Go
package strategy
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// FixedDelay implements strategy.Interface for fixed intervals up to max repeats
|
|
type FixedDelay struct {
|
|
repeats int
|
|
delay time.Duration
|
|
}
|
|
|
|
// NewFixedDelay makes a Interface
|
|
func NewFixedDelay(repeats int, delay time.Duration) Interface {
|
|
if repeats == 0 {
|
|
repeats = 1
|
|
}
|
|
result := FixedDelay{repeats: repeats, delay: delay}
|
|
return &result
|
|
}
|
|
|
|
// Start returns channel, similar to time.Timer
|
|
// then publishing signals to channel ch for retries attempt.
|
|
// can be terminated (canceled) via context.
|
|
func (s *FixedDelay) Start(ctx context.Context) (ch chan struct{}) {
|
|
ch = make(chan struct{})
|
|
go func() {
|
|
defer close(ch)
|
|
for i := 0; i < s.repeats; i++ {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
ch <- struct{}{}
|
|
time.Sleep(s.delay)
|
|
}
|
|
}
|
|
}()
|
|
return ch
|
|
}
|