danielgtaylor / huma

Huma REST/HTTP API Framework for Golang with OpenAPI 3.1
https://huma.rocks/
MIT License
1.95k stars 144 forks source link

SSE Error with gin adapter: "warning: unable to set write deadline" #491

Open darrenparkinson opened 3 months ago

darrenparkinson commented 3 months ago

Hi,

Just looking to add SSE to an existing project that uses the gin router.

I'm using the example here but when I swap the router for Gin, I'm getting the error: warning: unable to set write deadline for every sent message.

e.g.

router := gin.Default()
api := humagin.New(router, huma.DefaultConfig("My API", "1.0.0"))

I notice this doesn't happen when using humago or humachi.

I can see that the error comes from this line here I think, but not sure what I'd need to do to fix that?

Is it because the gin context referenced in the adapter doesn't implement the SetWriteDeadline(time.Time) error function?

Any thoughts greatly appreciated. Many thanks.

danielgtaylor commented 2 months ago

@darrenparkinson it seems that the Gin context's Writer field doesn't support it. Maybe there is a different way to do SSE with Gin? Here is an example that seems to use built-in functionality in Gin:

https://github.com/gin-gonic/examples/blob/master/server-sent-event/main.go#L65

You may just not be able to use this functionality in Huma with Gin as the router. The alternative would be to try and figure out how to build it into the Gin adapter, but I'm not sure how feasible that is.

clausMeko commented 1 month ago

@danielgtaylor I had the same problem and just switched to a different sse implementation.

It uses http.Handler and no huma.Operation.

Problem: Is there a way to get an entry in the openAPi doc say for api/v1/stream ?

cheers

clausMeko commented 1 month ago

I am a little confused why this works:

noOp := func(context.Context, *struct{}) (*struct{}, error) {
  return nil, nil
}
huma.Get(api, "/shelf/stream", noOp, tag("stream"))
r.Handle("/shelf/stream", s)

where s is the alternative implementation.

I thought if a handler is registered for a route it there cannot be another one. But it works and i leave it for documentation, so one can get some OpenApi entries for http.Handler. This might also kind of fixes #486. I guess they could put noops for old endpoints and incrementally upgrade to huma.

jzillmann commented 1 month ago

getting the same logging with chi...

clausMeko commented 1 month ago

@jzillmann I have to confirm that, was also using chi.