• Stars
    star
    2,679
  • Rank 17,052 (Top 0.4 %)
  • Language
    Go
  • License
    Other
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Efficient token-bucket-based rate limiter package.

ratelimit

-- import "github.com/juju/ratelimit"

The ratelimit package provides an efficient token bucket implementation. See http://en.wikipedia.org/wiki/Token_bucket.

Usage

func Reader

func Reader(r io.Reader, bucket *Bucket) io.Reader

Reader returns a reader that is rate limited by the given token bucket. Each token in the bucket represents one byte.

func Writer

func Writer(w io.Writer, bucket *Bucket) io.Writer

Writer returns a writer that is rate limited by the given token bucket. Each token in the bucket represents one byte.

type Bucket

type Bucket struct {
}

Bucket represents a token bucket that fills at a predetermined rate. Methods on Bucket may be called concurrently.

func NewBucket

func NewBucket(fillInterval time.Duration, capacity int64) *Bucket

NewBucket returns a new token bucket that fills at the rate of one token every fillInterval, up to the given maximum capacity. Both arguments must be positive. The bucket is initially full.

func NewBucketWithQuantum

func NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket

NewBucketWithQuantum is similar to NewBucket, but allows the specification of the quantum size - quantum tokens are added every fillInterval.

func NewBucketWithRate

func NewBucketWithRate(rate float64, capacity int64) *Bucket

NewBucketWithRate returns a token bucket that fills the bucket at the rate of rate tokens per second up to the given maximum capacity. Because of limited clock resolution, at high rates, the actual rate may be up to 1% different from the specified rate.

func (*Bucket) Available

func (tb *Bucket) Available() int64

Available returns the number of available tokens. It will be negative when there are consumers waiting for tokens. Note that if this returns greater than zero, it does not guarantee that calls that take tokens from the buffer will succeed, as the number of available tokens could have changed in the meantime. This method is intended primarily for metrics reporting and debugging.

func (*Bucket) Rate

func (tb *Bucket) Rate() float64

Rate returns the fill rate of the bucket, in tokens per second.

func (*Bucket) Take

func (tb *Bucket) Take(count int64) time.Duration

Take takes count tokens from the bucket without blocking. It returns the time that the caller should wait until the tokens are actually available.

Note that if the request is irrevocable - there is no way to return tokens to the bucket once this method commits us to taking them.

func (*Bucket) TakeAvailable

func (tb *Bucket) TakeAvailable(count int64) int64

TakeAvailable takes up to count immediately available tokens from the bucket. It returns the number of tokens removed, or zero if there are no available tokens. It does not block.

func (*Bucket) TakeMaxDuration

func (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool)

TakeMaxDuration is like Take, except that it will only take tokens from the bucket if the wait time for the tokens is no greater than maxWait.

If it would take longer than maxWait for the tokens to become available, it does nothing and reports false, otherwise it returns the time that the caller should wait until the tokens are actually available, and reports true.

func (*Bucket) Wait

func (tb *Bucket) Wait(count int64)

Wait takes count tokens from the bucket, waiting until they are available.

func (*Bucket) WaitMaxDuration

