Skip to content

Commit

Permalink
Merge pull request #147 from mysteriumnetwork/debounce
Browse files Browse the repository at this point in the history
Add a wait timer after each transaction
  • Loading branch information
tomasmik authored Oct 21, 2021
2 parents 7be0d0a + f84ced2 commit 041d035
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions transfer/multichain_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func TestMultichainQueue(t *testing.T) {
})
t.Run("chain does not exist", func(t *testing.T) {
mq := NewMultichainQueue()
q1 := NewQueue(1)
q2 := NewQueue(1)
q1 := NewQueue(1, 0)
q2 := NewQueue(1, 0)
mq.AddQueue(1, q1)
mq.AddQueue(2, q2)
go q1.Run()
Expand Down
23 changes: 19 additions & 4 deletions transfer/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package transfer
import (
"errors"
"sync"
"time"

"github.com/ethereum/go-ethereum/core/types"
)
Expand All @@ -30,7 +31,8 @@ type Queue struct {
queue chan queueEntry
stop chan struct{}

once sync.Once
debounce time.Duration
once sync.Once
}

// ErrQueueClosed is returned when queue is closed and transaction was not processed.
Expand All @@ -46,10 +48,11 @@ type queueEntry struct {

// NewQueue returns a new queue. Size for the queue can be given
// so that more than 1 transaction could be queued at a time.
func NewQueue(size uint) *Queue {
func NewQueue(size uint, debounceTime time.Duration) *Queue {
return &Queue{
queue: make(chan queueEntry, size),
stop: make(chan struct{}, 0),
queue: make(chan queueEntry, size),
debounce: debounceTime,
stop: make(chan struct{}, 0),
}
}

Expand All @@ -67,10 +70,22 @@ func (q *Queue) Run() {
case entry := <-q.queue:
tx, err := entry.exec()
q.resp(tx, err, entry.resp)
q.waitAfterSend()
}
}
}

func (q *Queue) waitAfterSend() {
if q.debounce == 0 {
return
}

select {
case <-q.stop:
case <-time.After(q.debounce):
}
}

func (q *Queue) resp(tx *types.Transaction, err error, ch chan<- QueueResponse) {
ch <- QueueResponse{
tx: tx,
Expand Down
6 changes: 3 additions & 3 deletions transfer/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestQueue(t *testing.T) {
t.Run("green path", func(t *testing.T) {
q := NewQueue(1)
q := NewQueue(1, 0)
go q.Run()
defer q.Stop()
fn := func() (*types.Transaction, error) {
Expand All @@ -28,7 +28,7 @@ func TestQueue(t *testing.T) {
assert.Equal(t, uint64(0x1), tx.Nonce())
})
t.Run("exec produced an error", func(t *testing.T) {
q := NewQueue(1)
q := NewQueue(1, 0)
go q.Run()
defer q.Stop()
fn := func() (*types.Transaction, error) {
Expand All @@ -43,7 +43,7 @@ func TestQueue(t *testing.T) {
assert.Nil(t, tx)
})
t.Run("closed queue returns an error", func(t *testing.T) {
q := NewQueue(1)
q := NewQueue(1, 0)
q.Stop()
fn := func() (*types.Transaction, error) {
return types.NewTransaction(1, common.Address{}, big.NewInt(1), 10, big.NewInt(1), []byte{}), nil
Expand Down

0 comments on commit 041d035

Please sign in to comment.