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 LimitConfigFn ¶
type LimitConfigFn func(*LimitConfig)
func WithContext ¶
func WithContext(ctx context.Context) LimitConfigFn
func WithRateLimit ¶
func WithRateLimit(rateLimit float64) LimitConfigFn
type RateLimiter ¶
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 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:
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:
Click to show internal directories.
Click to hide internal directories.