-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstallreader.go
More file actions
103 lines (89 loc) · 2.45 KB
/
stallreader.go
File metadata and controls
103 lines (89 loc) · 2.45 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package rest
import (
"context"
"errors"
"io"
"sync"
"time"
)
var ErrUploadStalled = errors.New("upload stalled: transferred less than 150KB in 30 seconds")
// stallDetectReader wraps an io.Reader and detects stalls during reading.
// It considers a stall when less than stallThreshold bytes are read in stallTimeout duration.
type stallDetectReader struct {
reader io.Reader
ctx context.Context
stallTimeout time.Duration
stallThreshold int64
lastProgress time.Time
bytesInPeriod int64
closed chan struct{}
closeOnce sync.Once
}
// newStallDetectReader creates a new reader that detects stalls.
// Default: stall if less than 150KB transferred in 30 seconds.
func newStallDetectReader(ctx context.Context, r io.Reader) *stallDetectReader {
return &stallDetectReader{
reader: r,
ctx: ctx,
stallTimeout: 30 * time.Second,
stallThreshold: 150 * 1024, // 150KB
lastProgress: time.Now(),
closed: make(chan struct{}),
}
}
func (sr *stallDetectReader) Read(p []byte) (n int, err error) {
// Set up a timer for read timeout
timer := time.NewTimer(sr.stallTimeout)
defer timer.Stop()
type result struct {
n int
err error
}
readCh := make(chan result, 1)
// Perform read in goroutine
go func() {
n, err := sr.reader.Read(p)
readCh <- result{n, err}
}()
select {
case <-sr.closed:
return 0, io.ErrClosedPipe
case <-sr.ctx.Done():
return 0, sr.ctx.Err()
case <-timer.C:
// Check if we've made enough progress in the period
if sr.bytesInPeriod < sr.stallThreshold {
return 0, ErrUploadStalled
}
// Reset counter for next period
sr.bytesInPeriod = 0
sr.lastProgress = time.Now()
// Continue waiting for read to complete
res := <-readCh
sr.bytesInPeriod += int64(res.n)
return res.n, res.err
case res := <-readCh:
sr.bytesInPeriod += int64(res.n)
// Check if it's been too long since last progress check
if time.Since(sr.lastProgress) > sr.stallTimeout {
if sr.bytesInPeriod < sr.stallThreshold {
return res.n, ErrUploadStalled
}
// Reset for next period
sr.bytesInPeriod = 0
sr.lastProgress = time.Now()
}
return res.n, res.err
}
}
// Close implements io.Closer to allow clean shutdown
func (sr *stallDetectReader) Close() error {
sr.closeOnce.Do(func() {
close(sr.closed)
})
// If the underlying reader implements io.Closer, close it too
if closer, ok := sr.reader.(io.Closer); ok {
return closer.Close()
}
return nil
}