All Projects → fujiwara → Shapeio

fujiwara / Shapeio

Licence: mit
Traffic shaper for Golang io.Reader and io.Writer

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

shapeio

Traffic shaper for Golang io.Reader and io.Writer

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].