Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func (r *retryer) WaitForRetry(ctx context.Context, duration time.Duration) {
}
}

func (r *retryer) WaitOrSkipRetry(
ctx context.Context, attempts int, cmd Completed, err error,
) bool {
if delay := r.RetryDelay(attempts, cmd, err); delay == 0 {
func (r *retryer) WaitOrSkipRetry(ctx context.Context, attempts int, cmd Completed, err error) bool {
delay := r.RetryDelay(attempts, cmd, err)
if delay == 0 {
runtime.Gosched()
return true
} else if delay > 0 {
if dl, ok := ctx.Deadline(); !ok || time.Until(dl) > delay {
}
if delay > 0 {
if dl, ok := ctx.Deadline(); !ok || time.Until(dl) > (delay+(100*time.Microsecond)) {
r.WaitForRetry(ctx, delay)
return true
}
Expand Down
27 changes: 27 additions & 0 deletions retryer_custom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rueidis

import (
"context"
"testing"
"time"
)

func TestWaitOrSkipRetry_DeadlineBuffer(t *testing.T) {
r := newRetryer(func(attempts int, cmd Completed, err error) time.Duration {
return 10 * time.Millisecond // This will force a 10ms delay.
})

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()

start := time.Now() // Returns false immediately b/c 5ms < 10ms + buffer inherently
retriable := r.WaitOrSkipRetry(ctx, 1, Completed{}, nil)
duration := time.Since(start)

if retriable {
t.Errorf("Expected retriable to be false when deadline is shorter than delay")
}
if duration > 2*time.Millisecond {
t.Errorf("Expected function to return immediately, but it took %v", duration)
}
}