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
How can i prevent a http connection to close #3874
Comments
By default Gin won't timeout your connection, you need to add your own middleware if you want that functionality. Have you tried this code? What issue are you facing in regard to this? This scenario isn't something that is usually implemented. Usually, you use asynchronous message queues like Kafka and RabbitMQ when you don't want a response in a set timeframe. If you want service B to reply within the same http request then you should use something that is synchronous like another http request or gRPC call. Waiting for a response from Kafka whilst keeping the http connection open can open a whole host of issues. What if the request or response is lost in Kafka? The client making the http request will wait indefinitely. I hope this clears things up and answers your question. |
Thank @jamesstocktonj1 for the response.
|
A context switch will always take less time than making a network call to add to the message queue. |
Description
Hi, i'm new from golang and gin. I want to implement a system that: Client sends http request to service A. Service A communicates with service B through kafka. Then service A takes the response from kafka then matches with the request id from client and responses back to client. The normal approach of service A would be:
Where we can see that the goroutine of each request has to wait for the message consumed from Kafka then response to client.
Now my question is: Does gin support passing the context through other goroutines so that i can use the context corresponding to the request.ID to response to the client. Draft code of my idea:
On the about implementation, the connection must be kept alive during the process produce and consume message from kafka. The connection only shutdown when servers response a message back to client. My teammate successfully implemented this idea using vertx using eventbus and RoutingContext so i guess there are some mechanics to work around that. I tried to implement in gin but the response doesnt match with the request (for example client sends a request with id=1 and receives the response with id=2). I notice that gin will sends a reponse with status = 200, body = nil as soon as the
func(c *gin.Context)
returns so how can we prevent that?Feel free to discuss.
Thank you for reading my question.
Environment
The text was updated successfully, but these errors were encountered: