-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from AAStarCommunity/chao/feature/rate-limit-b…
…y-apikey feat: rate limit
- Loading branch information
Showing
6 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,6 @@ go.work | |
build/ | ||
config/*.json | ||
|
||
vendor | ||
|
||
conf/appsettings.*.yaml |
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,50 @@ | ||
package middlewares | ||
|
||
import ( | ||
"AAStarCommunity/EthPaymaster_BackService/rpc_server/api/utils" | ||
"errors" | ||
"github.com/gin-gonic/gin" | ||
"golang.org/x/time/rate" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
DefaultLimit rate.Limit = 50 // limit `DefaultLimit` requests per second | ||
DefaultBurst int = 50 // burst size, for surge traffic | ||
) | ||
|
||
var limiter map[string]*rate.Limiter | ||
|
||
// RateLimiterByApiKey represents the rate limit by each ApiKey for each api calling | ||
func RateLimiterByApiKey() gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
if exists, current := utils.CurrentUser(ctx); exists { | ||
|
||
if limiting(¤t) { | ||
ctx.Next() | ||
} else { | ||
_ = ctx.AbortWithError(http.StatusTooManyRequests, errors.New("too many requests")) | ||
} | ||
} else { | ||
_ = ctx.AbortWithError(http.StatusUnauthorized, errors.New("401 Unauthorized")) | ||
} | ||
} | ||
} | ||
|
||
func limiting(apiKey *string) bool { | ||
|
||
var l *rate.Limiter | ||
if limit, ok := limiter[*apiKey]; ok { | ||
l = limit | ||
} else { | ||
// TODO: different rate config for each current(apiKey) should get from dashboard service | ||
l = rate.NewLimiter(DefaultLimit, DefaultBurst) | ||
limiter[*apiKey] = l | ||
} | ||
|
||
return l.Allow() | ||
} | ||
|
||
func init() { | ||
limiter = make(map[string]*rate.Limiter, 100) | ||
} |
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,43 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRateLimitShouldPreventRequestWhenOverDefaultLimit(t *testing.T) { | ||
|
||
mockApiKey := "TestingAipKey" | ||
|
||
// assuming this for loop taking less than 1 second to finish | ||
for i := 0; i < int(DefaultLimit)+5; i++ { | ||
b := limiting(&mockApiKey) | ||
if i < int(DefaultLimit) { | ||
assert.Equal(t, true, b) | ||
} else { | ||
assert.Equal(t, false, b) | ||
} | ||
} | ||
} | ||
|
||
func TestRateLimiterShouldAllowDefaultLimitPerSecond(t *testing.T) { | ||
if os.Getenv("GITHUB_ACTIONS") != "" { | ||
t.Skip() | ||
return | ||
} | ||
mockApiKey := "TestingAipKey" | ||
|
||
for x := 1; x <= 2; x++ { | ||
for i := 0; i < int(DefaultLimit)+5; i++ { | ||
b := limiting(&mockApiKey) | ||
if i < int(DefaultLimit) { | ||
assert.Equal(t, true, b) | ||
} else { | ||
assert.Equal(t, false, b) | ||
} | ||
} | ||
time.Sleep(time.Second * 2) | ||
} | ||
} |
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