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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line