feat(wallet)_: renamed some methods for clarity
This commit is contained in:
parent
9fe87657d6
commit
bf7aabfa3e
|
@ -237,7 +237,7 @@ func (c *ClientWithFallback) IsConnected() bool {
|
||||||
|
|
||||||
func (c *ClientWithFallback) makeCall(ctx context.Context, main func() ([]any, error), fallback func() ([]any, error)) ([]any, error) {
|
func (c *ClientWithFallback) makeCall(ctx context.Context, main func() ([]any, error), fallback func() ([]any, error)) ([]any, error) {
|
||||||
if c.commonLimiter != nil {
|
if c.commonLimiter != nil {
|
||||||
if limited, err := c.commonLimiter.IsLimitReached(c.tag); limited {
|
if allow, err := c.commonLimiter.Allow(c.tag); !allow {
|
||||||
return nil, fmt.Errorf("tag=%s, %w", c.tag, err)
|
return nil, fmt.Errorf("tag=%s, %w", c.tag, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,9 +63,9 @@ type RequestData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestLimiter interface {
|
type RequestLimiter interface {
|
||||||
SetMaxRequests(tag string, maxRequests int, interval time.Duration) error
|
SetLimit(tag string, maxRequests int, interval time.Duration) error
|
||||||
GetMaxRequests(tag string) (*RequestData, error)
|
GetLimit(tag string) (*RequestData, error)
|
||||||
IsLimitReached(tag string) (bool, error)
|
Allow(tag string) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RPCRequestLimiter struct {
|
type RPCRequestLimiter struct {
|
||||||
|
@ -78,7 +78,7 @@ func NewRequestLimiter(storage RequestsStorage) *RPCRequestLimiter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *RPCRequestLimiter) SetMaxRequests(tag string, maxRequests int, interval time.Duration) error {
|
func (rl *RPCRequestLimiter) SetLimit(tag string, maxRequests int, interval time.Duration) error {
|
||||||
err := rl.saveToStorage(tag, maxRequests, interval, 0, time.Now())
|
err := rl.saveToStorage(tag, maxRequests, interval, 0, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to save request data to storage", "error", err)
|
log.Error("Failed to save request data to storage", "error", err)
|
||||||
|
@ -88,7 +88,7 @@ func (rl *RPCRequestLimiter) SetMaxRequests(tag string, maxRequests int, interva
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *RPCRequestLimiter) GetMaxRequests(tag string) (*RequestData, error) {
|
func (rl *RPCRequestLimiter) GetLimit(tag string) (*RequestData, error) {
|
||||||
data, err := rl.storage.Get(tag)
|
data, err := rl.storage.Get(tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -115,37 +115,38 @@ func (rl *RPCRequestLimiter) saveToStorage(tag string, maxRequests int, interval
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rl *RPCRequestLimiter) IsLimitReached(tag string) (bool, error) {
|
func (rl *RPCRequestLimiter) Allow(tag string) (bool, error) {
|
||||||
data, err := rl.storage.Get(tag)
|
data, err := rl.storage.Get(tag)
|
||||||
|
log.Info("Allow", "data", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if data == nil {
|
if data == nil {
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a number of requests is over the limit within the interval
|
// Check if a number of requests is over the limit within the interval
|
||||||
if time.Since(data.CreatedAt) < data.Period {
|
if time.Since(data.CreatedAt) < data.Period {
|
||||||
if data.NumReqs >= data.MaxReqs {
|
if data.NumReqs >= data.MaxReqs {
|
||||||
return true, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := rl.saveToStorage(tag, data.MaxReqs, data.Period, data.NumReqs+1, data.CreatedAt)
|
err := rl.saveToStorage(tag, data.MaxReqs, data.Period, data.NumReqs+1, data.CreatedAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the number of requests if the interval has passed
|
// Reset the number of requests if the interval has passed
|
||||||
err = rl.saveToStorage(tag, data.MaxReqs, data.Period, 0, time.Now())
|
err = rl.saveToStorage(tag, data.MaxReqs, data.Period, 0, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return true, err // still allow if failed to save
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type RPCRpsLimiter struct {
|
type RPCRpsLimiter struct {
|
||||||
|
|
|
@ -13,7 +13,7 @@ func setupTest() (*InMemRequestsMapStorage, RequestLimiter) {
|
||||||
return storage, rl
|
return storage, rl
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetMaxRequests(t *testing.T) {
|
func TestSetLimit(t *testing.T) {
|
||||||
storage, rl := setupTest()
|
storage, rl := setupTest()
|
||||||
|
|
||||||
// Define test inputs
|
// Define test inputs
|
||||||
|
@ -21,8 +21,8 @@ func TestSetMaxRequests(t *testing.T) {
|
||||||
maxRequests := 10
|
maxRequests := 10
|
||||||
interval := time.Second
|
interval := time.Second
|
||||||
|
|
||||||
// Call the SetMaxRequests method
|
// Call the SetLimit method
|
||||||
err := rl.SetMaxRequests(tag, maxRequests, interval)
|
err := rl.SetLimit(tag, maxRequests, interval)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify that the data was saved to storage correctly
|
// Verify that the data was saved to storage correctly
|
||||||
|
@ -34,27 +34,27 @@ func TestSetMaxRequests(t *testing.T) {
|
||||||
require.Equal(t, 0, data.NumReqs)
|
require.Equal(t, 0, data.NumReqs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMaxRequests(t *testing.T) {
|
func TestGetLimit(t *testing.T) {
|
||||||
storage, rl := setupTest()
|
storage, rl := setupTest()
|
||||||
|
|
||||||
|
// Define test inputs
|
||||||
data := &RequestData{
|
data := &RequestData{
|
||||||
Tag: "testTag",
|
Tag: "testTag",
|
||||||
Period: time.Second,
|
Period: time.Second,
|
||||||
MaxReqs: 10,
|
MaxReqs: 10,
|
||||||
NumReqs: 1,
|
NumReqs: 1,
|
||||||
}
|
}
|
||||||
// Define test inputs
|
|
||||||
storage.Set(data)
|
storage.Set(data)
|
||||||
|
|
||||||
// Call the GetMaxRequests method
|
// Call the GetLimit method
|
||||||
ret, err := rl.GetMaxRequests(data.Tag)
|
ret, err := rl.GetLimit(data.Tag)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify the returned data
|
// Verify the returned data
|
||||||
require.Equal(t, data, ret)
|
require.Equal(t, data, ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsLimitReachedWithinPeriod(t *testing.T) {
|
func TestAllowWithinPeriod(t *testing.T) {
|
||||||
storage, rl := setupTest()
|
storage, rl := setupTest()
|
||||||
|
|
||||||
// Define test inputs
|
// Define test inputs
|
||||||
|
@ -71,22 +71,22 @@ func TestIsLimitReachedWithinPeriod(t *testing.T) {
|
||||||
}
|
}
|
||||||
storage.Set(data)
|
storage.Set(data)
|
||||||
|
|
||||||
// Call the IsLimitReached method
|
// Call the Allow method
|
||||||
for i := 0; i < maxRequests; i++ {
|
for i := 0; i < maxRequests; i++ {
|
||||||
limitReached, err := rl.IsLimitReached(tag)
|
allow, err := rl.Allow(tag)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify the result
|
// Verify the result
|
||||||
require.False(t, limitReached)
|
require.True(t, allow)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the IsLimitReached method again
|
// Call the Allow method again
|
||||||
limitReached, err := rl.IsLimitReached(tag)
|
allow, err := rl.Allow(tag)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, limitReached)
|
require.False(t, allow)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsLimitReachedWhenPeriodPassed(t *testing.T) {
|
func TestAllowWhenPeriodPassed(t *testing.T) {
|
||||||
storage, rl := setupTest()
|
storage, rl := setupTest()
|
||||||
|
|
||||||
// Define test inputs
|
// Define test inputs
|
||||||
|
@ -104,10 +104,10 @@ func TestIsLimitReachedWhenPeriodPassed(t *testing.T) {
|
||||||
}
|
}
|
||||||
storage.Set(data)
|
storage.Set(data)
|
||||||
|
|
||||||
// Call the IsLimitReached method
|
// Call the Allow method
|
||||||
limitReached, err := rl.IsLimitReached(tag)
|
allow, err := rl.Allow(tag)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify the result
|
// Verify the result
|
||||||
require.False(t, limitReached)
|
require.True(t, allow)
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,8 +29,8 @@ const (
|
||||||
transferHistoryTag = "transfer_history"
|
transferHistoryTag = "transfer_history"
|
||||||
newTransferHistoryTag = "new_transfer_history"
|
newTransferHistoryTag = "new_transfer_history"
|
||||||
|
|
||||||
transferHistoryMaxRequests = 10000
|
transferHistoryLimit = 10000
|
||||||
transferHistoryMaxRequestsPeriod = 24 * time.Hour
|
transferHistoryLimitPeriod = 24 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
type nonceInfo struct {
|
type nonceInfo struct {
|
||||||
|
@ -1123,7 +1123,7 @@ func (c *loadBlocksAndTransfersCommand) fetchHistoryBlocksForAccount(group *asyn
|
||||||
|
|
||||||
chainClient := chain.ClientWithTag(c.chainClient, transferHistoryTag)
|
chainClient := chain.ClientWithTag(c.chainClient, transferHistoryTag)
|
||||||
limiter := chain.NewRequestLimiter(chain.NewInMemRequestsMapStorage())
|
limiter := chain.NewRequestLimiter(chain.NewInMemRequestsMapStorage())
|
||||||
limiter.SetMaxRequests(transferHistoryTag, transferHistoryMaxRequests, transferHistoryMaxRequestsPeriod)
|
limiter.SetLimit(transferHistoryTag, transferHistoryLimit, transferHistoryLimitPeriod)
|
||||||
chainClient.SetLimiter(limiter)
|
chainClient.SetLimiter(limiter)
|
||||||
|
|
||||||
fbc := &findBlocksCommand{
|
fbc := &findBlocksCommand{
|
||||||
|
|
|
@ -1047,7 +1047,7 @@ func setupFindBlocksCommand(t *testing.T, accountAddress common.Address, fromBlo
|
||||||
// Reimplement the common function that is called from every method to check for the limit
|
// Reimplement the common function that is called from every method to check for the limit
|
||||||
countAndlog = func(tc *TestClient, method string, params ...interface{}) error {
|
countAndlog = func(tc *TestClient, method string, params ...interface{}) error {
|
||||||
if tc.GetLimiter() != nil {
|
if tc.GetLimiter() != nil {
|
||||||
if limited, _ := tc.GetLimiter().IsLimitReached(transferHistoryTag); limited {
|
if allow, _ := tc.GetLimiter().Allow(transferHistoryTag); !allow {
|
||||||
t.Log("ERROR: requests over limit")
|
t.Log("ERROR: requests over limit")
|
||||||
return chain.ErrRequestsOverLimit
|
return chain.ErrRequestsOverLimit
|
||||||
}
|
}
|
||||||
|
@ -1180,7 +1180,7 @@ func TestFindBlocksCommandWithLimiter(t *testing.T) {
|
||||||
fbc, tc, blockChannel, _ := setupFindBlocksCommand(t, accountAddress, big.NewInt(0), big.NewInt(20), rangeSize, balances, nil, nil, nil, nil)
|
fbc, tc, blockChannel, _ := setupFindBlocksCommand(t, accountAddress, big.NewInt(0), big.NewInt(20), rangeSize, balances, nil, nil, nil, nil)
|
||||||
|
|
||||||
limiter := chain.NewRequestLimiter(chain.NewInMemRequestsMapStorage())
|
limiter := chain.NewRequestLimiter(chain.NewInMemRequestsMapStorage())
|
||||||
limiter.SetMaxRequests(transferHistoryTag, maxRequests, time.Hour)
|
limiter.SetLimit(transferHistoryTag, maxRequests, time.Hour)
|
||||||
tc.SetLimiter(limiter)
|
tc.SetLimiter(limiter)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
@ -1207,7 +1207,7 @@ func TestFindBlocksCommandWithLimiterTagDifferentThanTransfers(t *testing.T) {
|
||||||
|
|
||||||
fbc, tc, blockChannel, _ := setupFindBlocksCommand(t, accountAddress, big.NewInt(0), big.NewInt(20), rangeSize, balances, outgoingERC20Transfers, incomingERC20Transfers, nil, nil)
|
fbc, tc, blockChannel, _ := setupFindBlocksCommand(t, accountAddress, big.NewInt(0), big.NewInt(20), rangeSize, balances, outgoingERC20Transfers, incomingERC20Transfers, nil, nil)
|
||||||
limiter := chain.NewRequestLimiter(chain.NewInMemRequestsMapStorage())
|
limiter := chain.NewRequestLimiter(chain.NewInMemRequestsMapStorage())
|
||||||
limiter.SetMaxRequests("some-other-tag-than-transfer-history", maxRequests, time.Hour)
|
limiter.SetLimit("some-other-tag-than-transfer-history", maxRequests, time.Hour)
|
||||||
tc.SetLimiter(limiter)
|
tc.SetLimiter(limiter)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
Loading…
Reference in New Issue