-
Notifications
You must be signed in to change notification settings - Fork 338
/
ioutil.go
81 lines (71 loc) · 1.91 KB
/
ioutil.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ioutil
import (
"context"
"io"
"time"
)
// timeoutReader monitors the progress of the io.Reader Read method
// and calls the cancel function when reading does not progress for
// the specified time.
type timeoutReader struct {
r io.Reader
n chan int
}
// Read implements the io.Reader Read interface.
func (pr *timeoutReader) Read(p []byte) (int, error) {
n, err := pr.r.Read(p)
select {
case pr.n <- n:
default:
}
return n, err
}
// TimeoutReader creates a new timeoutReader instance and starts
// a goroutine that monitors the progress of reading from the given reader.
// If no progress is made for the duration of the given timeout, then the
// reader executes the given cancel function and terminates the underlying
// goroutine. This goroutine will also terminate when the given context
// is canceled or if the n (returned by the given reader) is equal to 0.
func TimeoutReader(ctx context.Context, r io.Reader, timeout time.Duration, cancel func(uint64)) io.Reader {
pr := &timeoutReader{r: r, n: make(chan int)}
go func() {
defer func() {
close(pr.n)
pr.n = nil
}()
var (
total = uint64(0)
timer = time.NewTimer(timeout)
)
for {
select {
case n := <-pr.n:
switch {
case n == 0:
return
case n > 0:
total += uint64(n)
timer.Reset(timeout)
}
case <-timer.C:
cancel(total)
return
case <-ctx.Done():
return
}
}
}()
return pr
}
// The WriterFunc type is an adapter to allow the use of
// ordinary functions as io.Writer Write method. If f is
// a function with the appropriate signature, WriterFunc(f)
// is an io.Writer that calls f.
type WriterFunc func([]byte) (int, error)
// WriterFunc calls f(p).
func (f WriterFunc) Write(p []byte) (n int, err error) {
return f(p)
}