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.

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

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler