mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* [MM-15267] Utils: add backoff function to allow retries (#10958) * [MM-15267] Utils: add unit test and update retry logic (#10958) * [MM-15267] Utils: Add three retries to ProgressiveRetry (#10958) * [MM-15267] Utils: add comments for progressive retry (#10958) * [MM-15267] Utils: add license header to newly added file (#10958) * [MM-15267] Utils: fix typo (#10958) * [MM-15267] Utils: inline callback in function call (#10958) * [MM-15267] Utils: remove type definition for backoff callback function (#10958) * [MM-15267] Utils: use lookup table for timeout duration (#10958) * [MM-15267] Utils: table driven unit tests for Progressive Backoff (#10958) * [MM-15267] Utils: simplify retry function (#10958) * [MM-15267] Utils: add assert and require packages to test file (#10958) * [MM-15267] Utils: revert changes in go.mod and go.sum (#10958)
27 lines
689 B
Go
27 lines
689 B
Go
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
var backoffTimeouts = []time.Duration{50 * time.Millisecond, 100 * time.Millisecond, 200 * time.Millisecond, 200 * time.Millisecond, 400 * time.Millisecond, 400 * time.Millisecond}
|
|
|
|
// ProgressiveRetry executes a BackoffOperation and waits an increasing time before retrying the operation.
|
|
func ProgressiveRetry(operation func() error) error {
|
|
var err error
|
|
|
|
for attempts := 0; attempts < len(backoffTimeouts); attempts++ {
|
|
err = operation()
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
time.Sleep(backoffTimeouts[attempts])
|
|
}
|
|
|
|
return err
|
|
}
|