2024-09-24 13:07:26 +00:00
|
|
|
package rpclimiter
|
2024-02-23 14:27:08 +00:00
|
|
|
|
|
|
|
import (
|
2024-05-22 07:23:44 +00:00
|
|
|
"database/sql"
|
2024-06-19 05:20:32 +00:00
|
|
|
"errors"
|
2024-02-23 14:27:08 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-05-21 13:05:04 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2024-02-23 14:27:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-05-09 12:07:41 +00:00
|
|
|
defaultMaxRequestsPerSecond = 50
|
2024-02-23 14:27:08 +00:00
|
|
|
minRequestsPerSecond = 20
|
|
|
|
requestsPerSecondStep = 10
|
|
|
|
|
2024-05-23 07:16:42 +00:00
|
|
|
tickerInterval = 1 * time.Second
|
|
|
|
LimitInfinitely = 0
|
2024-02-23 14:27:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-06-19 05:20:32 +00:00
|
|
|
ErrRequestsOverLimit = errors.New("number of requests over limit")
|
2024-02-23 14:27:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type callerOnWait struct {
|
|
|
|
requests int
|
|
|
|
ch chan bool
|
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
type LimitsStorage interface {
|
|
|
|
Get(tag string) (*LimitData, error)
|
|
|
|
Set(data *LimitData) error
|
2024-06-19 05:20:32 +00:00
|
|
|
Delete(tag string) error
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 13:05:04 +00:00
|
|
|
type InMemRequestsMapStorage struct {
|
|
|
|
data sync.Map
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 13:05:04 +00:00
|
|
|
func NewInMemRequestsMapStorage() *InMemRequestsMapStorage {
|
|
|
|
return &InMemRequestsMapStorage{}
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
func (s *InMemRequestsMapStorage) Get(tag string) (*LimitData, error) {
|
2024-05-21 13:05:04 +00:00
|
|
|
data, ok := s.data.Load(tag)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
return data.(*LimitData), nil
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
func (s *InMemRequestsMapStorage) Set(data *LimitData) error {
|
|
|
|
if data == nil {
|
|
|
|
return fmt.Errorf("data is nil")
|
|
|
|
}
|
|
|
|
|
2024-05-21 13:05:04 +00:00
|
|
|
s.data.Store(data.Tag, data)
|
2024-05-20 12:38:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 05:20:32 +00:00
|
|
|
func (s *InMemRequestsMapStorage) Delete(tag string) error {
|
|
|
|
s.data.Delete(tag)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
type LimitsDBStorage struct {
|
|
|
|
db *RPCLimiterDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLimitsDBStorage(db *sql.DB) *LimitsDBStorage {
|
|
|
|
return &LimitsDBStorage{
|
|
|
|
db: NewRPCLimiterDB(db),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LimitsDBStorage) Get(tag string) (*LimitData, error) {
|
|
|
|
return s.db.GetRPCLimit(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LimitsDBStorage) Set(data *LimitData) error {
|
|
|
|
if data == nil {
|
|
|
|
return fmt.Errorf("data is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
limit, err := s.db.GetRPCLimit(data.Tag)
|
2024-05-23 10:01:55 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2024-05-22 07:23:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit == nil {
|
|
|
|
return s.db.CreateRPCLimit(*data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.db.UpdateRPCLimit(*data)
|
|
|
|
}
|
|
|
|
|
2024-06-19 05:20:32 +00:00
|
|
|
func (s *LimitsDBStorage) Delete(tag string) error {
|
|
|
|
return s.db.DeleteRPCLimit(tag)
|
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
type LimitData struct {
|
2024-05-20 11:21:21 +00:00
|
|
|
Tag string
|
|
|
|
CreatedAt time.Time
|
|
|
|
Period time.Duration
|
2024-05-20 12:38:02 +00:00
|
|
|
MaxReqs int
|
|
|
|
NumReqs int
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RequestLimiter interface {
|
2024-05-21 13:57:34 +00:00
|
|
|
SetLimit(tag string, maxRequests int, interval time.Duration) error
|
2024-05-22 07:23:44 +00:00
|
|
|
GetLimit(tag string) (*LimitData, error)
|
2024-06-19 05:20:32 +00:00
|
|
|
DeleteLimit(tag string) error
|
2024-05-21 13:57:34 +00:00
|
|
|
Allow(tag string) (bool, error)
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RPCRequestLimiter struct {
|
2024-05-22 07:23:44 +00:00
|
|
|
storage LimitsStorage
|
2024-05-21 17:40:37 +00:00
|
|
|
mu sync.Mutex
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
func NewRequestLimiter(storage LimitsStorage) *RPCRequestLimiter {
|
2024-05-20 11:21:21 +00:00
|
|
|
return &RPCRequestLimiter{
|
|
|
|
storage: storage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 13:57:34 +00:00
|
|
|
func (rl *RPCRequestLimiter) SetLimit(tag string, maxRequests int, interval time.Duration) error {
|
2024-05-20 12:38:02 +00:00
|
|
|
err := rl.saveToStorage(tag, maxRequests, interval, 0, time.Now())
|
2024-05-20 11:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to save request data to storage", "error", err)
|
2024-05-20 12:38:02 +00:00
|
|
|
return err
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 12:38:02 +00:00
|
|
|
return nil
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 07:23:44 +00:00
|
|
|
func (rl *RPCRequestLimiter) GetLimit(tag string) (*LimitData, error) {
|
2024-05-20 12:38:02 +00:00
|
|
|
data, err := rl.storage.Get(tag)
|
|
|
|
if err != nil {
|
2024-05-21 13:05:04 +00:00
|
|
|
return nil, err
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 05:20:32 +00:00
|
|
|
func (rl *RPCRequestLimiter) DeleteLimit(tag string) error {
|
|
|
|
err := rl.storage.Delete(tag)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to delete request data from storage", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:38:02 +00:00
|
|
|
func (rl *RPCRequestLimiter) saveToStorage(tag string, maxRequests int, interval time.Duration, numReqs int, timestamp time.Time) error {
|
2024-05-22 07:23:44 +00:00
|
|
|
data := &LimitData{
|
2024-05-20 11:21:21 +00:00
|
|
|
Tag: tag,
|
2024-05-20 12:38:02 +00:00
|
|
|
CreatedAt: timestamp,
|
2024-05-20 11:21:21 +00:00
|
|
|
Period: interval,
|
2024-05-20 12:38:02 +00:00
|
|
|
MaxReqs: maxRequests,
|
|
|
|
NumReqs: numReqs,
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := rl.storage.Set(data)
|
|
|
|
if err != nil {
|
2024-05-20 12:38:02 +00:00
|
|
|
log.Error("Failed to save request data to storage", "error", err)
|
2024-05-20 11:21:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-21 13:57:34 +00:00
|
|
|
func (rl *RPCRequestLimiter) Allow(tag string) (bool, error) {
|
2024-05-21 17:40:37 +00:00
|
|
|
rl.mu.Lock()
|
|
|
|
defer rl.mu.Unlock()
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
data, err := rl.storage.Get(tag)
|
|
|
|
if err != nil {
|
2024-05-21 13:57:34 +00:00
|
|
|
return true, err
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 13:05:04 +00:00
|
|
|
if data == nil {
|
2024-05-21 13:57:34 +00:00
|
|
|
return true, nil
|
2024-05-21 13:05:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 05:20:32 +00:00
|
|
|
// Check if the interval has passed and reset the number of requests
|
|
|
|
if time.Since(data.CreatedAt) >= data.Period && data.Period.Milliseconds() != LimitInfinitely {
|
|
|
|
err = rl.saveToStorage(tag, data.MaxReqs, data.Period, 0, time.Now())
|
2024-05-20 12:38:02 +00:00
|
|
|
if err != nil {
|
2024-05-21 13:57:34 +00:00
|
|
|
return true, err
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 13:57:34 +00:00
|
|
|
return true, nil
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 05:20:32 +00:00
|
|
|
// Check if a number of requests is over the limit within the interval
|
|
|
|
if time.Since(data.CreatedAt) < data.Period || data.Period.Milliseconds() == LimitInfinitely {
|
|
|
|
if data.NumReqs >= data.MaxReqs {
|
2024-08-07 17:54:42 +00:00
|
|
|
log.Info("Number of requests over limit",
|
|
|
|
"tag", tag,
|
|
|
|
"numReqs", data.NumReqs,
|
|
|
|
"maxReqs", data.MaxReqs,
|
|
|
|
"period", data.Period,
|
|
|
|
"createdAt", data.CreatedAt.UTC())
|
2024-06-19 05:20:32 +00:00
|
|
|
return false, ErrRequestsOverLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, rl.saveToStorage(tag, data.MaxReqs, data.Period, data.NumReqs+1, data.CreatedAt)
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 05:20:32 +00:00
|
|
|
// Reset the number of requests if the interval has passed
|
|
|
|
return true, rl.saveToStorage(tag, data.MaxReqs, data.Period, 0, time.Now()) // still allow the request if failed to save as not critical
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RPCRpsLimiter struct {
|
2024-02-23 14:27:08 +00:00
|
|
|
uuid uuid.UUID
|
|
|
|
|
|
|
|
maxRequestsPerSecond int
|
|
|
|
maxRequestsPerSecondMutex sync.RWMutex
|
|
|
|
|
|
|
|
requestsMadeWithinSecond int
|
|
|
|
requestsMadeWithinSecondMutex sync.RWMutex
|
|
|
|
|
|
|
|
callersOnWaitForRequests []callerOnWait
|
|
|
|
callersOnWaitForRequestsMutex sync.RWMutex
|
|
|
|
|
|
|
|
quit chan bool
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
func NewRPCRpsLimiter() *RPCRpsLimiter {
|
2024-02-23 14:27:08 +00:00
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
limiter := RPCRpsLimiter{
|
2024-02-23 14:27:08 +00:00
|
|
|
uuid: uuid.New(),
|
|
|
|
maxRequestsPerSecond: defaultMaxRequestsPerSecond,
|
|
|
|
quit: make(chan bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
limiter.start()
|
|
|
|
|
|
|
|
return &limiter
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
func (rl *RPCRpsLimiter) ReduceLimit() {
|
2024-02-23 14:27:08 +00:00
|
|
|
rl.maxRequestsPerSecondMutex.Lock()
|
|
|
|
defer rl.maxRequestsPerSecondMutex.Unlock()
|
|
|
|
if rl.maxRequestsPerSecond <= minRequestsPerSecond {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rl.maxRequestsPerSecond = rl.maxRequestsPerSecond - requestsPerSecondStep
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
func (rl *RPCRpsLimiter) start() {
|
2024-02-23 14:27:08 +00:00
|
|
|
ticker := time.NewTicker(tickerInterval)
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2024-02-23 14:27:08 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
{
|
|
|
|
rl.requestsMadeWithinSecondMutex.Lock()
|
|
|
|
oldrequestsMadeWithinSecond := rl.requestsMadeWithinSecond
|
|
|
|
if rl.requestsMadeWithinSecond != 0 {
|
|
|
|
rl.requestsMadeWithinSecond = 0
|
|
|
|
}
|
|
|
|
rl.requestsMadeWithinSecondMutex.Unlock()
|
|
|
|
if oldrequestsMadeWithinSecond == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rl.callersOnWaitForRequestsMutex.Lock()
|
|
|
|
numOfRequestsToMakeAvailable := rl.maxRequestsPerSecond
|
|
|
|
for {
|
|
|
|
if numOfRequestsToMakeAvailable == 0 || len(rl.callersOnWaitForRequests) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var index = -1
|
|
|
|
for i := 0; i < len(rl.callersOnWaitForRequests); i++ {
|
|
|
|
if rl.callersOnWaitForRequests[i].requests <= numOfRequestsToMakeAvailable {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if index == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
callerOnWait := rl.callersOnWaitForRequests[index]
|
|
|
|
numOfRequestsToMakeAvailable -= callerOnWait.requests
|
|
|
|
rl.callersOnWaitForRequests = append(rl.callersOnWaitForRequests[:index], rl.callersOnWaitForRequests[index+1:]...)
|
|
|
|
|
|
|
|
callerOnWait.ch <- true
|
|
|
|
}
|
|
|
|
rl.callersOnWaitForRequestsMutex.Unlock()
|
|
|
|
|
|
|
|
case <-rl.quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
func (rl *RPCRpsLimiter) Stop() {
|
2024-02-23 14:27:08 +00:00
|
|
|
rl.quit <- true
|
|
|
|
close(rl.quit)
|
|
|
|
for _, callerOnWait := range rl.callersOnWaitForRequests {
|
|
|
|
close(callerOnWait.ch)
|
|
|
|
}
|
|
|
|
rl.callersOnWaitForRequests = nil
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
func (rl *RPCRpsLimiter) WaitForRequestsAvailability(requests int) error {
|
2024-02-23 14:27:08 +00:00
|
|
|
if requests > rl.maxRequestsPerSecond {
|
|
|
|
return ErrRequestsOverLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
rl.requestsMadeWithinSecondMutex.Lock()
|
|
|
|
if rl.requestsMadeWithinSecond+requests <= rl.maxRequestsPerSecond {
|
|
|
|
rl.requestsMadeWithinSecond += requests
|
|
|
|
rl.requestsMadeWithinSecondMutex.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rl.requestsMadeWithinSecondMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
callerOnWait := callerOnWait{
|
|
|
|
requests: requests,
|
|
|
|
ch: make(chan bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
rl.callersOnWaitForRequestsMutex.Lock()
|
|
|
|
rl.callersOnWaitForRequests = append(rl.callersOnWaitForRequests, callerOnWait)
|
|
|
|
rl.callersOnWaitForRequestsMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
<-callerOnWait.ch
|
|
|
|
|
|
|
|
close(callerOnWait.ch)
|
|
|
|
|
|
|
|
rl.requestsMadeWithinSecondMutex.Lock()
|
|
|
|
rl.requestsMadeWithinSecond += requests
|
|
|
|
rl.requestsMadeWithinSecondMutex.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|