Comments (3)
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.
from gin.
Thank @jamesstocktonj1 for the response.
-
I think i should explain about my case. I attached the diagram. Service A in this diagram is like the adapter to transform http request to message queue. Traditionally, for each request, service A uses 1 go routine for the handler function to receive http request, produce message to a queue to internal services, consume message in a another queue, match the message's correlationID to the corresponding request then response back to client (everything is in 1 and only 1 go routine. We can seperate the produce/consume to different goroutines but the main point is http request and response currently are in the same goroutine). Now is there anyway to receive http request in 1 go routines, produce message to a queue then the go routine terminates. While we open another go routine to consume message from queue then perform somematching context/collerationID and response back to client? To sum up, for each request i want to spawn 2 goroutines: 1 for reading the request's body -> producing message, another one for consuming message -> response back to the client.
-
Right now, because of the low cost of context switching and magic go's cocurrent mechanic, the traditional approach may work fine but to me it is not optimized since we have to wait for the corresponding message. Maybe my thought is different from GO's world. Please Let me know your opinion.
from gin.
A context switch will always take less time than making a network call to add to the message queue.
Go's goroutines have even less context to switch than a standard thread due to their lightweight nature. Also as for having to wait for a message. Go's scheduler knows when goroutines are blocked and won't schedule them unless they are unblocked. Therefore when a goroutine is blocked waiting for a http response it will not be blocking another goroutine from being able to run on the cpu.
from gin.
Related Issues (20)
- Can't get status as expected from context.Status() HOT 1
- Gin Context implementation breaks context.Context contract HOT 4
- [Suggestions] Introducing an AI-powered robot to assist with daily issue and pull request (PR) tasks.
- Need c.JSON() response like behaviour for c.SSEvent() response for supporting first party clients like OpenAI SDK HOT 2
- custom time.Time type can not use bindQuery get a value. HOT 3
- configure server parameters HOT 1
- The binding:"required" tag does not seem to work for struct types HOT 3
- Is there a way to preserve order in JSON on response? HOT 1
- redirect is not success HOT 1
- I want to use shouldmindbodywith first and then use formafile, but it doesn't work right now HOT 3
- Extend the routing tree entry
- Transparent decompression for gzip, deflate, etc? HOT 1
- Custom time.Time type can not use bindQuery get a value 2 HOT 2
- http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83) HOT 1
- Suggestion: Move Print Debug to Run() functions? HOT 7
- AbortWithStatusJSON doesn't abort context HOT 2
- I translated `tree.go` into java
- GetUint and GetUint64 wrong return HOT 1
- Multipart form-data request with empty key for single file upload
- gin with golang 1.21, 1.22 DATA RACE
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.