ratelimit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 5 Imported by: 0

README

ratelimit

A very hacky rate limiter for Go programs. I'm aware that it's probably a hacky and/or bad Semaphore implementation.

It is not recommended to use this in production. Ever. Unless you really want to, I guess.

bucket

I have exactly zero idea how bucket works. If someone can explain it to me, that would be great.

Installation

go get -d github.com/stoicturtle/ratelimit

Usage

package main

import (
	"net/http"
	"time"
	
	"github.com/stoicturtle/ratelimit"
)

func fnToRateLimit() error {
	_, err := http.Get("https://www.google.com")
	
	return err
}

func main() {
	const (
		numCalls               = 3000
		callsPerInterval int64 = 500
		callInterval           = time.Second
	)
	
	r := ratelimit.NewRateLimiter(callsPerInterval, callInterval)
	
	for i := 0; i < numCalls; i++ {
		// wrap the desired function in a zero-param function which only returns an error
		f := func() error {
			return fnToRateLimit()
		}

		 // one might want to actually check return errors here. 
		_ = r.RateLimit(f) 
	}
}

Documentation

Index

Constants

View Source
const DefaultBucketTimeout = time.Minute

DefaultBucketTimeout is the default amount of time that a call to RateLimiter.RateLimit will wait to acquire some bucket space before timing out and returning a context.DeadlineExceeded error.

Note that this does *not* apply to RateLimiter.RateLimitContext when called with a programmer-created context.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimiter

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

RateLimiter is a fancy little struct which can do everything you want it to and more in the realm of rate limiting.

func NewRateLimiter

func NewRateLimiter(callsPerInterval int64, interval time.Duration) *RateLimiter

NewRateLimiter returns a RateLimiter configured with the number of allowed function calls per interval, as well as the interval.

Examples: - api A allows 250 calls per second. Call NewRateLimiter(250, time.Second). - api B allowed 50 calls per 5 minutes. Call NewRateLimiter(50, 5*time.Minute).

func (RateLimiter) BucketTimeout

func (r RateLimiter) BucketTimeout() time.Duration

BucketTimeout returns the current configured timeout duration used for non-contexed calls to RateLimit, after which deadline the context will return a context.DeadlineExceeded error.

By default, this is set to DefaultBucketTimeout (1 minute).

func (RateLimiter) CallsPerInterval

func (r RateLimiter) CallsPerInterval() int64

CallsPerInterval returns the current number of calls allowed to be made during a RateLimiter's interval.

func (RateLimiter) Interval

func (r RateLimiter) Interval() time.Duration

Interval returns the current interval for a RateLimiter.

func (*RateLimiter) RateLimit

func (r *RateLimiter) RateLimit(fn RateLimiterFn) error

RateLimit calls RateLimitContext using a "default" context, which is a context.Context using the RateLimiter's current bucket timeout as the time.Duration passed to context.WithTimeout (by default, this is set to DefaultBucketTimeout).

Param fn: function with no parameters which returns an error (see RateLimiterFn). This is usually wrapped around the function call one is actually making.

Return: error, if one occurs either from calling fn() or from the context. Otherwise, any error returned from calling fn() is returned.

func (*RateLimiter) RateLimitContext

func (r *RateLimiter) RateLimitContext(ctx context.Context, fn RateLimiterFn) error

RateLimitContext calls the passed function fn using the passed context.Context. If the passed context's deadline is exceeded while waiting to acquire bucket space, a wrapped error is returned. Otherwise, the result of calling fn() is returned.

func (*RateLimiter) SetBucketTimeout

func (r *RateLimiter) SetBucketTimeout(timeout time.Duration) *RateLimiter

SetBucketTimeout sets the timeout duration used for non-contexed calls to RateLimit, after which deadline the context will return a context.DeadlineExceeded error. Note that this function will block the RateLimiter's other functions until it finishes.

func (*RateLimiter) SetCallsPerInterval

func (r *RateLimiter) SetCallsPerInterval(n int64) *RateLimiter

SetCallsPerInterval sets the number of calls allowed to be made during the configured interval. Note that this function will block the RateLimiter's other functions until it finishes.

func (*RateLimiter) SetInterval

func (r *RateLimiter) SetInterval(interval time.Duration) *RateLimiter

SetInterval sets the RateLimiter's interval within which the RateLimiter's callsPerInterval number of calls are allowed to be made. Note that this function will block the RateLimiter's other functions until it finishes.

func (*RateLimiter) Wrap

func (r *RateLimiter) Wrap(fn RateLimiterFn) WrappedFn

Wrap wraps the passed function within another function, which no parameters and calls RateLimit using fn(). Helpful for building functions which are "preloaded" with a rate limiter, rather than calling RateLimit around every function call.

func (*RateLimiter) WrapContext

func (r *RateLimiter) WrapContext(fn RateLimiterFn) WrappedFnContext

WrapContext wraps the passed function within another function, which takes a single context.Context parameter and calls RateLimitContext using fn() and that context. Helpful for building functions which are "preloaded" with a rate limiter, rather than calling RateLimitContext around every function call.

type RateLimiterFn

type RateLimiterFn func() error

RateLimiterFn is a function which takes no parameters, and returns an error. It represents the function type passed to RateLimiter.RateLimit and RateLimiter.RateLimitContext.

type WrappedFn

type WrappedFn func() error

WrappedFn represents a function wrapped by a call to RateLimiter.RateLimit.

type WrappedFnContext

type WrappedFnContext func(context.Context) error

WrappedFnContext represents a function wrapped by a call to RateLimiter.RateLimitContext.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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