1
0
mirror of https://github.com/umputun/reproxy.git synced 2025-11-23 22:04:57 +02:00
Files
reproxy/examples/plugin/vendor/github.com/go-pkgz/repeater/strategy/backoff.go
Umputun 7139c57766 RPC plugins support (#85)
* wip

* resolve merge artifacts

* full coverage for conductor

* wire plugin conductor to main and proxy

* wip, with separate match handler

* split matching logic with another handler, add initial docs

* move parts of proxy to handlers, add tests

* add headers in to be sent to proxied url

* merged from master

* add example with docker compose

* supress excesive debug reporting 0-9 disabled in docker

* add plugin tests

* randomize test port

* lint: minor warns

* lint: err shadow
2021-06-01 02:56:39 -05:00

60 lines
1.3 KiB
Go

package strategy
import (
"context"
"math"
"math/rand"
"sync"
"time"
)
// Backoff implements strategy.Interface for exponential-backoff
// it starts from 100ms (by default, if no Duration set) and goes in steps with last * math.Pow(factor, attempt)
// optional jitter randomize intervals a little bit.
type Backoff struct {
Duration time.Duration
Repeats int
Factor float64
Jitter bool
once sync.Once
}
// Start returns channel, similar to time.Timer
// then publishing signals to channel ch for retries attempt. Closed ch indicates "done" event
// consumer (repeater) should stop it explicitly after completion
func (b *Backoff) Start(ctx context.Context) <-chan struct{} {
b.once.Do(func() {
if b.Duration == 0 {
b.Duration = 100 * time.Millisecond
}
if b.Repeats == 0 {
b.Repeats = 1
}
if b.Factor <= 0 {
b.Factor = 1
}
})
ch := make(chan struct{})
go func() {
defer close(ch)
rnd := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.Repeats; i++ {
select {
case <-ctx.Done():
return
case ch <- struct{}{}:
}
delay := float64(b.Duration) * math.Pow(b.Factor, float64(i))
if b.Jitter {
delay = rnd.Float64()*(float64(2*b.Duration)) + (delay - float64(b.Duration))
}
sleep(ctx, time.Duration(delay))
}
}()
return ch
}