-
Notifications
You must be signed in to change notification settings - Fork 290
/
reader.go
46 lines (39 loc) · 923 Bytes
/
reader.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
package runtimelog
import (
"context"
"io"
"sync"
"time"
)
// A reader that will stop returning data after its context has been canceled.
//
// If any data is read from the underlying stream after the cancel happens, throw the data out.
type HardCancelReader struct {
ctx context.Context
reader io.Reader
Now func() time.Time
mu sync.Mutex
lastRead time.Time
}
func NewHardCancelReader(ctx context.Context, reader io.Reader) *HardCancelReader {
return &HardCancelReader{ctx: ctx, reader: reader, Now: time.Now}
}
func (r *HardCancelReader) LastReadTime() time.Time {
r.mu.Lock()
defer r.mu.Unlock()
return r.lastRead
}
func (r *HardCancelReader) Read(b []byte) (int, error) {
err := r.ctx.Err()
if err != nil {
return 0, err
}
n, err := r.reader.Read(b)
if r.ctx.Err() != nil {
return 0, r.ctx.Err()
}
r.mu.Lock()
defer r.mu.Unlock()
r.lastRead = r.Now()
return n, err
}