2024-02-23 14:27:08 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-05-21 13:05:04 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
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
|
|
|
|
|
|
|
|
tickerInterval = 1 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrRequestsOverLimit = fmt.Errorf("number of requests over limit")
|
|
|
|
)
|
|
|
|
|
|
|
|
type callerOnWait struct {
|
|
|
|
requests int
|
|
|
|
ch chan bool
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
type RequestsStorage interface {
|
2024-05-21 13:05:04 +00:00
|
|
|
Get(tag string) (*RequestData, error)
|
|
|
|
Set(data *RequestData) 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-21 13:05:04 +00:00
|
|
|
func (s *InMemRequestsMapStorage) Get(tag string) (*RequestData, error) {
|
|
|
|
data, ok := s.data.Load(tag)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.(*RequestData), nil
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 13:05:04 +00:00
|
|
|
func (s *InMemRequestsMapStorage) Set(data *RequestData) error {
|
|
|
|
s.data.Store(data.Tag, data)
|
2024-05-20 12:38:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-20 11:21:21 +00:00
|
|
|
type RequestData struct {
|
|
|
|
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
|
|
|
|
GetLimit(tag string) (*RequestData, error)
|
|
|
|
Allow(tag string) (bool, error)
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RPCRequestLimiter struct {
|
|
|
|
storage RequestsStorage
|
2024-05-21 17:40:37 +00:00
|
|
|
mu sync.Mutex
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRequestLimiter(storage RequestsStorage) *RPCRequestLimiter {
|
|
|
|
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-21 13:57:34 +00:00
|
|
|
func (rl *RPCRequestLimiter) GetLimit(tag string) (*RequestData, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *RPCRequestLimiter) saveToStorage(tag string, maxRequests int, interval time.Duration, numReqs int, timestamp time.Time) error {
|
2024-05-21 13:05:04 +00:00
|
|
|
data := &RequestData{
|
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-05-20 12:38:02 +00:00
|
|
|
// Check if a number of requests is over the limit within the interval
|
|
|
|
if time.Since(data.CreatedAt) < data.Period {
|
|
|
|
if data.NumReqs >= data.MaxReqs {
|
2024-05-21 13:57:34 +00:00
|
|
|
return false, nil
|
2024-05-20 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := rl.saveToStorage(tag, data.MaxReqs, data.Period, data.NumReqs+1, data.CreatedAt)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the number of requests if the interval has passed
|
|
|
|
err = rl.saveToStorage(tag, data.MaxReqs, data.Period, 0, time.Now())
|
|
|
|
if err != nil {
|
2024-05-21 13:57:34 +00:00
|
|
|
return true, err // still allow if failed to save
|
2024-05-20 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 13:57:34 +00:00
|
|
|
return true, nil
|
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() {
|
|
|
|
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
|
|
|
|
}
|