-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EVM-779 Debug Transaction endpoint - throttling (#1818)
- Loading branch information
1 parent
b302fdb
commit 689e360
Showing
11 changed files
with
169 additions
and
9 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jsonrpc | ||
|
||
import ( | ||
"container/heap" | ||
"errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var errRequestLimitExceeded = errors.New("request limit exceeded") | ||
|
||
type Throttling struct { | ||
requestsPerSeconds int | ||
requests timeQueue | ||
lock sync.Mutex | ||
} | ||
|
||
func NewThrottling(requestsPerSeconds int) *Throttling { | ||
return &Throttling{ | ||
requestsPerSeconds: requestsPerSeconds, | ||
} | ||
} | ||
|
||
func (t *Throttling) AttemptRequest() error { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
||
currTime := time.Now().UTC() | ||
|
||
// remove all old requests | ||
for t.requests.Len() > 0 && currTime.Sub(t.requests[0]) > time.Second { | ||
heap.Pop(&t.requests) | ||
} | ||
|
||
// if too many requests in one second return error | ||
if t.requests.Len() == t.requestsPerSeconds { | ||
return errRequestLimitExceeded | ||
} | ||
|
||
heap.Push(&t.requests, currTime) | ||
|
||
return nil | ||
} | ||
|
||
type timeQueue []time.Time | ||
|
||
func (t *timeQueue) Len() int { | ||
return len(*t) | ||
} | ||
|
||
func (t *timeQueue) Swap(i, j int) { | ||
(*t)[i], (*t)[j] = (*t)[j], (*t)[i] | ||
} | ||
|
||
func (t *timeQueue) Push(x interface{}) { | ||
if time, ok := x.(time.Time); ok { | ||
*t = append(*t, time) | ||
} | ||
} | ||
|
||
func (t *timeQueue) Pop() interface{} { | ||
x := (*t)[len(*t)-1] | ||
*t = (*t)[0 : len(*t)-1] | ||
|
||
return x | ||
} | ||
|
||
func (t *timeQueue) Less(i, j int) bool { | ||
return (*t)[i].Compare((*t)[j]) < 0 | ||
} |
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,41 @@ | ||
package jsonrpc | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestThrottling(t *testing.T) { | ||
t.Parallel() | ||
|
||
th := NewThrottling(5) | ||
|
||
assert.NoError(t, th.AttemptRequest()) | ||
assert.NoError(t, th.AttemptRequest()) | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
|
||
assert.NoError(t, th.AttemptRequest()) | ||
assert.NoError(t, th.AttemptRequest()) | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
|
||
assert.NoError(t, th.AttemptRequest()) | ||
assert.ErrorIs(t, th.AttemptRequest(), errRequestLimitExceeded) | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
|
||
assert.ErrorIs(t, th.AttemptRequest(), errRequestLimitExceeded) | ||
|
||
time.Sleep(time.Millisecond * 701) | ||
|
||
assert.NoError(t, th.AttemptRequest()) | ||
assert.Equal(t, 4, th.requests.Len()) | ||
|
||
time.Sleep(time.Millisecond * 1002) | ||
|
||
assert.NoError(t, th.AttemptRequest()) | ||
assert.Equal(t, 1, th.requests.Len()) | ||
} |
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