2 // Written by Maxim Khitrov (November 2012)
12 // ErrLimit is returned by the Writer when a non-blocking write is short due to
13 // the transfer rate limit.
14 var ErrLimit = errors.New("flowrate: flow rate limit exceeded")
16 // Limiter is implemented by the Reader and Writer to provide a consistent
17 // interface for monitoring and controlling data transfer.
18 type Limiter interface {
21 SetTransferSize(bytes int64)
22 SetLimit(new int64) (old int64)
23 SetBlocking(new bool) (old bool)
26 // Reader implements io.ReadCloser with a restriction on the rate of data
29 io.Reader // Data source
30 *Monitor // Flow control monitor
32 limit int64 // Rate limit in bytes per second (unlimited when <= 0)
33 block bool // What to do when no new bytes can be read due to the limit
36 // NewReader restricts all Read operations on r to limit bytes per second.
37 func NewReader(r io.Reader, limit int64) *Reader {
38 return &Reader{r, New(0, 0), limit, true}
41 // Read reads up to len(p) bytes into p without exceeding the current transfer
42 // rate limit. It returns (0, nil) immediately if r is non-blocking and no new
43 // bytes can be read at this time.
44 func (r *Reader) Read(p []byte) (n int, err error) {
45 p = p[:r.Limit(len(p), r.limit, r.block)]
47 n, err = r.IO(r.Reader.Read(p))
52 // SetLimit changes the transfer rate limit to new bytes per second and returns
53 // the previous setting.
54 func (r *Reader) SetLimit(new int64) (old int64) {
55 old, r.limit = r.limit, new
59 // SetBlocking changes the blocking behavior and returns the previous setting. A
60 // Read call on a non-blocking reader returns immediately if no additional bytes
61 // may be read at this time due to the rate limit.
62 func (r *Reader) SetBlocking(new bool) (old bool) {
63 old, r.block = r.block, new
67 // Close closes the underlying reader if it implements the io.Closer interface.
68 func (r *Reader) Close() error {
70 if c, ok := r.Reader.(io.Closer); ok {
76 // Writer implements io.WriteCloser with a restriction on the rate of data
79 io.Writer // Data destination
80 *Monitor // Flow control monitor
82 limit int64 // Rate limit in bytes per second (unlimited when <= 0)
83 block bool // What to do when no new bytes can be written due to the limit
86 // NewWriter restricts all Write operations on w to limit bytes per second. The
87 // transfer rate and the default blocking behavior (true) can be changed
88 // directly on the returned *Writer.
89 func NewWriter(w io.Writer, limit int64) *Writer {
90 return &Writer{w, New(0, 0), limit, true}
93 // Write writes len(p) bytes from p to the underlying data stream without
94 // exceeding the current transfer rate limit. It returns (n, ErrLimit) if w is
95 // non-blocking and no additional bytes can be written at this time.
96 func (w *Writer) Write(p []byte) (n int, err error) {
98 for len(p) > 0 && err == nil {
99 s := p[:w.Limit(len(p), w.limit, w.block)]
101 c, err = w.IO(w.Writer.Write(s))
111 // SetLimit changes the transfer rate limit to new bytes per second and returns
112 // the previous setting.
113 func (w *Writer) SetLimit(new int64) (old int64) {
114 old, w.limit = w.limit, new
118 // SetBlocking changes the blocking behavior and returns the previous setting. A
119 // Write call on a non-blocking writer returns as soon as no additional bytes
120 // may be written at this time due to the rate limit.
121 func (w *Writer) SetBlocking(new bool) (old bool) {
122 old, w.block = w.block, new
126 // Close closes the underlying writer if it implements the io.Closer interface.
127 func (w *Writer) Close() error {
129 if c, ok := w.Writer.(io.Closer); ok {