| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package middleware
- import (
- "fmt"
- "net"
- "net/http"
- "perms-system-server/internal/response"
- "github.com/zeromicro/go-zero/core/limit"
- "github.com/zeromicro/go-zero/core/stores/redis"
- "github.com/zeromicro/go-zero/rest/httpx"
- )
- type RateLimitMiddleware struct {
- limiter *limit.PeriodLimit
- }
- func NewRateLimitMiddleware(rds *redis.Redis, period int, quota int, keyPrefix string) *RateLimitMiddleware {
- limiter := limit.NewPeriodLimit(period, quota, rds, keyPrefix)
- return &RateLimitMiddleware{limiter: limiter}
- }
- func (m *RateLimitMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- ip := extractClientIP(r)
- key := fmt.Sprintf("ip:%s", ip)
- code, _ := m.limiter.Take(key)
- if code == limit.OverQuota {
- httpx.ErrorCtx(r.Context(), w, response.ErrTooManyRequests("请求过于频繁,请稍后再试"))
- return
- }
- next(w, r)
- }
- }
- func extractClientIP(r *http.Request) string {
- host, _, err := net.SplitHostPort(r.RemoteAddr)
- if err != nil {
- return r.RemoteAddr
- }
- return host
- }
|