Files
fn_registry/functions/infra/sse_keepalive.md
T
egutierrez 47fac22230 chore: auto-commit (799 archivos)
- .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>
2026-05-14 00:28:20 +02:00

2.2 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_keepalive function go infra 1.0.0 impure func SSEKeepalive(w http.ResponseWriter, interval time.Duration, done <-chan struct{}) Envia comentarios SSE periodicos (`: keepalive\n\n`) al writer hasta que done se cierre. Sirve para evitar que proxies o load balancers cierren conexiones inactivas. Bloqueante: lanzar como goroutine. Si w implementa http.Flusher hace flush tras cada keepalive.
sse
keepalive
server-sent-events
http
server
infra
realtime
pendiente-usar
false error_go_core
net/http
time
name desc
w http.ResponseWriter destino del stream SSE. Si implementa http.Flusher se hace flush tras cada keepalive.
name desc
interval intervalo entre keepalives (recomendado 15-30s). Si <= 0 se usa 30s por defecto.
name desc
done canal de cierre. Cuando se cierre, la goroutine retorna inmediatamente.
ningun retorno; la funcion retorna cuando done se cierra o cuando una escritura falla (cliente desconectado) true
escribe comentario keepalive periodicamente
termina cuando done se cierra
termina cuando la escritura falla
functions/infra/sse_test.go functions/infra/sse_keepalive.go

Ejemplo

http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/event-stream")
    w.Header().Set("Cache-Control", "no-cache")
    w.Header().Set("Connection", "keep-alive")

    done := make(chan struct{})
    defer close(done)

    go SSEKeepalive(w, 15*time.Second, done)

    // Bucle principal de envio de eventos...
})

Notas

Comentario SSE: la spec dice que cualquier linea que empiece con : es un comentario y el cliente la ignora. Mantienen viva la conexion sin generar eventos espurios. Recomendado intervalo entre 15-30s para infraestructuras tipicas (nginx default = 60s timeout idle).

Si la escritura falla (conexion cerrada) la funcion retorna sin error, asumiendo que el handler principal ya detectara la desconexion via r.Context().Done().