From c8820a346c11e5f495f425257cda4c124badfd15 Mon Sep 17 00:00:00 2001 From: irrun Date: Mon, 1 Apr 2024 13:51:21 +0800 Subject: [PATCH] chore: remove useless files --- syncutils/batch_runner.go | 88 ---------------------------------- syncutils/batch_runner_test.go | 49 ------------------- 2 files changed, 137 deletions(-) delete mode 100644 syncutils/batch_runner.go delete mode 100644 syncutils/batch_runner_test.go diff --git a/syncutils/batch_runner.go b/syncutils/batch_runner.go deleted file mode 100644 index b232e6a..0000000 --- a/syncutils/batch_runner.go +++ /dev/null @@ -1,88 +0,0 @@ -package syncutils - -import ( - "sync" - "sync/atomic" - - "go.uber.org/multierr" -) - -type Task func() error - -// BatchRunner is a tool to run tasks concurrently, its methods are not thread-safe -type BatchRunner struct { - concurrencyLimit int - - tasks []Task -} - -// NewBatchRunner creates BatchRunner -func NewBatchRunner() *BatchRunner { - return &BatchRunner{} -} - -// Reset resets all settings and clear tasks -func (br *BatchRunner) Reset() *BatchRunner { - *br = BatchRunner{} - return br -} - -// WithConcurrencyLimit sets concurrency limit -func (br *BatchRunner) WithConcurrencyLimit(limit int) *BatchRunner { - br.concurrencyLimit = limit - return br -} - -// AddTasks adds tasks -func (br *BatchRunner) AddTasks(task ...Task) *BatchRunner { - br.tasks = append(br.tasks, task...) - return br -} - -// Exec execute all added tasks concurrently -func (br *BatchRunner) Exec() error { - tasksCount := len(br.tasks) - if tasksCount == 0 { - return nil - } - if tasksCount == 1 { - return br.tasks[0]() - } - - concurLimit := br.concurrencyLimit - if concurLimit == 0 || concurLimit > tasksCount { - concurLimit = tasksCount - } - - errs := make([]error, tasksCount) - - var wg sync.WaitGroup - wg.Add(concurLimit) - - tidx := int32(-1) - execFunc := func() { - defer wg.Done() - - for { - idx := atomic.AddInt32(&tidx, 1) - if int(idx) >= tasksCount { - return - } - - errs[idx] = br.tasks[idx]() - } - } - - for i := 1; i < concurLimit; i++ { - go execFunc() - } - execFunc() - - wg.Wait() - return multierr.Combine(errs...) -} - -// BatchRun runs tasks concurrently -func BatchRun(tasks ...Task) error { - return NewBatchRunner().AddTasks(tasks...).Exec() -} diff --git a/syncutils/batch_runner_test.go b/syncutils/batch_runner_test.go deleted file mode 100644 index fc7d7cb..0000000 --- a/syncutils/batch_runner_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package syncutils_test - -import ( - "errors" - "sync/atomic" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/bnb-chain/bsc-mev-sentry/syncutils" -) - -func TestBatchRun(t *testing.T) { - br := syncutils.NewBatchRunner() - var sum1, sum2 int32 - for i := int32(1); i < 100000000; i++ { - i := i - br.AddTasks(func() error { - atomic.AddInt32(&sum1, i) - return nil - }) - sum2 += i - } - err := br.WithConcurrencyLimit(30).Exec() - assert.Nil(t, err) - assert.Equal(t, sum2, sum1) - - err = syncutils.BatchRun( - func() error { - return nil - }, - func() error { - return errors.New("test_err") - }, - ) - - assert.EqualError(t, err, "test_err") - - err = syncutils.BatchRun( - func() error { - return errors.New("test_err1") - }, - func() error { - return errors.New("test_err2") - }, - ) - - assert.EqualError(t, err, "test_err1; test_err2") -}