hertz

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: 8 Imported by: 0

Documentation

Overview

This package provides Sentinel integration for hertz

For server side, users may create a Hertz Server with Sentinel middleware. A simple of server side:

import (
	sentinelPlugin "github.com/sentinel-go/pkg/adapters/hertz"
	"github.com/cloudwego/hertz/pkg/app/server"
)

h := server.New()
h.Use(sentinelPlugin.SentinelServerMiddleware())

For client side, users may create a Hertz Client with Sentinel middleware. A simple of client side:

import (
	sentinelPlugin "github.com/sentinel-go/pkg/adapters/hertz"
	"github.com/cloudwego/hertz/pkg/app/client"
)

client, _ := client.NewClient()
client.Use(sentinelPlugin.SentinelClientMiddleware())

The plugin extracts service FullMethod as the resource name by default. Users may provide customized resource name extractor when creating new Sentinel middlewares (via options).

Fallback logic: the plugin will stop by default if current request is blocked by Sentinel rules. Users may also provide customized fallback logic via WithClientBlockFallback(handler) options for client side.

the plugin will return "429 Too Many Requests" status code if current request is blocked by Sentinel rules. Users may also provide customized fallback logic via WithServerBlockFallback(handler) options for server side.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SentinelClientMiddleware

func SentinelClientMiddleware(opts ...ClientOption) client.Middleware

SentinelClientMiddleware returns new client.Middleware Default resource name is {method}:{path}, such as "GET:/api/users" Default block fallback is returning blockError Define your own behavior by setting serverOptions

func SentinelServerMiddleware

func SentinelServerMiddleware(opts ...ServerOption) app.HandlerFunc

SentinelServerMiddleware returns new app.HandlerFunc Default resource name is {method}:{path}, such as "GET:/api/users/:id" Default block fallback is returning 429 code Define your own behavior by setting serverOptions

Types

type ClientOption

type ClientOption struct {
	Fn func(o *clientOptions)
}

func WithClientBlockFallback

func WithClientBlockFallback(fn func(ctx context.Context, req *protocol.Request,
	resp *protocol.Response, blockError error) error,
) ClientOption

WithClientBlockFallback sets the fallback handler when requests are blocked for client side.

func WithClientResourceExtractor

func WithClientResourceExtractor(fn func(context.Context, *protocol.Request,
	*protocol.Response) string,
) ClientOption

WithClientResourceExtractor sets the resource extractor of the web requests for client side.

type ServerOption

type ServerOption struct {
	Fn func(*serverOptions)
}

func WithServerBlockFallback

func WithServerBlockFallback(fn func(context.Context, *app.RequestContext)) ServerOption

WithServerBlockFallback sets the fallback handler when requests are blocked for server side.

func WithServerResourceExtractor

func WithServerResourceExtractor(fn func(context.Context, *app.RequestContext) string) ServerOption

WithServerResourceExtractor sets the resource extractor of the web requests for server side.

Jump to

Keyboard shortcuts

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