Remove usage of context.DeadlineExceeded

for internal decisions as this error is strongly used by other packages. By checking such wrapped errors the internal decision can be influenced accidentally.
In this case the retry mechanism checked if the error is context.DeadlineExceeded and assumed it would be created by the internal context. This assumption was wrong.
This commit is contained in:
Maximilian Paß
2023-10-30 14:51:31 +01:00
committed by Sebastian Serth
parent 6b69a2d732
commit d0dd5c08cb
6 changed files with 22 additions and 12 deletions

View File

@ -3,7 +3,6 @@ package util
import (
"context"
"errors"
"fmt"
"github.com/openHPI/poseidon/pkg/logging"
"time"
)
@ -14,6 +13,7 @@ var (
MaxConnectionRetriesExponential = 18
// InitialWaitingDuration is the default initial duration of waiting after a failed time.
InitialWaitingDuration = time.Second
ErrRetryContextDone = errors.New("the retry context is done")
)
func retryExponential(ctx context.Context, sleep time.Duration, f func() error) func() error {
@ -23,6 +23,7 @@ func retryExponential(ctx context.Context, sleep time.Duration, f func() error)
if err != nil {
select {
case <-ctx.Done():
err = ErrRetryContextDone
case <-time.After(sleep):
sleep *= 2
}
@ -39,7 +40,7 @@ func retryConstant(ctx context.Context, sleep time.Duration, f func() error) fun
if err != nil {
select {
case <-ctx.Done():
return fmt.Errorf("stopped retrying: %w", ctx.Err())
return ErrRetryContextDone
case <-time.After(sleep):
}
}
@ -53,7 +54,7 @@ func retryAttempts(maxAttempts int, f func() error) (err error) {
err = f()
if err == nil {
return nil
} else if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
} else if errors.Is(err, ErrRetryContextDone) {
return err
}
log.WithField("count", i).WithError(err).Debug("retrying after error")