mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Database: Don't sleep 10ms before every request (#64832)
Previously every DB operation would wait 10ms before even trying. Now we try first, and defer creating the ticker until we need it.
This commit is contained in:
parent
31d6416157
commit
f4a5f91496
@ -1,14 +1,14 @@
|
||||
package retryer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RetrySignal = int
|
||||
|
||||
const (
|
||||
FuncSuccess RetrySignal = iota
|
||||
FuncFailure
|
||||
FuncFailure RetrySignal = iota
|
||||
FuncComplete
|
||||
FuncError
|
||||
)
|
||||
@ -17,35 +17,33 @@ const (
|
||||
// `maxDelay` after each failure. Stops when the provided function returns `FuncComplete`, or `maxRetries` is reached.
|
||||
func Retry(body func() (RetrySignal, error), maxRetries int, minDelay time.Duration, maxDelay time.Duration) error {
|
||||
currentDelay := minDelay
|
||||
ticker := time.NewTicker(currentDelay)
|
||||
defer ticker.Stop()
|
||||
var ticker *time.Ticker
|
||||
|
||||
retries := 0
|
||||
for range ticker.C {
|
||||
for {
|
||||
response, err := body()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response == FuncComplete {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch response {
|
||||
case FuncSuccess:
|
||||
currentDelay = minDelay
|
||||
ticker.Reset(currentDelay)
|
||||
retries = 0
|
||||
case FuncFailure:
|
||||
retries++
|
||||
if retries >= maxRetries {
|
||||
return errors.New("max retries exceeded")
|
||||
}
|
||||
|
||||
if ticker == nil {
|
||||
ticker = time.NewTicker(currentDelay)
|
||||
defer ticker.Stop()
|
||||
} else {
|
||||
currentDelay = minDuration(currentDelay*2, maxDelay)
|
||||
ticker.Reset(currentDelay)
|
||||
retries++
|
||||
case FuncComplete:
|
||||
return nil
|
||||
}
|
||||
|
||||
if retries >= maxRetries {
|
||||
return nil
|
||||
}
|
||||
<-ticker.C
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func minDuration(a time.Duration, b time.Duration) time.Duration {
|
||||
|
@ -14,9 +14,7 @@ func TestMaxRetries(t *testing.T) {
|
||||
retryVal++
|
||||
return FuncFailure, nil
|
||||
}, 8, 100*time.Millisecond, 100*time.Millisecond)
|
||||
if err != nil {
|
||||
assert.FailNow(t, "Error while retrying function")
|
||||
}
|
||||
assert.Error(t, err) // Exceeding max-retries is an error.
|
||||
|
||||
assert.Equal(t, 8, retryVal)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user