ratelimit

package module
v0.0.0-...-70b09e5 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2016 License: MIT Imports: 1 Imported by: 0

README

ratelimit Build Status Coverage Go Report Card GoDoc

A Token Bucket based rate limiter implemented implemented using go-routines.

Examples

RateLimiter.Wait - Blocking

see godocs RateLimiter-WaitMaxDuration

// 2 requests per second refreshing 2 capacity every second
r := ratelimit.NewRateLimiter(time.Second, 2, 2)
maxDuration := 500 * time.Millisecond
s := r.WaitMaxDuration(maxDuration)
fmt.Printf("r.Wait() success[%t]\n", s)
s = r.WaitMaxDuration(maxDuration)
fmt.Printf("r.Wait() success[%t]\n", s)
s = r.WaitMaxDuration(maxDuration)
fmt.Printf("r.Wait() success[%t]\n", s)
// Output:
// r.Wait() success[true]
// r.Wait() success[true]
// r.Wait() success[false]

RateLimiter.Wait - Blocking

see godocs RateLimiter-Wait

// 2 requests per second refreshing 2 capacity every second
r := ratelimit.NewRateLimiter(time.Second, 2, 2)
start := time.Now()
r.Wait()
fmt.Printf("r.Wait() elapsed less than 500 ms [%t]\n", time.Now().Sub(start) < 500*time.Millisecond)
r.Wait()
fmt.Printf("r.Wait() elapsed less than 500 ms [%t]\n", time.Now().Sub(start) < 500*time.Millisecond)
r.Wait()
fmt.Printf("r.Wait() elapsed greater than 500 ms [%t]\n", time.Now().Sub(start) > 500*time.Millisecond)
// Output:
// r.Wait() elapsed less than 500 ms [true]
// r.Wait() elapsed less than 500 ms [true]
// r.Wait() elapsed greater than 500 ms [true]

Contributing

See Contributing

License

MIT License

TODO

  • godoc documentation
  • write examples
  • benchmark vs non-goroutine implementations like https://github.com/beefsack/go-rate
  • configure rate limit policy
    • by default we start the rate limit interval time only when it's actually used (e.g. following twitter api rate limit semantics)
      • more commonly like in a traditionally token bucket algorithm the interval is continuously replenishing tokens see Token Bucket

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

func NewRateLimiter

func NewRateLimiter(resetInterval time.Duration, itemsPerInterval, limit int64) *RateLimiter

func (*RateLimiter) Update

func (r *RateLimiter) Update(limit, remaining int64, resetAt time.Time)

func (*RateLimiter) Wait

func (r *RateLimiter) Wait()
Example
package main

import (
	"fmt"
	"github.com/dougnukem/ratelimit"
	"time"
)

func main() {
	// 2 requests per second refreshing 2 capacity every second
	r := ratelimit.NewRateLimiter(time.Second, 2, 2)
	start := time.Now()
	r.Wait()
	fmt.Printf("r.Wait() elapsed less than 500 ms [%t]\n", time.Now().Sub(start) < 500*time.Millisecond)
	r.Wait()
	fmt.Printf("r.Wait() elapsed less than 500 ms [%t]\n", time.Now().Sub(start) < 500*time.Millisecond)
	r.Wait()
	fmt.Printf("r.Wait() elapsed greater than 500 ms [%t]\n", time.Now().Sub(start) > 500*time.Millisecond)
}
Output:

r.Wait() elapsed less than 500 ms [true]
r.Wait() elapsed less than 500 ms [true]
r.Wait() elapsed greater than 500 ms [true]

func (*RateLimiter) WaitMaxDuration

func (r *RateLimiter) WaitMaxDuration(duration time.Duration) bool
Example
package main

import (
	"fmt"
	"github.com/dougnukem/ratelimit"
	"time"
)

func main() {
	// 2 requests per second refreshing 2 capacity every second
	r := ratelimit.NewRateLimiter(time.Second, 2, 2)
	maxDuration := 500 * time.Millisecond
	s := r.WaitMaxDuration(maxDuration)
	fmt.Printf("r.Wait() success[%t]\n", s)
	s = r.WaitMaxDuration(maxDuration)
	fmt.Printf("r.Wait() success[%t]\n", s)
	s = r.WaitMaxDuration(maxDuration)
	fmt.Printf("r.Wait() success[%t]\n", s)
}
Output:

r.Wait() success[true]
r.Wait() success[true]
r.Wait() success[false]

Jump to

Keyboard shortcuts

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