{"id":"07472e858d460b07","repo":"gofiber/fiber","slug":"sse-write-retry-w","errorCode":null,"errorMessage":"sse: write retry: %w","messagePattern":"sse: write retry: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"middleware/sse/sse.go","lineNumber":161,"sourceCode":"\treturn s.write(func(w *bufio.Writer) error {\n\t\treturn writeComment(w, comment)\n\t})\n}\n\n// Retry writes an SSE retry field and flushes it to the client.\nfunc (s *Stream) Retry(retry time.Duration) error {\n\tif retry <= 0 {\n\t\treturn nil\n\t}\n\treturn s.write(func(w *bufio.Writer) error {\n\t\t// Assemble the whole field in a fixed-size scratch and issue one\n\t\t// write: no fmt boxing, and a single error branch.\n\t\tvar scratch [32]byte\n\t\tframe := append(scratch[:0], \"retry: \"...)\n\t\tframe = utils.AppendInt(frame, retry.Milliseconds())\n\t\tframe = append(frame, '\\n', '\\n')\n\t\tif _, err := w.Write(frame); err != nil {\n\t\t\treturn fmt.Errorf(\"sse: write retry: %w\", err)\n\t\t}\n\t\treturn nil\n\t})\n}\n\nfunc (s *Stream) write(fn func(w *bufio.Writer) error) error {\n\ts.mu.Lock()\n\tdefer s.mu.Unlock()\n\n\tif s.err != nil {\n\t\treturn s.err\n\t}\n\tif s.isClosed {\n\t\treturn errStreamClosed\n\t}\n\tif err := fn(s.w); err != nil {\n\t\treturn s.failLocked(err)\n\t}","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/sse/sse.go#L143-L179","documentation":"Writing the SSE retry: field (the reconnection hint in ms) to the stream failed at w.Write. This is sent once at stream start when Config.Retry > 0, or on demand via stream.Retry(d); failure means the client has disconnected or the connection broke.","triggerScenarios":"Config.Retry is set and the client closed the connection during the initial write; or stream.Retry(d) is called after the client is gone.","commonSituations":"Clients that connect and immediately disconnect; proxy closing the link before the first byte; flaky connections.","solutions":["Treat the returned error as stream termination and return from the handler.","If the initial Retry write must succeed, ensure the client is connected before sending (it is the first frame).","Set Config.Retry only when you actually want to advise client reconnect delay."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := stream.Retry(reconnectIn); err != nil {\n    return // client gone\n}","preventionTips":["Check the return of stream.Retry.","Don't send Retry repeatedly on a dead stream."],"tags":["sse","network","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}