1 /* AsyncStream pulls data in from a io.Reader source (such as a file or network
2 socket) and fans out to any number of StreamReader sinks.
4 Unlike io.TeeReader() or io.MultiWriter(), new StreamReaders can be created at
5 any point in the lifetime of the AsyncStream, and each StreamReader will read
6 the contents of the buffer up to the "frontier" of the buffer, at which point
7 the StreamReader blocks until new data is read from the source.
9 This is useful for minimizing readthrough latency as sinks can read and act on
10 data from the source without waiting for the source to be completely buffered.
11 It is also useful as a cache in situations where re-reading the original source
12 potentially is costly, since the buffer retains a copy of the source data.
16 Begin reading into a buffer with maximum size 'buffersize' from 'source':
17 stream := AsyncStreamFromReader(buffersize, source)
19 To create a new reader (this can be called multiple times, each reader starts
20 at the beginning of the buffer):
21 reader := tr.MakeStreamReader()
23 Make sure to close the reader when you're done with it.
26 When you're done with the stream:
29 Alternately, if you already have a filled buffer and just want to read out from it:
30 stream := AsyncStreamFromSlice(buf)
32 r := tr.MakeStreamReader()
43 var ErrAlreadyClosed = errors.New("cannot close a stream twice")
45 type AsyncStream struct {
47 requests chan sliceRequest
49 subtract_reader chan bool
50 wait_zero_readers chan bool
54 // Reads from the buffer managed by the Transfer()
55 type StreamReader struct {
58 responses chan sliceResult
61 func AsyncStreamFromReader(buffersize int, source io.Reader) *AsyncStream {
63 buffer: make([]byte, buffersize),
64 requests: make(chan sliceRequest),
65 add_reader: make(chan bool),
66 subtract_reader: make(chan bool),
67 wait_zero_readers: make(chan bool),
76 func AsyncStreamFromSlice(buf []byte) *AsyncStream {
79 requests: make(chan sliceRequest),
80 add_reader: make(chan bool),
81 subtract_reader: make(chan bool),
82 wait_zero_readers: make(chan bool),
91 func (this *AsyncStream) MakeStreamReader() *StreamReader {
92 this.add_reader <- true
93 return &StreamReader{0, this, make(chan sliceResult)}
96 // Reads from the buffer managed by the Transfer()
97 func (this *StreamReader) Read(p []byte) (n int, err error) {
98 this.stream.requests <- sliceRequest{this.offset, len(p), this.responses}
99 rr, valid := <-this.responses
101 this.offset += len(rr.slice)
102 return copy(p, rr.slice), rr.err
104 return 0, io.ErrUnexpectedEOF
108 func (this *StreamReader) WriteTo(dest io.Writer) (written int64, err error) {
109 // Record starting offset in order to correctly report the number of bytes sent
110 starting_offset := this.offset
112 this.stream.requests <- sliceRequest{this.offset, 32 * 1024, this.responses}
113 rr, valid := <-this.responses
115 this.offset += len(rr.slice)
117 if rr.err == io.EOF {
118 // EOF is not an error.
119 return int64(this.offset - starting_offset), nil
121 return int64(this.offset - starting_offset), rr.err
127 return int64(this.offset), io.ErrUnexpectedEOF
132 // Close the responses channel
133 func (this *StreamReader) Close() error {
134 if this.stream == nil {
135 return ErrAlreadyClosed
137 this.stream.subtract_reader <- true
138 close(this.responses)
143 func (this *AsyncStream) Close() error {
145 return ErrAlreadyClosed
148 this.wait_zero_readers <- true
150 close(this.add_reader)
151 close(this.subtract_reader)
152 close(this.wait_zero_readers)