throttle

package module
v0.0.0-...-207a473 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: MIT Imports: 3 Imported by: 0

README

grpc-throttle

grpc-throttle interceptor for go-grpc-middleware. inspired by jbrandhorst

Get

$ go get github.com/yaronsumel/grpc-throttle

Usage

Make SemaphoreMap with specific size per methods

var sMap = throttle.SemaphoreMap{
    "/authpb.Auth/Method": make(throttle.Semaphore, 1),
}

Create ThrottleFunc which returns Semaphore for method.. or control it in any other way using the the context


func ThrottleFunc(ctx context.Context,fullMethod string) (throttle.Semaphore, bool) {
    if s, ok := sMap[fullMethod]; ok {
        return s, true
    }
    return nil, false
}

Use it as interceptor


server := grpc.NewServer(
    grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
        // keep it last in the interceptor chain
        throttle.StreamServerInterceptor(ThrottleFunc)
    )),
    grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
        // keep it last in the interceptor chain
        throttle.UnaryServerInterceptor(ThrottleFunc),
    )),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamServerInterceptor

func StreamServerInterceptor(fn ThrottleFunc) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new unary server interceptors that performs per-request throttling.

func UnaryServerInterceptor

func UnaryServerInterceptor(fn ThrottleFunc) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptors that performs per-request throttling.

Types

type Semaphore

type Semaphore chan struct{}

Semaphore chan

func (Semaphore) ReleaseSlot

func (s Semaphore) ReleaseSlot()

ReleaseSlot release Semaphore

func (Semaphore) WaitForSlotAvailable

func (s Semaphore) WaitForSlotAvailable(ctx context.Context) error

WaitForSlotAvailable wait for Available Semaphore

type SemaphoreMap

type SemaphoreMap map[string]chan struct{}

SemaphoreMap its is map of FullMethod and Semaphore

type ThrottleFunc

type ThrottleFunc func(ctx context.Context, fullMethod string) (Semaphore, bool)

ThrottleFunc will return Semaphore for each fullMethod string

Jump to

Keyboard shortcuts

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