tokenbucket

package module
v2.0.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Example

Simple example of server initialization code.

package main

import (
	"github.com/juju/ratelimit"
	"google.golang.org/grpc"

	grpc_ratelimit "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit"
)

// Hard-coded for simplicity sake, but make this configurable in your application.
const (
	// Add 5 token per seconds.
	rate = 5
	// Capacity of bucket. allow only 40 requests.
	tokenCapacity = 40
)

// Simple example of server initialization code.
func main() {
	limiter := TokenBucketInterceptor{}
	limiter.tokenBucket = ratelimit.NewBucket(rate, int64(tokenCapacity))

	_ = grpc.NewServer(
		grpc.ChainUnaryInterceptor(
			grpc_ratelimit.UnaryServerInterceptor(&limiter),
		),
		grpc.ChainStreamInterceptor(
			grpc_ratelimit.StreamServerInterceptor(&limiter),
		),
	)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TokenBucketInterceptor

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

TokenBucketInterceptor implement token bucket algorithm.

func (*TokenBucketInterceptor) Limit

Limit implements Limiter interface.

Jump to

Keyboard shortcuts

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