Merge branch 'main' of https://github.com/72wo/blazing
This commit is contained in:
@@ -12,11 +12,13 @@ import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/xiaoqidun/limit"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
var (
|
||||
Main = gcmd.Command{
|
||||
limiter = limit.New()
|
||||
Main = gcmd.Command{
|
||||
Name: "main",
|
||||
Usage: "main",
|
||||
Brief: "start http server",
|
||||
@@ -52,12 +54,12 @@ func beforeServeHook(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
}
|
||||
|
||||
var limiter = rate.NewLimiter(rate.Limit(10), 1)
|
||||
|
||||
// Limiter is a middleware that implements rate limiting for all HTTP requests.
|
||||
// It returns HTTP 429 (Too Many Requests) when the rate limit is exceeded.
|
||||
func Limiter(r *ghttp.Request) {
|
||||
if !limiter.Allow() {
|
||||
rateLimiter := limiter.Get(r.GetClientIp(), rate.Limit(10), 2)
|
||||
|
||||
if !rateLimiter.Allow() {
|
||||
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
|
||||
r.ExitAll()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user