throttle

package
v0.0.0-...-e2ed998 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2020 License: MIT Imports: 3 Imported by: 0

README

Throttle Middleware

Throttle middleware for gons. This is only a usable example for throttle middleware.

This middleware is using go.uber.org/ratelimit to ratelimit the message consumption.

The middleawre expect two parameters:

  1. Rate limit when throttle is on.
  2. Rate limit when throttle is loosen.

For example:

import (
    "github.com/albertwidi/gonsq"
    "github.com/albertwidi/gonsq/middleware/throttle"
)

// ...

cm := gonsq.ManageConsumers(...)

// This means 100 messages per second when throttled,
// and 200 messages per second when throttle is loosen.
tmw := throttle.New(100,200)

cm.Use(tmw)

You can always create your own middleware and use message.Stats as the trigger to throttle the message consumption:

func (tm *Throttle) Throttle(handler gonsq.HandlerFunc) gonsq.HandlerFunc {
	return func(ctx context.Context, message *gonsq.Message) error {
		if message.Stats.Throttle().IsThrottleLoosen() {
			tm.secondLimit.Take()
		} else if message.Stats.Throttle().IsThrottled() {
			tm.firstLimit.Take()
		}
		return handler(ctx, message)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Throttle

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

Throttle implements gonsq.MiddlewareFunc.

func New

func New(throttleLimit, loosenLimit int) *Throttle

New throttle middleware. Create a new object and initialize the rate-limiter based on the throttleLimit and loosenLimit. This because the gonsq have two level of throttling: throttle and loosenThrottle.

func (*Throttle) Throttle

func (tm *Throttle) Throttle(handler gonsq.HandlerFunc) gonsq.HandlerFunc

Throttle middleware for nsq. This middleware check whether there is some information about throttling in the message.

Jump to

Keyboard shortcuts

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