https://github.com/fujiwara/shapeio
Traffic shaper for Golang io.Reader and io.Writer
https://github.com/fujiwara/shapeio
go golang traffic-shaping
Last synced: about 1 year ago
JSON representation
Traffic shaper for Golang io.Reader and io.Writer
- Host: GitHub
- URL: https://github.com/fujiwara/shapeio
- Owner: fujiwara
- License: mit
- Created: 2016-04-01T08:51:01.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-02-18T17:09:50.000Z (over 4 years ago)
- Last Synced: 2024-10-11T21:12:42.479Z (over 1 year ago)
- Topics: go, golang, traffic-shaping
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 126
- Watchers: 3
- Forks: 18
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# shapeio
Traffic shaper for Golang io.Reader and io.Writer
```go
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
```go
type Reader struct {
}
```
#### func NewReader
```go
func NewReader(r io.Reader) *Reader
```
NewReader returns a reader that implements io.Reader with rate limiting.
#### func (*Reader) Read
```go
func (s *Reader) Read(p []byte) (int, error)
```
Read reads bytes into p.
#### func (*Reader) SetRateLimit
```go
func (s *Reader) SetRateLimit(l float64)
```
SetRateLimit sets rate limit (bytes/sec) to the reader.
#### type Writer
```go
type Writer struct {
}
```
#### func NewWriter
```go
func NewWriter(w io.Writer) *Writer
```
NewWriter returns a writer that implements io.Writer with rate limiting.
#### func (*Writer) SetRateLimit
```go
func (s *Writer) SetRateLimit(l float64)
```
SetRateLimit sets rate limit (bytes/sec) to the writer.
#### func (*Writer) Write
```go
func (s *Writer) Write(p []byte) (int, error)
```
Write writes bytes from p.
## License
The MIT License (MIT)
Copyright (c) 2016 FUJIWARA Shunichiro