• Stars
    star
    390
  • Rank 110,242 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 10 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A timed rate limiter for Go

go-rate

Build Status GoDoc

go-rate is a rate limiter designed for a range of use cases, including server side spam protection and preventing saturation of APIs you consume.

It is used in production at LangTrend to adhere to the GitHub API rate limits.

Usage

Import github.com/beefsack/go-rate and create a new rate limiter with the rate.New(limit int, interval time.Duration) function.

The rate limiter provides a Wait() and a Try() (bool, time.Duration) method for both blocking and non-blocking functionality respectively.

API documentation available at godoc.org.

Examples

Blocking rate limiting

This example demonstrates limiting the output rate to 3 times per second.

package main

import (
	"fmt"
	"time"

	"github.com/beefsack/go-rate"
)

func main() {
	rl := rate.New(3, time.Second) // 3 times per second
	begin := time.Now()
	for i := 1; i <= 10; i++ {
		rl.Wait()
		fmt.Printf("%d started at %s\n", i, time.Now().Sub(begin))
	}
	// Output:
	// 1 started at 12.584us
	// 2 started at 40.13us
	// 3 started at 44.92us
	// 4 started at 1.000125362s
	// 5 started at 1.000143066s
	// 6 started at 1.000144707s
	// 7 started at 2.000224641s
	// 8 started at 2.000240751s
	// 9 started at 2.00024244s
	// 10 started at 3.000314332s
}

Blocking rate limiting with multiple limiters

This example demonstrates combining rate limiters, one limiting at once per second, the other limiting at 2 times per 3 seconds.

package main

import (
	"fmt"
	"time"

	"github.com/beefsack/go-rate"
)

func main() {
	begin := time.Now()
	rl1 := rate.New(1, time.Second)   // Once per second
	rl2 := rate.New(2, time.Second*3) // 2 times per 3 seconds
	for i := 1; i <= 10; i++ {
		rl1.Wait()
		rl2.Wait()
		fmt.Printf("%d started at %s\n", i, time.Now().Sub(begin))
	}
	// Output:
	// 1 started at 11.197us
	// 2 started at 1.00011941s
	// 3 started at 3.000105858s
	// 4 started at 4.000210639s
	// 5 started at 6.000189578s
	// 6 started at 7.000289992s
	// 7 started at 9.000289942s
	// 8 started at 10.00038286s
	// 9 started at 12.000386821s
	// 10 started at 13.000465465s
}

Non-blocking rate limiting

This example demonstrates non-blocking rate limiting, such as would be used to limit spam in a chat client.

package main

import (
	"fmt"
	"time"

	"github.com/beefsack/go-rate"
)

var rl = rate.New(3, time.Second) // 3 times per second

func say(message string) {
	if ok, remaining := rl.Try(); ok {
		fmt.Printf("You said: %s\n", message)
	} else {
		fmt.Printf("Spam filter triggered, please wait %s\n", remaining)
	}
}

func main() {
	for i := 1; i <= 5; i++ {
		say(fmt.Sprintf("Message %d", i))
	}
	time.Sleep(time.Second / 2)
	say("I waited half a second, is that enough?")
	time.Sleep(time.Second / 2)
	say("Okay, I waited a second.")
	// Output:
	// You said: Message 1
	// You said: Message 2
	// You said: Message 3
	// Spam filter triggered, please wait 999.980816ms
	// Spam filter triggered, please wait 999.976704ms
	// Spam filter triggered, please wait 499.844795ms
	// You said: Okay, I waited a second.
}

Authors

More Repositories

1

webify

Turn shell commands into web services
Go
935
star
2

go-astar

Go implementation of the A* search algorithm
Go
563
star
3

angular-d3

D3.js directives for AngularJS
HTML
109
star
4

git-mirror

Host Git repository mirrors with ease
Go
107
star
5

GDScript-sublime

Godot Engine GDScript syntax highlighting for Sublime Text
GDScript
65
star
6

bgg-ranking-historicals

34
star
7

zsh-simplicity

Minimal composable configs for zsh
Shell
19
star
8

gobndl

Bundle Go dependencies inside Go projects
Go
12
star
9

bgg-climbers

Go
11
star
10

ruby-continent

A ruby library for continents and their countries
Ruby
7
star
11

gar

Go application archiver inspired by JAR for Java
Go
7
star
12

bgg-ranked-csv

Go
6
star
13

go-jch

Jump Consistent Hash implementation in Go
Go
6
star
14

geekdo-chart

A simple application to display Geekdo rankings (boardgamegeek, videogamegeek and rpggeek) as charts over time
Go
4
star
15

go-geekdo

Go
4
star
16

geekdo-search-scraper

Go
4
star
17

github-language-colors

Scrape the GitHub language colors used in the language bar
Go
4
star
18

jch-rs

Jump Consistent Hash for Rust.
Rust
3
star
19

unrealscript-sublime

Deprecated, see https://github.com/Zinggi/UnrealScriptIDE
3
star
20

bash-powerline-installer

Powerline in your bash terminal
Shell
2
star
21

whois_server

2
star
22

domain.availability.js

JavaScript
1
star
23

go-dot

Dot rendering using braille characters in Go
Go
1
star
24

gar-example

An example application designed to use the gar Go application archiver
Go
1
star
25

baconsacktd

1
star
26

rbasic

BASIC interpreter written in Rust
Rust
1
star
27

transmitter-api

API to fetch transmitter data, including location and frequency
Ruby
1
star
28

annscraper

PHP
1
star
29

tourney

PHP
1
star
30

issue-gantt

Ruby
1
star
31

termui-rich-widget

A rich text widget for termui supporting text formatting and input
Go
1
star
32

go-under-cover

Tunnel network traffic through secure websockets
Go
1
star
33

cardy

Cardy is a flashcard server with a bundled jQuery Mobile client.
JavaScript
1
star
34

moodle_quiz_scraper

Ruby
1
star
35

vim_config

My personal vim configuration
Vim Script
1
star
36

design-wok

Connecting businesses with designers
Ruby
1
star
37

gantt.js

CoffeeScript
1
star
38

percy-android

Percy client for Android.
Java
1
star
39

mysql-glb

Benchmark MySQL by replaying a general log
Go
1
star
40

coo

Static web builder, with a vast range of supported languages
CoffeeScript
1
star
41

ez-commerce

Ruby
1
star
42

symbolic_media_library

PHP
1
star
43

pargo

Parser combinator library for Go
Go
1
star
44

spawtz-indoor-cricket-scraper

Go
1
star