iocap

package module
v0.0.0-...-dd64b79 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2016 License: MIT Imports: 3 Imported by: 2

README

Build Status GoDoc

iocap

Go package for rate limiting data streams using the familiar io.Reader and io.Writer interfaces.

iocap provides simple wrappers over arbitrary io.Reader and io.Writer instances to allow throttling throughput of either read or write operations. Data streams can be rate limited individually or grouped together to provide an aggregate rate over multiple operations.

For examples and usage, see the godoc.

How it works

Under the hood, iocap uses a very simple leaky bucket implementation to shape the flow of traffic. This implementation uses timestamps instead of a constant-rate "leak" to empty the bucket. The reason for this is to allow readers and writers to utilize the leaky bucket without requiring additional setup, including starting/stopping of timers or other goroutines.

Documentation

Overview

Package iocap provides rate limiting for data streams using the familiar io.Reader and io.Writer interfaces.

Rates can be expressed in a few different units using helper functions:

rate := iocap.Kbps(512) // Kilobits/s
rate := iocap.Mbps(10)  // Megabits/s
rate := iocap.Gbps(1)   // Gigabits/s

Rates can also be described manually with any size or interval:

rate := iocap.RateOpts{
	Interval: time.Second,
	Size:     5 * 1024 * 1024, // 5MB/s
}

Readers and Writers are created by passing in an existing io.Reader or io.Writer along with a rate.

r = iocap.NewReader(r, rate)
w = iocap.NewWriter(w, rate)

Rate limits can be applied to multiple readers and/or writers by creating a rate limiting group for them.

// Create the shared group
g := iocap.NewGroup(rate)

// Pull a new reader and writer off of the group. Rate limits are now
// applied and enforced across both.
r = g.NewReader(r)
w = g.NewWriter(w)

Index

Examples

Constants

View Source
const (
	Kb // Kilobit
	Mb // Megabit
	Gb // Gigabit
)

Variables

View Source
var (
	// The zero-value of RateOpts is used to indicate that no rate limit
	// should be applied to read/write operations.
	Unlimited = RateOpts{0, 0}
)

Functions

This section is empty.

Types

type Group

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

Group is used to group multiple readers and/or writers onto the same bucket, thus enforcing the rate limit across multiple independent processes.

Example
// Create a rate limiting group.
rate := Kbps(512)
g := NewGroup(rate)

// Create a new reader and writer on the group.
buf := new(bytes.Buffer)
r := g.NewReader(buf)
w := g.NewWriter(buf)

// Reader and writer are rate limited together
n, err := w.Write([]byte("hello world!"))
if err != nil {
	fmt.Println(err)
	return
}
out := make([]byte, n)
_, err = r.Read(out)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(out))
Output:

hello world!

func NewGroup

func NewGroup(opts RateOpts) *Group

NewGroup creates a new rate limiting group with the specific rate.

func (*Group) NewReader

func (g *Group) NewReader(src io.Reader) *Reader

NewReader creates and returns a new reader in the group.

func (*Group) NewWriter

func (g *Group) NewWriter(dst io.Writer) *Writer

NewWriter creates and returns a new writer in the group.

func (*Group) SetRate

func (g *Group) SetRate(opts RateOpts)

SetRate is used to dynamically update the rate options of the group.

type RateOpts

type RateOpts struct {
	// Interval is the time period of the rate
	Interval time.Duration

	// Size is the number of bytes per interval
	Size int
}

RateOpts is used to encapsulate rate limiting options.

func Gbps

func Gbps(n float64) RateOpts

Gbps returns a RateOpts configured for n gigabits per second.

func Kbps

func Kbps(n float64) RateOpts

Kbps returns a RateOpts configured for n kilobits per second.

func Mbps

func Mbps(n float64) RateOpts

Mbps returns a RateOpts configured for n megabits per second.

type Reader

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

Reader implements the io.Reader interface and limits the rate at which bytes come off of the underlying source reader.

Example
// Create a buffer to read from.
buf := bytes.NewBufferString("hello world!")

// Create the rate limited reader.
rate := Kbps(512)
r := NewReader(buf, rate)

// Read from the reader.
out := make([]byte, buf.Len())
n, err := r.Read(out)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(n, string(out))
Output:

12 hello world!

func NewReader

func NewReader(src io.Reader, opts RateOpts) *Reader

NewReader wraps src in a new rate limited reader.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads bytes off of the underlying source reader onto p with rate limiting. Reads until EOF or until p is filled.

func (*Reader) SetRate

func (r *Reader) SetRate(opts RateOpts)

SetRate is used to dynamically set the rate options on the reader.

type Writer

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

Writer implements the io.Writer interface and limits the rate at which bytes are written to the underlying writer.

Example
// Create the buffer to write to.
buf := new(bytes.Buffer)

// Create the rate limited writer.
rate := Kbps(512)
r := NewWriter(buf, rate)

// Write data into the writer.
n, err := r.Write([]byte("hello world!"))
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(n, buf.String())
Output:

12 hello world!

func NewWriter

func NewWriter(dst io.Writer, opts RateOpts) *Writer

NewWriter wraps dst in a new rate limited writer.

func (*Writer) SetRate

func (w *Writer) SetRate(opts RateOpts)

SetRate is used to dynamically set the rate options on the writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write writes len(p) bytes onto the underlying io.Writer, respecting the configured rate limit options.

Directories

Path Synopsis
Package httpcap provides rate limiting for HTTP handlers and response writers.
Package httpcap provides rate limiting for HTTP handlers and response writers.
mapper
Package mapper provides HTTP request mapping based on arbitrary criteria, including the raw HTTP request details.
Package mapper provides HTTP request mapping based on arbitrary criteria, including the raw HTTP request details.

Jump to

Keyboard shortcuts

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