2016-01-11 09:52
浏览 27


I'm new to concurrent programming, and have no idea what concepts to start with, so please be gentle.

I am writing a webservice as a front-end to a TCP server. This server listens to the port I give it, and returns the response to the TCP connection for each request.

Here is why I'm writing a web-service front-end for this server:

  • The server can handle one request at a time, and I'm trying to make it be able to process several inputs concurrently, by launching multiple processes and giving them a different port to listen on. For example, I want to launch 30 instances and tell them to listen on ports 20000-20029.
  • Our team uses PHP, and PHP does not have the capacity to launch server instances and maintain them concurrently, so I'm trying to write an API they can just send HTTP requests to.

So, here is the structure I have thought of.

  1. I will have a main() function. This function launches the processes concurrently, then starts an HTTP server on port 80 and listens.
  2. I have an http.Handler that adds the content of a request to a channel,.
  3. I will have gorutines, one per server instance, that are in an infinite loop.

The code for the function mentioned in item three would be something like this:

func handleRequest(queue chan string) {
    for {
        request := <-queue
        conn, err := connectToServer()
        err = sendRequestToServer(conn)
        response, err := readResponseFromServer(conn)

So, my http.Handler can simply do something like queue<- request to add the request to the queue, and handleRequest, which has blocked, waiting for the channel to have something to get, will simply get the request and continue on. When done, the loop finishes, execution comes back to the request := <-queue, and the same thing continues.

My problem starts in the http.Handler. It makes perfect sense to put requests in a channel, because multiple gorutines are all listening to it. However, how can these gorutines return the result to my http.Handler?

One way is to use a channel, let's call it responseQueue, that all of these gorutines would then write to. The problem is that when a response is added to the channel, I don't know which request it belongs to. In other words, when multiple http.Handlers send requests, each executing handler will not know which response the current message in the channel belongs to.

Is there a best practice, or a pattern, to send data to a gorutine from another gorutine and receive the data back?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanmen2189 2016-01-11 10:23

    Create a per request response channel and include it in the value sent to the worker. The handler receives from the channel. The worker sends the result to the channel.

    打赏 评论

相关推荐 更多相似问题