limiter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2021 License: Unlicense Imports: 3 Imported by: 0

README

Build Status codecov

hash-limiter

simple lock-free limiter using hash and atomic operations

Usage

import (
    limiter "github.com/dongnguyenvt/hash-limiter"
)

func main() {
    // create a limiter with 1024 buckets and limit of 10 concurrent requests 
    rl, err := limiter.NewLimiter(1024, 10)
    // check error
    client := "some client id"
    resource := "some resource id"
    ok, give := rl.Take(client, resource)
    if !ok {
        // return 429 here
        os.Exit(1)
    }
    defer give()
    // handle some expensive tasks
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Give

type Give func()

type Limiter

type Limiter interface {
	Take(key ...string) (ok bool, give Give)
}

func NewLimiter

func NewLimiter(bucket, limit int) (Limiter, error)

Jump to

Keyboard shortcuts

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