GithubHelp home page GithubHelp logo

policed's Introduction

Golang rate limiter for any TCP server

This tool can be used for wrapping any tcp server listenet Accept()`s for rate limiting purposes.

Internally rate.Limiter is used (generally, simple token buffer algorithm).

Burst factor

Policer burst-size limit in this implementation is "the amount of time to allow a burst of traffic at the full line rate of a policed interface should not be lower than 5 ms." (c) Juniper Networks

In other words, if rate limit is set to 10kBps (10000 bps) and burst factor is 0.005 (default), actual burst size will be 50 bytes (10000 * 0.005)

Usually you don't want to change it, but here is it's principe: the lower the burst factor is, the more accurate is rate limiting, but the more resources it consumes. And vice versa.

If your desired rate limits are low enough, like 128kB-1mB per second, default burst is OK for you.

But if your rates are something like 10mB/s and higher, you may want to increase burst, maybe, to 0.05

More about burst

usage

Initialization

import "github.com/ircop/policed"

// ...

// Create and initialize new policier instance. 
// First argument is global rate limit, second is per-connection limit (in kB per second)
policier := policed.NewPolicier(10240, 1024)

// Wrap Accept() call with policier.
// Policier returns WrappedConn instance, which implements net.Conn interface
conn, err := policier.Wrap(listener.Accept())

// ... do stuff ... 


// Change global rate limit:
policier.SetGlobalRate(8192)

// Change per-connection limit:
policier.SetConnRate(512)

// change individual connection limit
conn.SetRate(rate * uint64(counter))

Changing parameters on the fly

// change global speed (kB/s).
// all currently established connections will also be affected.
policier.SetGlobalRate(10240)


// change per-connection speed limit (kB/s).
// all currently established connections will also be affected.
policier.SetConnRate(1024)


// Change burst factor
policier.SetBurstFactor(0.05)

// set individual rate limit for connection (kB/s)
conn.SetRate(512)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.