func (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool

WaitMaxDuration is like Wait except that it will only take tokens from the bucket if it needs to wait for no greater than maxWait. It reports whether any tokens have been removed from the bucket If no tokens have been removed, it returns immediately.

More Repositories

1

juju

Orchestration engine that enables the deployment, integration and lifecycle management of applications at any scale, on any infrastructure (Kubernetes or otherwise).
Go
2,288
star
2

errors

Common juju errors and functions to annotate errors. Based on juju/errgo
Go
1,382
star
3

errgo

Error tracing and annotation.
Go
228
star
4

utils

General utility functions
Go
213
star
5

juju-gui

Juju-GUI is a web-based GUI for Juju <https://jujucharms.com/>.
JavaScript
182
star
6

loggo

A logging library for Go. Doesn't use the built in go log standard library, but instead offers a replacement.
Go
134
star
7

fslock

Go
129
star
8

persistent-cookiejar

cookiejar is a fork of net/http/cookiejar that allows serialisation of the stored cookies
Go
113
star
9

python-libjuju

Python library for the Juju API
Python
59
star
10

cheatsheet

A Juju Quicksheet with some common usage examples
58
star
11

charm-championship

Submissions for the Juju Charm Championship
43
star
12

charm-tools

Tools for charm authors and maintainers
Python
42
star
13

httprequest

JSON-oriented HTTP server and client helpers
Go
38
star
14

mutex

Provides a named machine level mutex shareable between processes.
Go
28
star
15

cmd

A command line implementation framework
Go
27
star
16

plugins

Basic collection of the first few plugins for Juju
Python
27
star
17

gomaasapi

Go bindings for talking to MAAS
Go
27
star
18

pubsub

Publish and subscribe functionality within a single process in Go.
Go
25
star
19

gnuflag

GNU-compatible flag handling with a stdlib-like API for Go
Go
25
star
20

docs

Juju documentation, edited on https://discourse.charmhub.io/, and published on https://juju.is/docs
Python
23
star
21

ansiterm

Colored writers and tabwriters.
Go
22
star
22

terraform-provider-juju

A Terraform provider for Juju
Go
21
star
23

gocharm

Write your charms in Go!
Go
21
star
24

layer-index

Index of layers for building charms
Python
21
star
25

testing

Testing gocheck suites and checkers used across juju projects
Go
19
star
26

retry

The retry package encapsulates the mechanism around retrying commands.
Go
19
star
27

charm-helpers

Python
18
star
28

amulet

Testing harness and tools for Juju Charms
Python
17
star
29

zaputil

Utility functions related to the zap logging package
Go
16
star
30

charmstore

The charm store server.
Go
15
star
31

clock

Clock definition and a testing clock.
Go
13
star
32

charm

Parsing and testing Juju charms
Go
13
star
33

xml

A fork of the Go xml package with fixed marshaling
Go
11
star
34

worker

Utilities for handling long lived Go workers
Go
10
star
35

juju-academy

Learn to use Juju
JavaScript
10
star
36

mgosession

Session pooling for the mgo package
Go
10
star
37

juju-crashdump

Script to assist in gathering logs and other debugging info from a Juju model
Python
10
star
38

charmstore-client

Client for charmstore.
Go
9
star
39

juju-talks

Presentations about Juju, pull requests welcome!
HTML
9
star
40

js-libjuju

JavaScript API client for Juju
TypeScript
9
star
41

replicaset

Create and manage mongodb replicasets.
Go
8
star
42

firestealer

A command line tool for parsing Prometheus metrics
Python
8
star
43

zip

Fork of Go's zip package with append feature.
Go
8
star
44

httpgovernor

HTTP request concurrency limiter
Go
7
star
45

packaging

An abstraction of different linux packaging systems.
Go
6
star
46

chaos-monkey

A tool to instrument chaos into a Juju environment.
Python
6
star
47

names

A package to deal with juju names (services, units, machines, etc)
Go
6
star
48

schema

coerce dynamically typed data structures into known forms.
Go
6
star
49

hello-juju-charm

The charm for the hello-juju application.
Python
5
star
50

jujusvg

Generate svgs from Juju bundles and environment.
Go
5
star
51

theblues

Python library for the juju charmstore (v4)
Python
5
star
52

1.25-upgrade

Tools to upgrade and move a 1.25 environment to a 2.2.4+ controller
Go
4
star
53

jenkins-github-lander

Web service to aid in landing approved branches automatically with a final test run through jenkins.
Python
4
star
54

juju-tosca

Juju Tosca Translator
Python
4
star
55

go-oracle-cloud

Go client interfacing with the oracle IAAS cloud API.
Go
4
star
56

mgo

The MongoDB driver for Go
Go
3
star
57

aclstore

A simple persistent store for ACLs, with HTTP API
Go
3
star
58

txjuju

A Twisted-based Juju client
Python
3
star
59

bakeryjs

Javascript implementation of the Macaroon Bakery
TypeScript
3
star
60

qthttptest

Check that JSON HTTP endpoints respond appropriately; compatible with quicktest.
Go
3
star
61

juju-restore

Restore script for Juju controllers
Go
3
star
62

version

Go
3
star
63

collections

Deque and set implementations
Go
3
star
64

bundlechanges

A Go library to generate the list of changes required to deploy a bundle
Go
3
star
65

postgrestest

Go support for testing against a live Postgres database
Go
2
star
66

concurrency-limiter

Limit the number of asynchronous concurrent tasks running
JavaScript
2
star
67

juju-gui-charm

Charm for Juju GUI.
Python
2
star
68

autopilot-log-collector

Python
2
star
69

mgopurge

A tool to repair broken mgo/txn transaction references in a Juju MongoDB instance.
Go
2
star
70

httpprof

httpprof is a fork of net/http/pprof which works correctly when not at the server's root
Go
2
star
71

hello-juju

A simple application used to demonstrate juju relations.
HTML
2
star
72

blobstore

This package provides a Mongo GridFS-backed blob storage engine.
Go
2
star
73

mgoutil

A Go package holding utilities related to the mgo package
Go
2
star
74

lru

A Go implementation of a least-recently-used cache
Go
2
star
75

description

Describes the Juju 2.x and 3.x serialization format of a model
Go
2
star
76

charmrepo

Charm repositories and charmstore client packages
Go
2
star
77

webbrowser

Go helpers for interacting with Web browsers.
Go
2
star
78

fake-juju

A juju binary using the dummy provider for integration test purposes.
Go
2
star
79

juju-qa-jenkins

Jenkins configuration for Juju CI
Python
2
star
80

termserver

LXD image builder for the jujushell service
Makefile
1
star
81

rfc

Go implementations of various standards, particularly IETF RFCs.
Go
1
star
82

juju-bundlelib

A Python library for working with Juju bundles.
Python
1
star
83

idmclient

client for USSO to macaroons bridge server
Go
1
star
84

mgomonitor

prometheus stats for gopkg.in/mgo.v2
Go
1
star
85

jknife

jknife are juju db surgery tools - this should only be used with direction of a Juju engineer
Go
1
star
86

jasp

CSS
1
star
87

jujuapidoc

Generate information on the Juju API
Go
1
star
88

juju-controller

A Juju controller charm
Python
1
star
89

jaaslibjs

JavaScript library for interacting with the JAAS services
JavaScript
1
star
90

http

Juju wrapper for the standard go HTTP library.
Go
1
star
91

naturalsort

Sort strings according to natural sort order.
Go
1
star
92

charm-developer-docs

Documenting how to write a Juju charm
Shell
1
star
93

lxc

Fork of lxd/lxc to add Juju specific tweaks
Go
1
star
94

romulus

Go
1
star
95

usso

Go
1
star
96

juju-process-docker

a plugin to allow juju to interface with docker
Go
1
star
97

charm-base-images

Shell
1
star
98

proxy

A golang type for grouping information about proxy variables.
Go
1
star
99

jaas-monitor

Monitor all your jaas models (prototype)
Shell
1
star
100

os

Host OS and series abstractions for Go.
Go
1
star