go_zero

package module
v0.0.0-...-3c16d7b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

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

func WithBlockFallback(fn func(r *http.Request) (int, string)) Option

WithBlockFallback sets the fallback handler when requests are blocked.

func WithResourceExtractor

func WithResourceExtractor(fn func(r *http.Request) string) Option

WithResourceExtractor sets the resource extractor of the web requests.

type SentinelRouteMiddleware

type SentinelRouteMiddleware struct {
}

func NewSentinelRouteMiddleware

func NewSentinelRouteMiddleware() *SentinelRouteMiddleware

func (*SentinelRouteMiddleware) Handle

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL