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