]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package request |
2 | ||
3 | import ( | |
4 | "io" | |
5 | "sync" | |
6 | ) | |
7 | ||
8 | // offsetReader is a thread-safe io.ReadCloser to prevent racing | |
9 | // with retrying requests | |
10 | type offsetReader struct { | |
11 | buf io.ReadSeeker | |
12 | lock sync.Mutex | |
13 | closed bool | |
14 | } | |
15 | ||
16 | func newOffsetReader(buf io.ReadSeeker, offset int64) *offsetReader { | |
17 | reader := &offsetReader{} | |
18 | buf.Seek(offset, 0) | |
19 | ||
20 | reader.buf = buf | |
21 | return reader | |
22 | } | |
23 | ||
24 | // Close will close the instance of the offset reader's access to | |
25 | // the underlying io.ReadSeeker. | |
26 | func (o *offsetReader) Close() error { | |
27 | o.lock.Lock() | |
28 | defer o.lock.Unlock() | |
29 | o.closed = true | |
30 | return nil | |
31 | } | |
32 | ||
33 | // Read is a thread-safe read of the underlying io.ReadSeeker | |
34 | func (o *offsetReader) Read(p []byte) (int, error) { | |
35 | o.lock.Lock() | |
36 | defer o.lock.Unlock() | |
37 | ||
38 | if o.closed { | |
39 | return 0, io.EOF | |
40 | } | |
41 | ||
42 | return o.buf.Read(p) | |
43 | } | |
44 | ||
45 | // Seek is a thread-safe seeking operation. | |
46 | func (o *offsetReader) Seek(offset int64, whence int) (int64, error) { | |
47 | o.lock.Lock() | |
48 | defer o.lock.Unlock() | |
49 | ||
50 | return o.buf.Seek(offset, whence) | |
51 | } | |
52 | ||
53 | // CloseAndCopy will return a new offsetReader with a copy of the old buffer | |
54 | // and close the old buffer. | |
55 | func (o *offsetReader) CloseAndCopy(offset int64) *offsetReader { | |
56 | o.Close() | |
57 | return newOffsetReader(o.buf, offset) | |
58 | } |