diff options
| author | Adnan Maolood <[email protected]> | 2021-02-23 15:31:41 -0500 |
|---|---|---|
| committer | Adnan Maolood <[email protected]> | 2021-02-23 15:31:41 -0500 |
| commit | 09e3393e4cf9d125ecac53087a995e866295f229 (patch) | |
| tree | 6837424dfad3ce0108b8d194d53108752a97342b | |
| parent | timeout: Don't recover from panics (diff) | |
| download | go-gemini-09e3393e4cf9d125ecac53087a995e866295f229.tar.xz go-gemini-09e3393e4cf9d125ecac53087a995e866295f229.zip | |
examples/stream: Simplify
| -rw-r--r-- | examples/stream.go | 29 |
1 files changed, 6 insertions, 23 deletions
diff --git a/examples/stream.go b/examples/stream.go index 5b49b11..7dadf31 100644 --- a/examples/stream.go +++ b/examples/stream.go @@ -39,33 +39,16 @@ func main() { // stream writes an infinite stream to w. func stream(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { - ch := make(chan string) - ctx, cancel := context.WithCancel(ctx) - - go func(ctx context.Context) { - for { - select { - case <-ctx.Done(): - return - default: - ch <- fmt.Sprint(time.Now().UTC()) - } - time.Sleep(time.Second) - } - // Close channel when finished. - // In this example this will never be reached. - close(ch) - }(ctx) - for { - s, ok := <-ch - if !ok { - break + select { + case <-ctx.Done(): + return + default: } - fmt.Fprintln(w, s) + fmt.Fprintln(w, time.Now().UTC()) if err := w.Flush(); err != nil { - cancel() return } + time.Sleep(time.Second) } } |