dqn48247 2018-03-13 11:15
浏览 226
已采纳

异步http请求处理

I'm trying to handle an HTTP request in Go asynchronously this way:

  1. I pass a handler function to the HTTP server
  2. In the handler I store the HttpRequest / HttpResponse objects in a slice or a map
  3. When returning from the handler function - the response is NOT returned to the client, but the connection remains open
  4. When "some" async input received from another source, I take the relevant HttpRequest / HttpResponse objects from memory, write the response and close the connection.

What I aim for is very similar to Jetty-Continuation in Java.

How can I implement such a behaviour in GoLang?

  • 写回答

1条回答 默认 最新

  • dongpu3898 2018-03-13 11:35
    关注

    You do not need this behavior in Go.

    Java HTTP servers use threads and if the servlet waits for something it effectively blocks the current thread. Threads are heavy and thread pools are limited.

    In Go the HTTP server implementation uses goroutines and if they are waiting they will not block operating system thread. Goroutines are lightweight and scheduled effectively by Go runtime. By effectively scheduling I mean switching when goroutine makes a system call or waits on channel.

    Simply speaking, do not try to copy the workaround from Java servlets as the workaround is not needed in Go.

    Let's consider a Java servlet, servlets share operating system threads

    class Slow extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
            Thread.sleep(1000);
            // stops the thread for a second
            // operating system puts a thread aside and reuses processor
            // it is out of Java control
            // when all pooled HTTP server threads are sleeping no request is served
        }
    
    }
    

    and Go HTTP handler, each handler is run in a seperate goroutine

    func fast(w http.ResponseWriter, r *http.Request) {
        time.Sleep(10000 * time.Second) 
        // Go scheduler puts the goroutine aside 
        // and reuses OS thread for handling another request
        // when one second passes the goroutine is scheduled again 
        // and finishes serving request
    }
    

    In Go you get what you want by default.

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

报告相同问题?