Generalize consumeSlow and add stop support

Signed-off-by: Tõnis Tiigi <tonistiigi@gmail.com> (github: tonistiigi)
Upstream-commit: 417e48e4a00c891e8fe5614ac6a1ef12de951f72
Component: engine
This commit is contained in:
Tonis Tiigi
2014-10-30 21:10:38 +02:00
parent 55acba12a1
commit 4b66ac1426
2 changed files with 17 additions and 11 deletions

View File

@ -254,18 +254,25 @@ func makeRandomString(n int) string {
return string(b)
}
func consumeSlow(reader io.Reader, chunkSize int, interval time.Duration) (n int, err error) {
// Reads chunkSize bytes from reader after every interval.
// Returns total read bytes.
func consumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
buffer := make([]byte, chunkSize)
for {
var readBytes int
readBytes, err = reader.Read(buffer)
n += readBytes
if err != nil {
if err == io.EOF {
err = nil
}
select {
case <-stop:
return
default:
var readBytes int
readBytes, err = reader.Read(buffer)
n += readBytes
if err != nil {
if err == io.EOF {
err = nil
}
return
}
time.Sleep(interval)
}
time.Sleep(interval)
}
}