Last change
on this file since 145 was 145, checked in by Izuru Yakumo, 22 months ago |
Updated the Makefile and vendored depedencies
Signed-off-by: Izuru Yakumo <yakumo.izuru@…>
|
File size:
1.1 KB
|
Line | |
---|
1 | package fasthttp
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "bufio"
|
---|
5 | "io"
|
---|
6 | "sync"
|
---|
7 |
|
---|
8 | "github.com/valyala/fasthttp/fasthttputil"
|
---|
9 | )
|
---|
10 |
|
---|
11 | // StreamWriter must write data to w.
|
---|
12 | //
|
---|
13 | // Usually StreamWriter writes data to w in a loop (aka 'data streaming').
|
---|
14 | //
|
---|
15 | // StreamWriter must return immediately if w returns error.
|
---|
16 | //
|
---|
17 | // Since the written data is buffered, do not forget calling w.Flush
|
---|
18 | // when the data must be propagated to reader.
|
---|
19 | type StreamWriter func(w *bufio.Writer)
|
---|
20 |
|
---|
21 | // NewStreamReader returns a reader, which replays all the data generated by sw.
|
---|
22 | //
|
---|
23 | // The returned reader may be passed to Response.SetBodyStream.
|
---|
24 | //
|
---|
25 | // Close must be called on the returned reader after all the required data
|
---|
26 | // has been read. Otherwise goroutine leak may occur.
|
---|
27 | //
|
---|
28 | // See also Response.SetBodyStreamWriter.
|
---|
29 | func NewStreamReader(sw StreamWriter) io.ReadCloser {
|
---|
30 | pc := fasthttputil.NewPipeConns()
|
---|
31 | pw := pc.Conn1()
|
---|
32 | pr := pc.Conn2()
|
---|
33 |
|
---|
34 | var bw *bufio.Writer
|
---|
35 | v := streamWriterBufPool.Get()
|
---|
36 | if v == nil {
|
---|
37 | bw = bufio.NewWriter(pw)
|
---|
38 | } else {
|
---|
39 | bw = v.(*bufio.Writer)
|
---|
40 | bw.Reset(pw)
|
---|
41 | }
|
---|
42 |
|
---|
43 | go func() {
|
---|
44 | sw(bw)
|
---|
45 | bw.Flush()
|
---|
46 | pw.Close()
|
---|
47 |
|
---|
48 | streamWriterBufPool.Put(bw)
|
---|
49 | }()
|
---|
50 |
|
---|
51 | return pr
|
---|
52 | }
|
---|
53 |
|
---|
54 | var streamWriterBufPool sync.Pool
|
---|
Note:
See
TracBrowser
for help on using the repository browser.