-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da1908c
commit 3219f31
Showing
15 changed files
with
247 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pipeline | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v3" | ||
"github.com/ozontech/file.d/cfg" | ||
) | ||
|
||
type BatcherBackoff struct { | ||
outFn BatcherOutFn | ||
backoff backoff.BackOff | ||
onRetryError func(err error) | ||
} | ||
|
||
type BackoffOpts struct { | ||
MinRetention time.Duration | ||
Multiplier float64 | ||
AttemptNum uint64 | ||
} | ||
|
||
func NewBatcherBackoff(batcherOutFn BatcherOutFn, opts BackoffOpts, onRetryError func(err error)) *BatcherBackoff { | ||
boff := cfg.GetBackoff(opts.MinRetention, opts.Multiplier, opts.AttemptNum) | ||
return &BatcherBackoff{outFn: batcherOutFn, backoff: boff, onRetryError: onRetryError} | ||
} | ||
|
||
func (b *BatcherBackoff) Out(data *WorkerData, batch *Batch) { | ||
b.backoff.Reset() | ||
err := backoff.Retry(func() error { | ||
return b.outFn(data, batch) | ||
}, b.backoff) | ||
if err != nil { | ||
b.onRetryError(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pipeline | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func TestBackoff(t *testing.T) { | ||
errorCount := &atomic.Int32{} | ||
prevValue := errorCount.Load() | ||
errorFn := func(err error) { | ||
errorCount.Inc() | ||
} | ||
|
||
batcherBackoff := NewBatcherBackoff( | ||
func(workerData *WorkerData, batch *Batch) error { | ||
return nil | ||
}, | ||
BackoffOpts{AttemptNum: 3}, | ||
errorFn, | ||
) | ||
|
||
batcherBackoff.Out(nil, nil) | ||
assert.Equal(t, prevValue, errorCount.Load(), "wrong error count") | ||
} | ||
|
||
func TestBackoffWithError(t *testing.T) { | ||
errorCount := &atomic.Int32{} | ||
prevValue := errorCount.Load() | ||
errorFn := func(err error) { | ||
errorCount.Inc() | ||
} | ||
|
||
batcherBackoff := NewBatcherBackoff( | ||
func(workerData *WorkerData, batch *Batch) error { | ||
return errors.New("some error") | ||
}, | ||
BackoffOpts{AttemptNum: 3}, | ||
errorFn, | ||
) | ||
|
||
batcherBackoff.Out(nil, nil) | ||
assert.Equal(t, prevValue+1, errorCount.Load(), "wrong error count") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.