gin
gin copied to clipboard
sse panic on fasthttphandler
Senario:
func sseHandlerGin(c *gin.Context) {
// Set headers for Server-Sent Events
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
// Stream data to the client
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
data := fmt.Sprintf("data: %s\n\n", "New data available at: "+time.Now().String())
c.Stream(func(w io.Writer) bool {
_, _ = w.Write([]byte(data))
return false
})
}
}
}
engine := gin.Default()
engine.GET("/sse",sseHandlerGin)
if err := fasthttp.ListenAndServe(":8080", fasthttpadaptor.NewFastHTTPHandler(engine)); err != nil {
panic(err)
}
i've got these panic error log.
interface conversion: *fasthttpadaptor.netHTTPResponseWriter is not http.CloseNotifier: missing method CloseNotify
C:/Users/xxgo/go/go1.20.10/src/runtime/iface.go:92 (0x8ba444)
getitab: panic(&TypeAssertionError{concrete: typ, asserted: &inter.typ, missingMethod: m.init()})
C:/Users/xxgo/go/go1.20.10/src/runtime/iface.go:429 (0x8bb23c)
assertI2I: return getitab(inter, tab._type, false)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/response_writer.go:117 (0xb4d34b)
(*responseWriter).CloseNotify: return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:1084 (0xb47a14)
(*Context).Stream: clientGone := w.CloseNotify()
E:/code/go/408/cmd/see_fasthttp.go:27 (0xb7f738)
sseHandlerGin: c.Stream(func(w io.Writer) bool {
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0xb4c2c1)
(*Context).Next: c.handlers[c.index](c)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 (0xb4c2ac)
CustomRecoveryWithWriter.func1: c.Next()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0xb4b3e6)
(*Context).Next: c.handlers[c.index](c)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:240 (0xb4b3c9)
LoggerWithConfig.func1: c.Next()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0xb4a48a)
(*Context).Next: c.handlers[c.index](c)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:620 (0xb4a111)
(*Engine).handleHTTPRequest: c.Next()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:576 (0xb49dbc)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Users/xxgo/go/pkg/mod/github.com/valyala/[email protected]/fasthttpadaptor/adaptor.go:58 (0xb7e8f3)
NewFastHTTPHandler.func1: h.ServeHTTP(&w, r.WithContext(ctx))
C:/Users/xxgo/go/pkg/mod/github.com/valyala/[email protected]/server.go:2359 (0xb730f2)
(*Server).serveConn: s.Handler(ctx)
C:/Users/xxgo/go/pkg/mod/github.com/valyala/[email protected]/workerpool.go:224 (0xb7b048)
(*workerPool).workerFunc: if err = wp.WorkerFunc(c); err != nil && err != errHijacked {
C:/Users/xxgo/go/pkg/mod/github.com/valyala/[email protected]/workerpool.go:196 (0xb7adb7)
There's something wrong with your usage. gin does not currently support fasthttp Reference Add support for fasthttp or fasthttprouter #498
- if err := fasthttp.ListenAndServe(":8080", fasthttpadaptor.NewFastHTTPHandler(engine)); err != nil {
- panic(err)
- }
+ engine.Run(":8080")