summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/shazow/rateio/reader.go
blob: 56bd8ffa3da5030bea5f0b70886e8259ae2f4493 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package rateio

import "io"

type reader struct {
	io.Reader
	Limiter
}

// Read reads data into p.
// Returns ErrRateExceeded error if our specified read is exceeded.
func (r *reader) Read(p []byte) (n int, err error) {
	n, err = r.Reader.Read(p)
	if err != nil {
		return
	}

	err = r.Limiter.Count(n)
	return
}

// NewReader proxies an io.Reader but keeps track of bytes read based on our Limiter.
func NewReader(r io.Reader, limiter Limiter) io.Reader {
	return &reader{r, limiter}
}