shapeio

package
v0.0.0-...-b1debd4 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 4 Imported by: 1

README

shapeio

Traffic shaper for Golang io.Reader and io.Writer

https://github.com/fujiwara/shapeio

import "github.com/fujiwara/shapeio"

func ExampleReader() {
	// example for downloading http body with rate limit.
	resp, _ := http.Get("http://example.com")
	defer resp.Body.Close()

	reader := shapeio.NewReader(resp.Body)
	reader.SetRateLimit(1024 * 10) // 10KB/sec
	io.Copy(ioutil.Discard, reader)
}

func ExampleWriter() {
	// example for writing file with rate limit.
	src := bytes.NewReader(bytes.Repeat([]byte{0}, 32*1024)) // 32KB
	f, _ := os.Create("/tmp/foo")
	writer := shapeio.NewWriter(f)
	writer.SetRateLimit(1024 * 10) // 10KB/sec
	io.Copy(writer, src)
	f.Close()
}

Usage

type Reader
type Reader struct {
}
func NewReader
func NewReader(r io.Reader) *Reader

NewReader returns a reader that implements io.Reader with rate limiting.

func (*Reader) Read
func (s *Reader) Read(p []byte) (int, error)

Read reads bytes into p.

func (*Reader) SetRateLimit
func (s *Reader) SetRateLimit(l float64)

SetRateLimit sets rate limit (bytes/sec) to the reader.

type Writer
type Writer struct {
}
func NewWriter
func NewWriter(w io.Writer) *Writer

NewWriter returns a writer that implements io.Writer with rate limiting.

func (*Writer) SetRateLimit
func (s *Writer) SetRateLimit(l float64)

SetRateLimit sets rate limit (bytes/sec) to the writer.

func (*Writer) Write
func (s *Writer) Write(p []byte) (int, error)

Write writes bytes from p.

License

The MIT License (MIT)

Copyright (c) 2016 FUJIWARA Shunichiro

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRateLimiterSetter

func NewRateLimiterSetter(w RateLimiterSetter, fns ...LimitConfigFn)

func WrapReadCloser

func WrapReadCloser(r io.Reader) io.ReadCloser

WrapReadCloser returns a ReadCloser with Close method wrapping the provided Reader r.

func WrapWriteCloser

func WrapWriteCloser(r io.Writer) io.WriteCloser

WrapWriteCloser returns a WriteCloser with Close method wrapping the provided Writer r.

Types

type LimitConfig

type LimitConfig struct {
	context.Context
	RateLimit float64
}

type LimitConfigFn

type LimitConfigFn func(*LimitConfig)

func WithContext

func WithContext(ctx context.Context) LimitConfigFn

func WithRateLimit

func WithRateLimit(rateLimit float64) LimitConfigFn

type RateLimiter

type RateLimiter struct {
	*rate.Limiter
	context.Context
}

func NewRateLimiter

func NewRateLimiter(fns ...LimitConfigFn) *RateLimiter

func (*RateLimiter) SetContext

func (s *RateLimiter) SetContext(ctx context.Context)

func (*RateLimiter) SetRateLimit

func (s *RateLimiter) SetRateLimit(bytesPerSec float64)

SetRateLimit sets rate limit (bytes/sec) to the reader.

type RateLimiterSetter

type RateLimiterSetter interface {
	SetContext(ctx context.Context)
	SetRateLimit(bytesPerSec float64)
}

type Reader

type Reader struct {
	io.ReadCloser
	RateLimiter
}
Example
package main

import (
	"io"
	"io/ioutil"
	"net/http"

	"github.com/bingoohuang/ngg/goup/shapeio"
)

func main() {
	// example for downloading http body with rate limit.
	resp, _ := http.Get("http://example.com")
	defer resp.Body.Close()

	reader := shapeio.NewReader(resp.Body)
	reader.SetRateLimit(1024 * 10) // 10KB/sec
	io.Copy(ioutil.Discard, reader)
}
Output:

func NewReader

func NewReader(r io.Reader, fns ...LimitConfigFn) *Reader

NewReader returns a reader that implements io.Reader with rate limiting.

func (*Reader) Read

func (s *Reader) Read(p []byte) (int, error)

Read reads bytes into p.

type Writer

type Writer struct {
	io.WriteCloser
	RateLimiter
}
Example
package main

import (
	"bytes"
	"io"
	"os"

	"github.com/bingoohuang/ngg/goup/shapeio"
)

func main() {
	// example for writing file with rate limit.
	src := bytes.NewReader(bytes.Repeat([]byte{0}, 32*1024)) // 32KB
	f, _ := os.Create("/tmp/foo")
	writer := shapeio.NewWriter(f)
	writer.SetRateLimit(1024 * 10) // 10KB/sec
	io.Copy(writer, src)
	f.Close()
}
Output:

func NewWriter

func NewWriter(w io.Writer, fns ...LimitConfigFn) *Writer

NewWriter returns a writer that implements io.Writer with rate limiting.

func (*Writer) Write

func (s *Writer) Write(p []byte) (int, error)

Write writes bytes from p.

Jump to

Keyboard shortcuts

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