ratelimiter

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

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

Go to latest
Published: Oct 20, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

ratelimiter

A Rate Limiter implementation in Golang

Go Report Card GoDoc

quickstart

package main

import (
	"fmt"
	"github.com/billcoding/ratelimiter"
	"time"
)

func main() {
	l := ratelimiter.NewLimiter(ratelimiter.CacheBucket(1, 3, 1, time.Second, ratelimiter.DefaultGenerator(), nil))
	fmt.Println(l.Try()) // output: {} true
	fmt.Println(l.Try()) // output: <nil> false
}

Documentation

Overview

Package ratelimiter

A Rate Limiter implementation in Golang

::quickstart::

package main

import (

"fmt"
"github.com/billcoding/ratelimiter"
"time"

)

func main() {
	l := ratelimiter.NewLimiter(ratelimiter.CacheBucket(1, 3, 1, time.Second, ratelimiter.DefaultGenerator(), nil))
	fmt.Println(l.Try()) // output: {} true
	fmt.Println(l.Try()) // output: <nil> false
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheBucket

func CacheBucket(initSize, maxSize, cacheMaxSize int, duration time.Duration, generator Generator, event *Event) *cacheBucket

CacheBucket return new cacheToken

func DefaultGenerator

func DefaultGenerator() *defaultGenerator

Types

type Bucket

type Bucket interface{ Token() (Tokenizer, bool) }

type Event

type Event struct {
	OnCreate  func(tokenizer Tokenizer)
	OnSave    func(tokenizer Tokenizer)
	OnCache   func(tokenizer Tokenizer)
	OnDiscard func(tokenizer Tokenizer)
	OnRemove  func(tokenizer Tokenizer)
}

type Generator

type Generator interface {
	Generate() Tokenizer
}

type Limiter

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

func NewLimiter

func NewLimiter(bucket Bucket) *Limiter

func (*Limiter) Try

func (l *Limiter) Try() (Tokenizer, bool)

type Tokenizer

type Tokenizer interface{}

Jump to

Keyboard shortcuts

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