ratelim

package module
v0.0.0-...-3877d0b Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2015 License: MIT Imports: 3 Imported by: 0

README

ratelim

Useful rate limiting and sync functions

Install

go get github.com/ryanfowler/ratelim

Documentation

The GoDoc is available here.

Examples

Coming...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

func NewCounter

func NewCounter() *Counter

func (*Counter) Dec

func (c *Counter) Dec() int64

func (*Counter) DecBy

func (c *Counter) DecBy(i int64) int64

func (*Counter) Inc

func (c *Counter) Inc() int64

func (*Counter) IncBy

func (c *Counter) IncBy(i int64) int64

func (*Counter) SetTo

func (c *Counter) SetTo(i int64) int64

func (*Counter) Zero

func (c *Counter) Zero() int64

type Limiter

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

func NewLimiter

func NewLimiter(max int64, dur time.Duration) *Limiter

func (*Limiter) Clear

func (lim *Limiter) Clear(key string)

func (*Limiter) ClearAll

func (lim *Limiter) ClearAll()

func (*Limiter) Close

func (lim *Limiter) Close()

func (*Limiter) Dec

func (lim *Limiter) Dec(key string) bool

func (*Limiter) DecBy

func (lim *Limiter) DecBy(key string, val int64) bool

func (*Limiter) Inc

func (lim *Limiter) Inc(key string) bool

func (*Limiter) IncBy

func (lim *Limiter) IncBy(key string, val int64) bool

func (*Limiter) IsClosed

func (lim *Limiter) IsClosed() bool

type List

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

List represents a light-weight implementation of a doubly linked list.

To learn more about doubly linked lists, visit: https://en.wikipedia.org/wiki/Doubly_linked_list

func NewList

func NewList() *List

NewList returns an initialized List pointer.

func (*List) Empty

func (l *List) Empty()

Empty removes all items from the List. After calling this function, the length of the list is 0.

func (*List) LEach

func (l *List) LEach(f func(int, interface{}) bool)

LEach iterates over each item in the List, starting from the left-most node (the head).

The parameter "f" is a function that accepts the index of the node (zero-based), and the value of the current node. This function should return true to continue the iteration, or false to immediately stop and return from the LEach function.

func (*List) LPop

func (l *List) LPop() interface{}

LPop removes the left-most node from the List (i.e. the head), and returns it's value.

func (*List) LPush

func (l *List) LPush(v interface{})

LPush inserts the provided value to the left-most position in the list (the head position).

func (*List) REach

func (l *List) REach(f func(int, interface{}) bool)

REach iterates over each item in the List, starting from the right-most node (the tail).

The parameter "f" is a function that accepts the index of the node (zero-based), and the value of the current node. This function should return true to continue the iteration, or false to immediately stop and return from the REach function.

func (*List) RPop

func (l *List) RPop() interface{}

RPop removes the right-most node from the List (i.e. the tail), and returns it's value.

func (*List) RPush

func (l *List) RPush(v interface{})

RPush inserts the provided value to the right-most position in the list (the tail position).

func (*List) ValueAt

func (l *List) ValueAt(i int) interface{}

ValueAt returns the value of the node at position "i". If the provided position is not in the bounds of the List, nil is returned.

type Pool

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

func NewPool

func NewPool(max int64, new func() interface{}) *Pool

func (*Pool) Empty

func (p *Pool) Empty()

func (*Pool) Get

func (p *Pool) Get() interface{}

func (*Pool) Put

func (p *Pool) Put(item interface{})

func (*Pool) Use

func (p *Pool) Use(f func(interface{}))

type TBucket

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

TBucket is a struct representing the token bucket algorithm.

All provided functions are safe for concurrent use. It is designed to be a fast, lightweight, and lock-free implementation of the token bucket algorithm.

For more information on the token bucket algorithm, visit: https://en.wikipedia.org/wiki/Token_bucket

func NewBurstyTBucket

func NewBurstyTBucket(bsize, burst int64, dur time.Duration) *TBucket

NewBurstyTBucket will create a new token bucket instance.

The parameter "bsize" is the maximum bucket size and the parameter "burst" is the number of tokens to be added to the bucket every time interval "dur".

To learn more about the token bucket algorithm, check out: https://en.wikipedia.org/wiki/Token_bucket

func NewTBucket

func NewTBucket(bsize int64, dur time.Duration) *TBucket

NewTBucket will create a new token bucket instance.

The parameter "bsize" is the maximum bucket size and the parameter "dur" is the time interval another token will be added to the bucket. Using this function is equivilant to calling NewBurstyTBucket(bsize, 1, dur).

To learn more about the token bucket algorithm, visit: https://en.wikipedia.org/wiki/Token_bucket

func (*TBucket) Close

func (tb *TBucket) Close() bool

