limiter

package module
v0.0.0-...-ad27db7 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: Apache-2.0 Imports: 9 Imported by: 4

README

limiter (This is a community driven project)

Adaptive Algorithm for Hertz

This project is inspired by kratos limiter. Thanks to their project.

Algorithm core

      The CPU load is collected by reading /proc/stat, and the CPU load is judged to trigger the flow restriction condition.

  • When the CPU load is less than the expected value: the current time is less than 1s from the last trigger to limit the flow, then determine whether the current maximum number of requests is greater than the past maximum load situation, if it is greater than the load situation, then limit the flow.
  • When the CPU load is greater than the expected value: determine whether the current number of requests is greater than the maximum load in the past, if it is greater than the maximum load in the past, then flow restriction will be performed.
How to use?
  1. Set middleware
    h := server.Default()
    h.Use(limiter.AdaptiveLimit())
  1. Run example code
import (
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/hertz-contrib/limiter"
)

func main() {
	h := server.Default(server.WithHostPorts(":1000"))
	h.Use(limiter.AdaptiveLimit())
	h.GET("/hello", func(c context.Context, ctx *app.RequestContext) {
		ctx.String(consts.StatusOK, "hello")
	})
	h.Spin()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLimit = "Hertz Adaptive Limit"
)

Functions

func AdaptiveLimit

func AdaptiveLimit(opts ...Option) app.HandlerFunc

AdaptiveLimit CPU sampling algorithm using BBR

func NewOption

func NewOption(opts ...Option) options

Types

type BBR

type BBR struct {
	// contains filtered or unexported fields
}

BBR implements bbr-like limiter.It is inspired by sentinel. https://github.com/alibaba/Sentinel/wiki/%E7%B3%BB%E7%BB%9F%E8%87%AA%E9%80%82%E5%BA%94%E9%99%90%E6%B5%81

func NewLimiter

func NewLimiter(opts ...Option) *BBR

func (*BBR) Allow

func (l *BBR) Allow() (func(), error)

Allow determines the alarm triggering conditions, record the interface time consumption and QPS

type Option

type Option func(o *options)

func WithBucket

func WithBucket(bucket int) Option

WithBucket defines bucket number for each window

func WithCPUThreshold

func WithCPUThreshold(threshold int64) Option

WithCPUThreshold defines cpu threshold load. e.g. if you want to set the cpu threshold to 80%, you should set the value to 800.

func WithDecay

func WithDecay(decay float64) Option

WithDecay defines cpu attenuation factor

func WithSamplingTime

func WithSamplingTime(samplingTime time.Duration) Option

WithSamplingTime defines cpu sampling time interval

func WithWindow

func WithWindow(window time.Duration) Option

WithWindow defines time duration per window

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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