Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sse panic on fasthttphandler #3840

Open
ysk229 opened this issue Feb 8, 2024 · 1 comment
Open

sse panic on fasthttphandler #3840

ysk229 opened this issue Feb 8, 2024 · 1 comment

Comments

@ysk229
Copy link

ysk229 commented Feb 8, 2024

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/gin@v1.9.1/response_writer.go:117 (0xb4d34b)
   (*responseWriter).CloseNotify: return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/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/gin@v1.9.1/context.go:174 (0xb4c2c1)
   (*Context).Next: c.handlers[c.index](c)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0xb4c2ac)
   CustomRecoveryWithWriter.func1: c.Next()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0xb4b3e6)
   (*Context).Next: c.handlers[c.index](c)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0xb4b3c9)
   LoggerWithConfig.func1: c.Next()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0xb4a48a)
   (*Context).Next: c.handlers[c.index](c)
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0xb4a111)
   (*Engine).handleHTTPRequest: c.Next()
C:/Users/xxgo/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0xb49dbc)
   (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Users/xxgo/go/pkg/mod/github.com/valyala/fasthttp@v1.51.0/fasthttpadaptor/adaptor.go:58 (0xb7e8f3)
   NewFastHTTPHandler.func1: h.ServeHTTP(&w, r.WithContext(ctx))
C:/Users/xxgo/go/pkg/mod/github.com/valyala/fasthttp@v1.51.0/server.go:2359 (0xb730f2)
   (*Server).serveConn: s.Handler(ctx)
C:/Users/xxgo/go/pkg/mod/github.com/valyala/fasthttp@v1.51.0/workerpool.go:224 (0xb7b048)
   (*workerPool).workerFunc: if err = wp.WorkerFunc(c); err != nil && err != errHijacked {
C:/Users/xxgo/go/pkg/mod/github.com/valyala/fasthttp@v1.51.0/workerpool.go:196 (0xb7adb7)
@RedCrazyGhost
Copy link
Contributor

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants