Chever John 9c39c8cbd7
init: iloom WMS monorepo with K8s deployment manifests
Go workspace (go.work) with 5 microservices + shared library:
- gateway (8080), auth-service (8081), purchaser-service (8082)
- textile-service (8083), washing-service (8084)
- shared: proto definitions, common packages

Infrastructure: docker-compose for local dev, K8s manifests for
K3s cluster deployment (mysql/redis/etcd + traefik ingress).

Frontend (iloom-flatten) added as git submodule.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-14 11:33:31 +08:00

55 lines
831 B
Go

package middleware
import (
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
)
type bucket struct {
tokens float64
lastCheck time.Time
mu sync.Mutex
}
var buckets sync.Map
func RateLimit(rps int) gin.HandlerFunc {
rate := float64(rps)
return func(c *gin.Context) {
ip := c.ClientIP()
val, _ := buckets.LoadOrStore(ip, &bucket{
tokens: rate,
lastCheck: time.Now(),
})
b := val.(*bucket)
b.mu.Lock()
now := time.Now()
elapsed := now.Sub(b.lastCheck).Seconds()
b.lastCheck = now
b.tokens += elapsed * rate
if b.tokens > rate {
b.tokens = rate
}
if b.tokens < 1 {
b.mu.Unlock()
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{
"code": 42900,
"message": "rate limit exceeded",
})
return
}
b.tokens--
b.mu.Unlock()
c.Next()
}
}