rater

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

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

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

README

rater

A rate limiter implementation based on Bucket in Go.

Go Report Card GoDoc

quickstart

package main

import (
	"fmt"
	"time"
	
	r "github.com/go-the-way/rater"
)

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

Documentation

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