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 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败