2016-10-31 15:50:02 +00:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An io.Reader which resets a watchdog timer whenever data is read
|
|
|
|
type watchdogReader struct {
|
2017-01-03 17:39:56 +00:00
|
|
|
timeout time.Duration
|
|
|
|
reader io.Reader
|
|
|
|
timer *time.Timer
|
2016-10-31 15:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new reader which will kick the watchdog timer whenever data is read
|
|
|
|
func newWatchdogReader(reader io.Reader, timeout time.Duration, timer *time.Timer) *watchdogReader {
|
|
|
|
return &watchdogReader{
|
|
|
|
timeout: timeout,
|
|
|
|
reader: reader,
|
|
|
|
timer: timer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads up to len(p) bytes into p
|
|
|
|
func (t *watchdogReader) Read(p []byte) (n int, err error) {
|
|
|
|
// FIXME limit the amount of data read in one chunk so as to not exceed the timeout?
|
|
|
|
resetTimer(t.timer, t.timeout)
|
2017-01-03 17:39:56 +00:00
|
|
|
n, err = t.reader.Read(p)
|
2016-10-31 15:50:02 +00:00
|
|
|
resetTimer(t.timer, t.timeout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check it satisfies the interface
|
|
|
|
var _ io.Reader = &watchdogReader{}
|