This go package provides a key based limiter for the go programming language. See Godoc.


Install the package

Open your terminal in the root folder of your go project and go get;

go get github.com/mrwaggel/golimiter

Code example

package main

import (

var l golimiter.Limiter

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


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


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

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

Vars and functions

The interface to reference uninstantiated variables.


Instantiate a limiter

The limit defines how many times a key can be incremented before IsLimited returns true within the given time frame 'expiresAfter'.

golimiter.New(limit int, expiresAfter time.Duration) golimiter.Limiter

Incrementing the counter for a key.

(golimiter.Limiter).Increment(key interface{})

Checking if a key is currently limited.

(golimiter.Limiter).IsLimited(key interface{}) bool

Clearing a key from all limits.

(golimiter.Limiter).Remove(key interface{})

Getting the current value of a key.

(golimiter.Limiter).Count(key interface{}) int

Use cases for a limiter

A limiter could be handy to limit login attempts on a website, or wrong captcha inputs, API requests.