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)
| 44 | |
| 45 | // NewEventStream creates a new SSE stream, with an optional payload which is used to send pings every [pingInterval]. |
| 46 | func NewEventStream(ctx context.Context, logger slog.Logger, pingPayload []byte) *EventStream { |
| 47 | // Send periodic pings to keep connections alive. |
| 48 | // The upstream provider may also send their own pings, but we can't rely on this. |
| 49 | tick := time.NewTicker(time.Nanosecond) |
| 50 | tick.Stop() // Ticker will start after stream initiation. |
| 51 | |
| 52 | return &EventStream{ |
| 53 | ctx: ctx, |
| 54 | logger: logger, |
| 55 | |
| 56 | pingPayload: pingPayload, |
| 57 | |
| 58 | eventsCh: make(chan event, 128), // Small buffer to unblock senders; once full, senders will block. |
| 59 | doneCh: make(chan struct{}), |
| 60 | tick: tick, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // InitiateStream initiates the SSE stream by sending headers and starting the |
| 65 | // ping ticker. This is safe to call multiple times as only the first call has |
no outgoing calls
no test coverage detected