package backoffimport ()// An OperationWithData is executing by RetryWithData() or RetryNotifyWithData().// The operation will be retried using a backoff policy if it returns an error.typeOperationWithData[ any] func() (, error)// An Operation is executing by Retry() or RetryNotify().// The operation will be retried using a backoff policy if it returns an error.typeOperationfunc() errorfunc ( Operation) () OperationWithData[struct{}] {returnfunc() (struct{}, error) {returnstruct{}{}, () }}// Notify is a notify-on-error function. It receives an operation error and// backoff delay if the operation failed (with an error).//// NOTE that if the backoff policy stated to stop retrying,// the notify function isn't called.typeNotifyfunc(error, time.Duration)// Retry the operation o until it does not return error or BackOff stops.// o is guaranteed to be run at least once.//// If o returns a *PermanentError, the operation is not retried, and the// wrapped error is returned.//// Retry sleeps the goroutine for the duration returned by BackOff after a// failed operation returns.func ( Operation, BackOff) error {returnRetryNotify(, , nil)}// RetryWithData is like Retry but returns data in the response too.func [ any]( OperationWithData[], BackOff) (, error) {returnRetryNotifyWithData(, , nil)}// RetryNotify calls notify function with the error and wait duration// for each failed attempt before sleep.func ( Operation, BackOff, Notify) error {returnRetryNotifyWithTimer(, , , nil)}// RetryNotifyWithData is like RetryNotify but returns data in the response too.func [ any]( OperationWithData[], BackOff, Notify) (, error) {returndoRetryNotify(, , , nil)}// RetryNotifyWithTimer calls notify function with the error and wait duration using the given Timer// for each failed attempt before sleep.// A default timer that uses system timer is used when nil is passed.func ( Operation, BackOff, Notify, Timer) error { , := doRetryNotify(.withEmptyData(), , , )return}// RetryNotifyWithTimerAndData is like RetryNotifyWithTimer but returns data in the response too.func [ any]( OperationWithData[], BackOff, Notify, Timer) (, error) {returndoRetryNotify(, , , )}func [ any]( OperationWithData[], BackOff, Notify, Timer) (, error) {var (errortime.Duration )if == nil { = &defaultTimer{} }deferfunc() { .Stop() }() := getContext() .Reset()for { , = ()if == nil {return , nil }var *PermanentErroriferrors.As(, &) {return , .Err }if = .NextBackOff(); == Stop {if := .Err(); != nil {return , }return , }if != nil { (, ) } .Start()select {case<-.Done():return , .Err()case<-.C(): } }}// PermanentError signals that the operation should not be retried.typePermanentErrorstruct {Errerror}func ( *PermanentError) () string {return .Err.Error()}func ( *PermanentError) () error {return .Err}func ( *PermanentError) ( error) bool { , := .(*PermanentError)return}// Permanent wraps the given err in a *PermanentError.func ( error) error {if == nil {returnnil }return &PermanentError{Err: , }}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.