golimiter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: MIT Imports: 2 Imported by: 0

README

golimiter

Go Reference Go Report Card

This packages provides a key based limiter. Read more here.

Usage

go get
go get github.com/mrwaggel/golimiter
example
package main

import (
	"github.com/mrwaggel/golimiter"
	"time"
)

func main() {
	l := golimiter.New(4, time.Second*5)
	key := "a"

	l.Increment(key)
	l.Increment(key)
	l.Increment(key)

	l.Count(key)     // 3
	l.IsLimited(key) // false

	l.Increment(key)

	l.Count(key)     // 4
	l.IsLimited(key) // true

	time.Sleep(time.Second * 6)
	l.Count(key)     // 0
	l.IsLimited(key) // false
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(limit int, expiresAfter time.Duration) *limiterManager

New returns a limiter manager, this is thread safe but does not lock keys like transactions would.

limit: 			how many unexpired entries a key can have
expiresAfter: 	how long before an entry expires for a key
gcInterval: 	the interval when the underlying cache
				should remove expired keys

Types

type Limiter

type Limiter interface {
	// IsLimited checks if the given key is limited
	IsLimited(key interface{}) bool
	// Increment increments the counter
	Increment(key interface{})
	// Remove clears key from all limits
	Remove(key interface{})
	// Count returns the total amount of values
	// that are not expired
	Count(key interface{}) int
}

Limiter is an interface to the limiterManager structure. use New(int, time.Duration) to instantiate a new limiter. See limiterManager (scroll down) for more information.

Jump to

Keyboard shortcuts

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