feat(wallet)_: Fixed account limit period for history to infinite instead of per day
Fix rpc stats doubling. Add test for infinite limit
This commit is contained in:
parent
ce773b69ce
commit
cec11e9313
|
@ -17,6 +17,7 @@ const (
|
||||||
requestsPerSecondStep = 10
|
requestsPerSecondStep = 10
|
||||||
|
|
||||||
tickerInterval = 1 * time.Second
|
tickerInterval = 1 * time.Second
|
||||||
|
LimitInfinitely = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -165,6 +166,11 @@ func (rl *RPCRequestLimiter) Allow(tag string) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if period is forever
|
||||||
|
if data.Period.Milliseconds() == LimitInfinitely {
|
||||||
|
return false, 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 {
|
||||||
|
|
|
@ -114,3 +114,29 @@ func TestAllowWhenPeriodPassed(t *testing.T) {
|
||||||
// Verify the result
|
// Verify the result
|
||||||
require.True(t, allow)
|
require.True(t, allow)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAllowRestrictInfinitelyWhenLimitReached(t *testing.T) {
|
||||||
|
storage, rl := setupTest()
|
||||||
|
|
||||||
|
// Define test inputs
|
||||||
|
tag := "testTag"
|
||||||
|
maxRequests := 10
|
||||||
|
|
||||||
|
// Set up the storage with test data
|
||||||
|
data := &LimitData{
|
||||||
|
Tag: tag,
|
||||||
|
Period: LimitInfinitely,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
MaxReqs: maxRequests,
|
||||||
|
NumReqs: maxRequests,
|
||||||
|
}
|
||||||
|
err := storage.Set(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Call the Allow method
|
||||||
|
allow, err := rl.Allow(tag)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Verify the result
|
||||||
|
require.False(t, allow)
|
||||||
|
}
|
||||||
|
|
|
@ -51,6 +51,5 @@ func CountCallWithTag(method string, tag string) {
|
||||||
methodMap := value.(*sync.Map)
|
methodMap := value.(*sync.Map)
|
||||||
value, _ = methodMap.LoadOrStore(method, uint(0))
|
value, _ = methodMap.LoadOrStore(method, uint(0))
|
||||||
methodMap.Store(method, value.(uint)+1)
|
methodMap.Store(method, value.(uint)+1)
|
||||||
|
stats.total++
|
||||||
CountCall(method)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1127,7 +1127,13 @@ func (c *loadBlocksAndTransfersCommand) fetchHistoryBlocksForAccount(group *asyn
|
||||||
chainClient := chain.ClientWithTag(c.chainClient, accountTag, transferHistoryTag)
|
chainClient := chain.ClientWithTag(c.chainClient, accountTag, transferHistoryTag)
|
||||||
storage := chain.NewLimitsDBStorage(c.db.client)
|
storage := chain.NewLimitsDBStorage(c.db.client)
|
||||||
limiter := chain.NewRequestLimiter(storage)
|
limiter := chain.NewRequestLimiter(storage)
|
||||||
err := limiter.SetLimit(accountTag, transferHistoryLimitPerAccount, transferHistoryLimitPeriod)
|
|
||||||
|
// Check if limit is already reached, then skip the comamnd
|
||||||
|
if allow, _ := limiter.Allow(accountTag); !allow {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := limiter.SetLimit(accountTag, transferHistoryLimitPerAccount, chain.LimitInfinitely)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("fetchHistoryBlocksForAccount SetLimit", "error", err, "accountTag", accountTag)
|
log.Error("fetchHistoryBlocksForAccount SetLimit", "error", err, "accountTag", accountTag)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue