sentinel.go 721 B

123456789101112131415161718192021222324252627282930
  1. package middleware
  2. import (
  3. "github.com/alibaba/sentinel-golang/core/system"
  4. sentinel "github.com/alibaba/sentinel-golang/pkg/adapters/gin"
  5. "github.com/gin-gonic/gin"
  6. log "github.com/go-admin-team/go-admin-core/logger"
  7. )
  8. // Sentinel 限流
  9. func Sentinel() gin.HandlerFunc {
  10. if _, err := system.LoadRules([]*system.Rule{
  11. {
  12. MetricType: system.InboundQPS,
  13. TriggerCount: 200,
  14. Strategy: system.BBR,
  15. },
  16. }); err != nil {
  17. log.Fatalf("Unexpected error: %+v", err)
  18. }
  19. return sentinel.SentinelMiddleware(
  20. sentinel.WithBlockFallback(func(ctx *gin.Context) {
  21. ctx.AbortWithStatusJSON(200, map[string]interface{}{
  22. "msg": "too many request; the quota used up!",
  23. "code": 500,
  24. })
  25. }),
  26. )
  27. }