douxiegan6468 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条