File: watchdog_reader.go

package info (click to toggle)
golang-github-ncw-swift 0.0~git20160617.0.b964f2c-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 276 kB
  • ctags: 436
  • sloc: makefile: 4
file content (34 lines) | stat: -rw-r--r-- 835 bytes parent folder | download | duplicates (2)
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
26
27
28
29
30
31
32
33
34
package swift

import (
	"io"
	"time"
)

// An io.Reader which resets a watchdog timer whenever data is read
type watchdogReader struct {
	timeout time.Duration
	reader  io.Reader
	timer   *time.Timer
}

// 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)
	n, err = t.reader.Read(p)
	resetTimer(t.timer, t.timeout)
	return
}

// Check it satisfies the interface
var _ io.Reader = &watchdogReader{}