hystrixlimitter

package
v0.0.0-...-3481ce3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2019 License: MIT Imports: 9 Imported by: 0

README

熔断&限流

  1. 服务端实现限流
  2. 客户端熔断

Documentation

Index

Constants

View Source
const (
	// DefaultLimiter 默认3毫秒
	DefaultLimiter = 3 * time.Millisecond
	// DefaultLimiterBurst 默认缓存token数
	DefaultLimiterBurst = 100
	// DefaultStreamLimiter 流调用 默认3毫米
	DefaultStreamLimiter = 3 * time.Millisecond
	// DefaultStreamLimiterBurst 流调用 默认缓存token数
	DefaultStreamLimiterBurst = 3
	// HystrixLimitterTypeServer 服务端
	HystrixLimitterTypeServer = "server"
	// HystrixLimitterTypeClient 客户端
	HystrixLimitterTypeClient = "client"
)

默认值

Variables

View Source
var (
	// ErrLimitExceed 超出限流器限制
	ErrLimitExceed = errors.New("Rate limit exceed!")
)

Functions

func NewHystrixLimitter

func NewHystrixLimitter(opts ...Option) middleware.Middleware

NewHystrixLimitter 创建熔断限流中间件

Types

type HystrixLimitter

type HystrixLimitter struct {
	Options       *Options
	Limiter       *rate.Limiter // 限流器
	StreamLimiter *rate.Limiter // 流调用 限流器

}

HystrixLimitter 熔断限流中间件

func (*HystrixLimitter) StreamClient

func (hl *HystrixLimitter) StreamClient(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (cs grpc.ClientStream, err error)

StreamClient 流式服客户中间件 grpc.StreamClientInterceptor

func (*HystrixLimitter) StreamHandler

func (hl *HystrixLimitter) StreamHandler(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error)

StreamHandler 流式中间件

func (*HystrixLimitter) UnaryClient

func (hl *HystrixLimitter) UnaryClient(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error)

UnaryClient 非流式客户端中间件 grpc.UnaryClientInterceptor

func (*HystrixLimitter) UnaryHandler

func (hl *HystrixLimitter) UnaryHandler(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error)

UnaryHandler 非流式中间件

type HystrixLimitterType

type HystrixLimitterType string

HystrixLimitterType 客户端还是服务端

type Option

type Option func(*Options)

Option 实例值设置

func ErrorPercentThreshold

func ErrorPercentThreshold(threshold int) Option

ErrorPercentThreshold 错误数量阀值,达到阀值,启动熔断

func FilterOutFunc

func FilterOutFunc(filterOutFunc middleware.FilterFunc) Option

FilterOutFunc 设置中间件忽略函数列表

func Limiter

func Limiter(limiter time.Duration) Option

Limiter 多久生成一个token

func LimiterBurst

func LimiterBurst(limiterBurst int) Option

LimiterBurst 限流缓存token数量

func Logger

func Logger(logger *zap.SugaredLogger) Option

Logger 设置日志对象

func MaxConcurrentRequests

func MaxConcurrentRequests(max int) Option

MaxConcurrentRequests 最大并发数,超过并发返回错误

func RequestVolumeThreshold

func RequestVolumeThreshold(threshold int) Option

RequestVolumeThreshold 请求数量的阀值,用这些数量的请求来计算阀值

func ServiceName

func ServiceName(serviceName string) Option

ServiceName 服务名

func SleepWindow

func SleepWindow(sleep int) Option

SleepWindow 熔断尝试恢复时间

func StreamLimiter

func StreamLimiter(limiter time.Duration) Option

StreamLimiter 多久生成一个token

func StreamLimiterBurst

func StreamLimiterBurst(limiterBurst int) Option

StreamLimiterBurst 限流缓存token数量

func Timeout

func Timeout(t int) Option

Timeout 单位毫秒

func Type

func Type(typ HystrixLimitterType) Option

Type 设置是客户端还是服务端

type Options

type Options struct {
	Type          HystrixLimitterType
	FilterOutFunc middleware.FilterFunc
	Logger        *zap.SugaredLogger
	/* 限流 */
	Limiter            time.Duration // 限流器,多久生成一个token
	StreamLimiter      time.Duration // 流调用 限流器,多久生成一个token
	LimiterBurst       int           // 缓存token数量
	StreamLimiterBurst int           // 流调用 缓存token数量
	/* 熔断 */
	ServiceName            string // 服务名
	Timeout                int    // 单位毫秒
	MaxConcurrentRequests  int    // 最大并发数,超过并发返回错误
	RequestVolumeThreshold int    // 请求数量的阀值,用这些数量的请求来计算阀值
	ErrorPercentThreshold  int    // 错误数量阀值,达到阀值,启动熔断
	SleepWindow            int    // 熔断尝试恢复时间
}

Options 注册相关参数

Jump to

Keyboard shortcuts

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