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.