tcpshaper

command module
v0.0.0-...-3735a92 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

tcpshaper

Golang Middleware for rate limiting tcp traffic

Build Status

Overview

This package provides middleware and utility functions for rate limiting net.Listener connections.

It makes use of the https://godoc.org/golang.org/x/time/rate package to provide the rate limiting itself.

Usage

Listener

package main

import (
  "context"
  "fmt"
  "github.com/gerritjvv/tcpshaper/bandwidth"
  "net"
)

func main() { 
  // Get a Listener, e.g tcp on any port
  tcpListner, err := net.Listen("tcp", ":0")
  if err != nil {
   panic(err)
  }

  ctx := context.Background()

  // Configure rate limit to:
  // total read+write traffic == 1mbs
  // Each connection read+write == 2kbs
  // The maximum that can be read or written at any one time is 2kb
  serverRate := bandwidth.NewRateConfig(1024*1024, 2048)
  connRate := bandwidth.NewRateConfig(2048, 2048)

  listener := bandwidth.NewListener(ctx, &bandwidth.ListenerConfig{
    ReadServerRate:  serverRate,
    WriteServerRate: serverRate,
    ReadConnRate:    connRate,
    WriteConnRate:   connRate,
  }, tcpListner)

  // Now use the listener
  // e.g listener.Accept()
  
  err = listener.Close()
  if err != nil {
    fmt.Printf("error while closing listener %s", err)
  }
}

Connection wrapping


package main
import (
  "context"
  "github.com/gerritjvv/tcpshaper/bandwidth"
)

func main()  {
 
  // Setup a limiter with a 2kb/s limit, and max 2kb per read event.
  readLimiter := bandwidth.NewBandwidthLimiter(bandwidth.NewRateConfig(2048, 2048))

  // Setup a "noop" limiter with infinite rates
  writeLimiter := bandwidth.NewBandwidthLimiter(bandwidth.NewRateConfig(bandwidth.Inf, 0))

  ctx := context.Background()

  conn := getConn() // get a connection

  rConn := bandwidth.NewRateLimitedConn(ctx, readLimiter, writeLimiter, conn)

  // Write is not rate limited
  _, _ = rConn.Write([]byte("test string"))

  // Read is reate limited
  _, _ = rConn.Read(make([]byte, 1))

  _ = rConn.Close()

}

Build and Test


## to build the library
./build.sh build

## run tests
./build.sh test

## show test coverage reports
./build.sh report

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package bandwidth provides rate limiting utilities and middleware for TCP connections.
Package bandwidth provides rate limiting utilities and middleware for TCP connections.

Jump to

Keyboard shortcuts

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