-
Notifications
You must be signed in to change notification settings - Fork 9
/
middleware.go
45 lines (40 loc) · 1.23 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package echo
import (
"net/http"
sentinel "github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/base"
"github.com/labstack/echo/v4"
)
// SentinelMiddleware returns new echo.HandlerFunc.
// Default resource name pattern is {httpMethod}:{apiPath}, such as "GET:/api/:id".
// Default block fallback is to return 429 (Too Many Requests) response.
//
// You may customize your own resource extractor and block handler by setting options.
func SentinelMiddleware(opts ...Option) echo.MiddlewareFunc {
options := evaluateOptions(opts)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
resourceName := c.Request().Method + ":" + c.Path()
if options.resourceExtract != nil {
resourceName = options.resourceExtract(c)
}
entry, blockErr := sentinel.Entry(
resourceName,
sentinel.WithResourceType(base.ResTypeWeb),
sentinel.WithTrafficType(base.Inbound),
)
if blockErr != nil {
if options.blockFallback != nil {
err = options.blockFallback(c)
} else {
// default error response
err = c.JSON(http.StatusTooManyRequests, "Blocked by Sentinel")
}
return err
}
defer entry.Exit()
err = next(c)
return err
}
}
}