Close stops the internal ticker that adds tokens. The TBucket instance is now permanently closed and cannpt be reopened. When the TBucket will no longer be used, this function must be called to stop the internal timer from continuing to fire.

It returns true if the TBucket has been closed, or false if the TBucket has already been closed.

func (*TBucket) Empty

func (tb *TBucket) Empty()

Empty removes all tokens from the bucket. This function does not stop the timer that adds new tokens to the bucket.

func (*TBucket) Fill

func (tb *TBucket) Fill()

Fill adds the maximum amount of tokens to the bucket (fills the bucket) according to the defined bucket size.

func (*TBucket) FillTo

func (tb *TBucket) FillTo(n int64)

FillTo adds "n" tokens to the bucket. The value "n" may be larger than the defined bucket size.

func (*TBucket) GetTok

func (tb *TBucket) GetTok() bool

GetTok attempts to retrieve a single token from the bucket.

It returns true if a token has been successfully retrieved, or returns false if no token is available (the bucket is empty).

func (*TBucket) GetToks

func (tb *TBucket) GetToks(n int64) bool

GetToks attempts to retrieve "n" tokens from the bucket.

It returns true if "n" tokens have been successfully retrieved, or returns false if there are not enough tokens available.

The provided parameter "n" cannot be smaller than 1. If a smaller value is provided, the value 1 will be used.

func (*TBucket) IsClosed

func (tb *TBucket) IsClosed() bool

IsClosed returns true if the TBucket has been closed. It returns false if it is still open.

func (*TBucket) IsPaused

func (tb *TBucket) IsPaused() bool

IsPaused returns true if the TBucket has been paused. It returns false if it is not in a paused state.

func (*TBucket) Pause

func (tb *TBucket) Pause() bool

Pause temporarily pauses the TBucket from adding new tokens to the bucket. When paused, tokens in the bucket can still be retrieved with GetTok or GetToks. The TBucket can be closed (with Close) or can be resumed (with Resume).

This function should be used when the TBucket should only be temporarily paused. If the TBucket will not be used again, Close should be called to stop the internal timer.

Pause returns true if the TBucket has been paused, or false if the TBucket is already in a paused state.

func (*TBucket) Resume

func (tb *TBucket) Resume() bool

Resume resumes a TBucket in a paused state and begins adding new tokens to the bucket again.

Resume returns true if the TBucket has been resumed, or false if the TBucket is not in a paused state.

type TBucketQ

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

func NewBurstyTBucketQ

func NewBurstyTBucketQ(bsize, burst int64, dur time.Duration, maxq int64) *TBucketQ

func NewTBucketQ

func NewTBucketQ(bsize int64, dur time.Duration, maxq int64) *TBucketQ

return a new token bucket with the specified maximum bucket size (burst), the time interval for each new token, and the maximum size of the queue

func (*TBucketQ) Close

func (tbq *TBucketQ) Close() bool

Close stops the internal ticker that adds tokens. The TBucketQ instance is now permanently closed and cannpt be reopened. When the TBucketQ will no longer be used, this function must be called to stop the internal timer from continuing to fire.

It returns true if the TBucketQ has been closed, or false if the TBucketQ has already been closed.

func (*TBucketQ) GetTok

func (tbq *TBucketQ) GetTok() bool

request a token; returns true if token obtained, false otherwise

func (*TBucketQ) GetTokNow

func (tbq *TBucketQ) GetTokNow() bool

func (*TBucketQ) GetToksNow

func (tbq *TBucketQ) GetToksNow(n int64) bool

func (*TBucketQ) IsClosed

func (tbq *TBucketQ) IsClosed() bool

IsClosed returns true if the TBucketQ has been closed. It returns false if it is still open.

func (*TBucketQ) IsPaused

func (tbq *TBucketQ) IsPaused() bool

IsPaused returns true if the TBucketQ has been paused. It returns false if it is not in a paused state.

func (*TBucketQ) Pause

func (tbq *TBucketQ) Pause() bool

Pause temporarily pauses the TBucketQ from adding new tokens to the bucket. When paused, tokens in the bucket can still be retrieved with GetTok or GetToks. The TBucketQ can be closed (with Close) or can be resumed (with Resume).

This function should be used when the TBucketQ should only be temporarily paused. If the TBucketQ will not be used again, Close should be called to stop the internal timer.

Pause returns true if the TBucketQ has been paused, or false if the TBucketQ is already in a paused state.

func (*TBucketQ) Resume

func (tbq *TBucketQ) Resume() bool

Resume resumes a TBucketQ in a paused state and begins adding new tokens to the bucket again.

Resume returns true if the TBucketQ has been resumed, or false if the TBucketQ is not in a paused state.

Jump to

Keyboard shortcuts

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