Documentation ¶
Overview ¶
This package provides Sentinel integration for go-zero.
go-zero provides both zrpc and rest modules.
For go-zero/zrpc, user can call `AddUnaryInterceptors` and `AddStreamInterceptors` on zrpc.RpcServer or `WithUnaryClientInterceptor` on zrpc.ClientOption to add grpc interceptors.
For go-zero/rest, there are two kinds of middlewares. The first one is the global middleware, which can be applied to rest.Server via:
import ( sgz "github.com/sentinel-go/pkg/adapters/go-zero" ) server := rest.MustNewServer(c.RestConf) server.Use(sgz.SentinelMiddleware())
The plugin extracts service FullMethod as the resource name by default. Users may provide customized resource name extractor when creating new Sentinel interceptors (via options).
Fallback logic: the plugin will return the BlockError by default if current request is blocked by Sentinel rules. Users may also provide customized fallback logic via WithXxxBlockFallback(handler) options.
The second one is the routing middleware, which is registered to specific routes via by go-zero automatically. Therefore, it is recomended to create routing middleware based on the generated templates. An example with Sentinel based on go-zero template is provided in `routing_middleware`.
The calling order is first global ones, then routing ones.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SentinelMiddleware ¶
func SentinelMiddleware(opts ...Option) rest.Middleware
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.
Types ¶
type Option ¶
type Option func(*options)
func WithBlockFallback ¶
WithBlockFallback sets the fallback handler when requests are blocked.
type SentinelRouteMiddleware ¶
type SentinelRouteMiddleware struct { }
func NewSentinelRouteMiddleware ¶
func NewSentinelRouteMiddleware() *SentinelRouteMiddleware
func (*SentinelRouteMiddleware) Handle ¶
func (m *SentinelRouteMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc