Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Version = "v1.1.0"
Functions ¶
This section is empty.
Types ¶
type ReadCloser ¶ added in v1.1.0
type ReadCloser struct {
*Reader
// contains filtered or unexported fields
}
ReadCloser is a rate-limited io.ReadCloser. It embeds *Reader so all rate-limit methods (SetRateLimit, SetRateLimitEvery, ...) are available directly, and Close delegates to the wrapped io.ReadCloser.
func NewReadCloser ¶ added in v1.1.0
func NewReadCloser(rc io.ReadCloser) *ReadCloser
NewReadCloser returns a ReadCloser that rate-limits reads from rc and closes rc on Close. SetRateLimit must still be called to set the rate.
func NewReadCloserWithContext ¶ added in v1.1.0
func NewReadCloserWithContext(rc io.ReadCloser, ctx context.Context) *ReadCloser
NewReadCloserWithContext is NewReadCloser with a context for WaitN.
func (*ReadCloser) Close ¶ added in v1.1.0
func (rc *ReadCloser) Close() error
Close closes the wrapped io.ReadCloser.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Example ¶
package main
import (
"io"
"io/ioutil"
"net/http"
"github.com/fujiwara/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 NewReaderWithContext ¶
NewReaderWithContext returns a reader that implements io.Reader with rate limiting.
func (*Reader) SetRateLimit ¶
SetRateLimit sets rate limit (bytes/sec) to the reader.
SetRateLimit may be called more than once and concurrently with Read to change the rate dynamically. On the first call, a new rate limiter is created and the initial burst is consumed. Subsequent calls update the existing limiter's rate in place. Note that a rate change does not affect a Wait that has already started inside an in-flight Read call; the new rate takes effect from the next call.
func (*Reader) SetRateLimitEvery ¶ added in v1.1.0
SetRateLimitEvery sets rate limit as bytes per the given duration.
It is equivalent to SetRateLimit(float64(bytes) / per.Seconds()) and is useful when the rate is more naturally expressed as "N bytes every D" (e.g. SetRateLimitEvery(60, time.Minute)). The same concurrency and in-flight-Wait semantics as SetRateLimit apply.
Inputs are not validated; the resulting rate follows golang.org/x/time/rate semantics (a non-positive per yields an infinite, i.e. unlimited, rate; a negative bytes yields a negative rate).
type WriteCloser ¶ added in v1.1.0
type WriteCloser struct {
*Writer
// contains filtered or unexported fields
}
WriteCloser is a rate-limited io.WriteCloser. It embeds *Writer so all rate-limit methods are available directly, and Close delegates to the wrapped io.WriteCloser.
func NewWriteCloser ¶ added in v1.1.0
func NewWriteCloser(wc io.WriteCloser) *WriteCloser
NewWriteCloser returns a WriteCloser that rate-limits writes to wc and closes wc on Close. SetRateLimit must still be called to set the rate.
func NewWriteCloserWithContext ¶ added in v1.1.0
func NewWriteCloserWithContext(wc io.WriteCloser, ctx context.Context) *WriteCloser
NewWriteCloserWithContext is NewWriteCloser with a context for WaitN.
func (*WriteCloser) Close ¶ added in v1.1.0
func (wc *WriteCloser) Close() error
Close closes the wrapped io.WriteCloser.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Example ¶
package main
import (
"bytes"
"io"
"os"
"github.com/fujiwara/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 NewWriterWithContext ¶
NewWriterWithContext returns a writer that implements io.Writer with rate limiting.
func (*Writer) SetRateLimit ¶
SetRateLimit sets rate limit (bytes/sec) to the writer.
SetRateLimit may be called more than once and concurrently with Write to change the rate dynamically. On the first call, a new rate limiter is created and the initial burst is consumed. Subsequent calls update the existing limiter's rate in place. Note that a rate change does not affect a Wait that has already started inside an in-flight Write call; the new rate takes effect from the next call.
func (*Writer) SetRateLimitEvery ¶ added in v1.1.0
SetRateLimitEvery sets rate limit as bytes per the given duration.
It is equivalent to SetRateLimit(float64(bytes) / per.Seconds()) and is useful when the rate is more naturally expressed as "N bytes every D" (e.g. SetRateLimitEvery(60, time.Minute)). The same concurrency and in-flight-Wait semantics as SetRateLimit apply.
Inputs are not validated; the resulting rate follows golang.org/x/time/rate semantics (a non-positive per yields an infinite, i.e. unlimited, rate; a negative bytes yields a negative rate).