stream.go94 lines, 2.3 KB
| 1 | // Package live pushes news to the clients that are connected right now. |
| 2 | package live |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "strconv" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/fiston-user/murmur/internal/platform/httpx" |
| 12 | ) |
| 13 | |
| 14 | // beat is how often a quiet stream says something. |
| 15 | const beat = 15 * time.Second |
| 16 | |
| 17 | // wait is how long one write to a client may take. |
| 18 | const wait = 10 * time.Second |
| 19 | |
| 20 | // A Handler serves GET /live: one user's events, as they happen. |
| 21 | type Handler struct { |
| 22 | Done <-chan struct{} // closed when this server shuts down |
| 23 | Hub *Hub // the streams that are open on this server |
| 24 | User func(context.Context) int64 // who a request is from |
| 25 | Missed Missed // what a client missed while it was away |
| 26 | } |
| 27 | |
| 28 | // Missed returns a user's events with an id above after, oldest first. |
| 29 | type Missed func(ctx context.Context, user, after int64) ([]Event, error) |
| 30 | |
| 31 | // Stream answers with text/event-stream, and it doesn't return for as long as |
| 32 | // the client listens. |
| 33 | func (h *Handler) Stream(w http.ResponseWriter, r *http.Request) { |
| 34 | user := h.User(r.Context()) |
| 35 | events := h.Hub.Register(user) |
| 36 | defer h.Hub.Unregister(user, events) |
| 37 | after, err := strconv.ParseInt(r.Header.Get("Last-Event-ID"), 10, 64) |
| 38 | var old []Event |
| 39 | if err == nil { |
| 40 | if old, err = h.Missed(r.Context(), user, after); err != nil { |
| 41 | httpx.ServerError(w, r, err) |
| 42 | return |
| 43 | } |
| 44 | } |
| 45 | w.Header().Set("Content-Type", "text/event-stream") |
| 46 | w.Header().Set("Cache-Control", "no-cache") |
| 47 | w.Header().Set("X-Accel-Buffering", "no") |
| 48 | rc := http.NewResponseController(w) |
| 49 | send := func(text string) error { |
| 50 | rc.SetWriteDeadline(time.Now().Add(wait)) |
| 51 | fmt.Fprint(w, text) |
| 52 | return rc.Flush() |
| 53 | } |
| 54 | send(": connected\n\n") |
| 55 | var seen int64 |
| 56 | for _, e := range old { |
| 57 | if send(e.text()) != nil { |
| 58 | return |
| 59 | } |
| 60 | seen = e.ID |
| 61 | } |
| 62 | tick := time.NewTicker(beat) |
| 63 | defer tick.Stop() |
| 64 | for { |
| 65 | select { |
| 66 | case <-r.Context().Done(): |
| 67 | return |
| 68 | case <-h.Done: |
| 69 | return |
| 70 | case <-tick.C: |
| 71 | if send(": still here\n\n") != nil { |
| 72 | return |
| 73 | } |
| 74 | case e, open := <-events: |
| 75 | if !open { |
| 76 | return |
| 77 | } |
| 78 | if e.ID != 0 && e.ID <= seen { |
| 79 | continue |
| 80 | } |
| 81 | if send(e.text()) != nil { |
| 82 | return |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func (e Event) text() string { |
| 89 | text := fmt.Sprintf("event: %s\ndata: %s\n\n", e.Kind, e.Data) |
| 90 | if e.ID != 0 { |
| 91 | text = fmt.Sprintf("id: %d\n", e.ID) + text |
| 92 | } |
| 93 | return text |
| 94 | } |