File: watchdog_reader.go

package info (click to toggle)
golang-github-ncw-swift 1.0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 412 kB
  • sloc: python: 29; sh: 18; makefile: 4
file content (55 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download | duplicates (5)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package swift

import (
	"io"
	"time"
)

var watchdogChunkSize = 1 << 20 // 1 MiB

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

// 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,
		chunkSize: watchdogChunkSize,
	}
}

// Read reads up to len(p) bytes into p
func (t *watchdogReader) Read(p []byte) (int, error) {
	//read from underlying reader in chunks not larger than t.chunkSize
	//while resetting the watchdog timer before every read; the small chunk
	//size ensures that the timer does not fire when reading a large amount of
	//data from a slow connection
	start := 0
	end := len(p)
	for start < end {
		length := end - start
		if length > t.chunkSize {
			length = t.chunkSize
		}

		resetTimer(t.timer, t.timeout)
		n, err := t.reader.Read(p[start : start+length])
		start += n
		if n == 0 || err != nil {
			return start, err
		}
	}

	resetTimer(t.timer, t.timeout)
	return start, nil
}

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