示例#1
0
func SplitReader(r io.ReadCloser, bufSize uint64, n uint) []io.ReadCloser {
	readers := make([]*splitReader, n)
	readClosers := make([]io.ReadCloser, n)
	for i := uint(0); i < n; i++ {
		ch := make(chan splitReaderChunk, 0)
		rc := rechunkingReader{[]byte{}, false, nil}
		r := &splitReader{rc, ch, make(chan struct{}, 1), false}
		func(r *splitReader) {
			r.fetchMore = func() (bytes []byte, err error) {
				if r.closed {
					err = io.EOF
					return
				}

				chunk, ok := <-ch
				if !ok {
					err = io.EOF
					return
				}
				bytes = chunk.buf
				err = chunk.err
				return
			}
		}(r)
		readers[i] = r
		readClosers[i] = r
	}
	go pumpSplitReader(r, bufSize, readers)
	return readClosers
}