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()
42 type AsyncStream struct {
44 requests chan sliceRequest
46 subtract_reader chan bool
47 wait_zero_readers chan bool
50 // Reads from the buffer managed by the Transfer()
51 type StreamReader struct {
54 responses chan sliceResult
57 func AsyncStreamFromReader(buffersize int, source io.Reader) *AsyncStream {
58 t := &AsyncStream{make([]byte, buffersize), make(chan sliceRequest), make(chan bool), make(chan bool), make(chan bool)}
66 func AsyncStreamFromSlice(buf []byte) *AsyncStream {
67 t := &AsyncStream{buf, make(chan sliceRequest), make(chan bool), make(chan bool), make(chan bool)}
75 func (this *AsyncStream) MakeStreamReader() *StreamReader {
76 this.add_reader <- true
77 return &StreamReader{0, this, make(chan sliceResult)}
80 // Reads from the buffer managed by the Transfer()
81 func (this *StreamReader) Read(p []byte) (n int, err error) {
82 this.stream.requests <- sliceRequest{this.offset, len(p), this.responses}
83 rr, valid := <-this.responses
85 this.offset += len(rr.slice)
86 return copy(p, rr.slice), rr.err
88 return 0, io.ErrUnexpectedEOF
92 func (this *StreamReader) WriteTo(dest io.Writer) (written int64, err error) {
93 // Record starting offset in order to correctly report the number of bytes sent
94 starting_offset := this.offset
96 this.stream.requests <- sliceRequest{this.offset, 32 * 1024, this.responses}
97 rr, valid := <-this.responses
99 this.offset += len(rr.slice)
101 if rr.err == io.EOF {
102 // EOF is not an error.
103 return int64(this.offset - starting_offset), nil
105 return int64(this.offset - starting_offset), rr.err
111 return int64(this.offset), io.ErrUnexpectedEOF
116 // Close the responses channel
117 func (this *StreamReader) Close() error {
118 this.stream.subtract_reader <- true
119 close(this.responses)
124 func (this *AsyncStream) Close() {
125 this.wait_zero_readers <- true
127 close(this.add_reader)
128 close(this.subtract_reader)
129 close(this.wait_zero_readers)