NewEventStream creates a new SSE stream, with an optional payload which is used to send pings every [pingInterval].
(ctx context.Context, logger slog.Logger, pingPayload []byte, clk quartz.Clock)
| 51 | |
| 52 | // NewEventStream creates a new SSE stream, with an optional payload which is used to send pings every [pingInterval]. |
| 53 | func NewEventStream(ctx context.Context, logger slog.Logger, pingPayload []byte, clk quartz.Clock) *EventStream { |
| 54 | // Send periodic pings to keep connections alive. |
| 55 | // The upstream provider may also send their own pings, but we can't rely on this. |
| 56 | tick := time.NewTicker(time.Nanosecond) |
| 57 | tick.Stop() // Ticker will start after stream initiation. |
| 58 | |
| 59 | return &EventStream{ |
| 60 | ctx: ctx, |
| 61 | logger: logger, |
| 62 | clk: clk, |
| 63 | |
| 64 | pingPayload: pingPayload, |
| 65 | |
| 66 | eventsCh: make(chan event, 128), // Small buffer to unblock senders; once full, senders will block. |
| 67 | doneCh: make(chan struct{}), |
| 68 | tick: tick, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // InitiateStream initiates the SSE stream by sending headers and starting the |
| 73 | // ping ticker. This is safe to call multiple times as only the first call has |