Skip to content

adamdrake/tokenbucket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Overview

This is a Go implementation of the token bucket algorithm used as a rate limiter.

It does not use what might be considered a more Go-like approach of a goroutine with a time.Ticker sending over a channel, but this keeps the implementation simpler.

This implementation is thread safe, and the one operation defined on the struct is protected by a sync.Mutex.

Example

package main

import (
    "fmt"
    "time"
    "github.com/adamdrake/tokenbucket"
)

func main() {
    b := tokenbucket.New(uint64(10), uint64(10), 3 * time.Second)
    i := 0
    for _ = range time.NewTicker(1 * time.Second).C {
        fmt.Println(i, b.Take())
        i++
    }
}

About

A rate limiter with the token bucket algorithm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages