limiter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 5 Imported by: 0

README

Limiter

GoDev

The limiter package provides reader/writer capabilities with transfer speed limiting in Go.

Overview

The Limiter is designed to limit the total transfer speed of readers and writers simultaneously.

Features

  • Transfer Speed Limiting: Control the speed at which operations can be performed.
  • Dynamic Configuration: Set and update the transfer speed limit and burst size as needed.
  • Contextual Support: Perform operations with transfer speed limits within a specific context.

Usage

Import the package in your Go code:

import "github.com/sunshineplan/limiter"

Create a new Limiter by calling the New function:

limiter := limiter.New(100*1024)

The above code creates a Limiter with a total transfer speed limit of 100KB per second and a burst size of 100KB.

Transfer Speed Limiting

To perform limited operations, you can use the provided Reader and Writer methods. Here's an example of using the Reader method:

reader := limiter.Reader(yourReader)

The Reader method takes an io.Reader and returns a new reader that performs limited reads.

Similarly, you can use the Writer method to create a limited writer:

writer := limiter.Writer(yourWriter)
Updating Configuration

You can update the limit and burst size of the Limiter by calling the respective Set methods:

limiter.SetLimit(200*1024) // 200KB
limiter.SetBurst(20*1024) // 20KB

These methods allow you to dynamically adjust the limiting behavior according to your requirements.

Contributing

Contributions are welcome! If you find a bug or have a suggestion for improvement, please open an issue or submit a pull request.

License

This package is released under the MIT License.

Documentation

Index

Constants

View Source
const Inf = Limit(rate.Inf)

Inf represents an infinite rate limit; it allows all transfers, even if the burst is zero.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit rate.Limit

Limit defines the maximum transfer speed of data. The Limit is represented as a rate limit per second. A zero Limit means no transfers are allowed.

func Every

func Every(interval time.Duration) Limit

Every converts the minimum time interval between transfers to a Limit.

type Limiter

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

A Limiter controls the speed at which transfers are allowed to happen.

The zero value is a valid Limiter, but it will reject all transfers. Use New to create non-zero Limiters.

func New

func New(limit Limit) *Limiter

New creates a new Limiter with the given transfer speed limit (bytes/sec).

func (*Limiter) Burst

func (lim *Limiter) Burst() int

Burst returns the current burst size.

func (*Limiter) Limit

func (lim *Limiter) Limit() Limit

Limit returns the current limit.

func (*Limiter) Reader

func (lim *Limiter) Reader(r io.Reader) io.Reader

Reader returns a reader with limiting.

func (*Limiter) ReaderWithContext

func (lim *Limiter) ReaderWithContext(ctx context.Context, r io.Reader) io.Reader

ReaderWithContext returns a reader with limiting and context.

func (*Limiter) SetBurst

func (lim *Limiter) SetBurst(newBurst int)

SetBurst sets a new burst size.

func (*Limiter) SetLimit

func (lim *Limiter) SetLimit(newLimit Limit)

SetLimit sets a new limit.

func (*Limiter) Writer

func (lim *Limiter) Writer(w io.Writer) io.Writer

Writer returns a writer with limiting.

func (*Limiter) WriterWithContext

func (lim *Limiter) WriterWithContext(ctx context.Context, w io.Writer) io.Writer

WriterWithContext returns a writer with limiting and context.

Jump to

Keyboard shortcuts

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