47fac22230
- .claude/CLAUDE.md - .claude/commands/subagentes.md - .claude/rules/INDEX.md - .mcp.json - bash/functions/cybersecurity/analyze_dns.md - bash/functions/cybersecurity/audit_http_headers.md - bash/functions/cybersecurity/audit_ssh_config.md - bash/functions/cybersecurity/check_firewall.md - bash/functions/cybersecurity/detect_suspicious_users.md - bash/functions/cybersecurity/encrypt_file.md - ... Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2.4 KiB
2.4 KiB
name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
| name | kind | lang | domain | version | purity | signature | description | tags | uses_functions | uses_types | returns | returns_optional | error_type | imports | params | output | tested | tests | test_file_path | file_path | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| sse_handler | function | go | infra | 1.0.0 | impure | func SSEHandler(events <-chan SSEEvent) http.HandlerFunc | Retorna un http.HandlerFunc que setea los headers SSE (Content-Type, Cache-Control, Connection, X-Accel-Buffering), consume eventos del canal y los envia con flush a cada cliente conectado. Cierra limpiamente si el cliente se desconecta (context cancelado) o si el canal de eventos se cierra. |
|
|
|
false | error_go_core |
|
|
http.HandlerFunc lista para montarse en una ruta. Sirve un solo cliente por invocacion (cada request abre su propio stream). | true |
|
functions/infra/sse_test.go | functions/infra/sse_handler.go |
Ejemplo
events := make(chan SSEEvent, 100)
routes := []Route{
{Method: "GET", Path: "/events", Handler: SSEHandler(events)},
}
go func() {
for i := 0; ; i++ {
events <- SSEEvent{
Event: "tick",
ID: fmt.Sprintf("%d", i),
Data: fmt.Sprintf(`{"n":%d}`, i),
}
time.Sleep(time.Second)
}
}()
mux := HTTPRouter(routes)
HTTPServe(":8080", mux, ctx)
Notas
Importante: este handler asume un canal compartido entre todos los clientes o un canal por request. Si se quiere broadcast a multiples clientes desde una sola fuente, montar un fan-out por encima (ej: un hub similar a WSHub pero para SSE). Para uso tipico (1 cliente = 1 stream) basta con crear el canal dentro del handler externo y pasarlo.
X-Accel-Buffering: no deshabilita el buffering en nginx (sin esto los eventos se acumulan hasta que nginx flushea su buffer, anulando el real-time). En Caddy/Traefik no es necesario pero no estorba.
El handler no monta keepalives automaticamente. Si la conexion va a estar idle mas de ~30s, lanzar SSEKeepalive en una goroutine paralela compartiendo el mismo writer.