aboutsummaryrefslogtreecommitdiff
path: root/requestctx_setbodystreamwriter_example_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2016-04-15 18:05:57 +0300
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2016-04-15 18:05:57 +0300
commit05949704db9b49a6fc7aa30220c983cc1c5f97a6 (patch)
treede7c9841a864a104ecd54f77fb47be0e3c762521 /requestctx_setbodystreamwriter_example_test.go
parentServer optimization: reduce the number of SetReadDeadline/SetWriteDeadline ca... (diff)
downloadfasthttp-05949704db9b49a6fc7aa30220c983cc1c5f97a6.tar.gz
fasthttp-05949704db9b49a6fc7aa30220c983cc1c5f97a6.tar.bz2
fasthttp-05949704db9b49a6fc7aa30220c983cc1c5f97a6.zip
Issue #78: Added an example for RequestCtx.SetBodyStreamWriter
Diffstat (limited to 'requestctx_setbodystreamwriter_example_test.go')
-rw-r--r--requestctx_setbodystreamwriter_example_test.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/requestctx_setbodystreamwriter_example_test.go b/requestctx_setbodystreamwriter_example_test.go
new file mode 100644
index 0000000..6bdcb81
--- /dev/null
+++ b/requestctx_setbodystreamwriter_example_test.go
@@ -0,0 +1,32 @@
+package fasthttp_test
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "time"
+
+ "github.com/valyala/fasthttp"
+)
+
+func ExampleRequestCtx_SetBodyStreamWriter() {
+ // Start fasthttp server for streaming responses.
+ if err := fasthttp.ListenAndServe(":8080", responseStreamHandler); err != nil {
+ log.Fatalf("unexpected error in server: %s", err)
+ }
+}
+
+func responseStreamHandler(ctx *fasthttp.RequestCtx) {
+ // Send the response in chunks and wait for a second between each chunk.
+ ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
+ for i := 0; i < 10; i++ {
+ fmt.Fprintf(w, "this is a message number %d", i)
+
+ // Do not forget flushing streamed data to the client.
+ if err := w.Flush(); err != nil {
+ return
+ }
+ time.Sleep(time.Second)
+ }
+ })
+}