CHAPTER 4Advanced HTTP Clients
In this chapter, you are going to do a deep dive into writing HTTP clients. The last chapter focused on performing various operations over HTTP. This chapter focuses on the techniques used to write HTTP clients that are robust and scale well. You will learn to enforce time-outs in your clients, create client middleware, and explore connection pooling. Let's get started!
Using a Custom HTTP Client
Let's consider the data downloader application that you wrote in the previous chapter. It's rare that the server with which you are communicating always behaves as expected. In fact, it's not just the server, but any of the other networking devices that your application's request is passing through may behave suboptimally. How does our client fare then? Let's find out.
Downloading from an Overloaded Server
Let's consider the following function, which will create an always overloaded test HTTP server where every response is delayed by 60 seconds:
func startBadTestHTTPServer() *httptest.Server {
ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
time.Sleep(60 * time.Second)
fmt.Fprint(w, "Hello World")
}))
return ts
}
Note the call to the Sleep()
function from the time
package. This will introduce a delay of 60 seconds before it sends a response to the client. Listing 4.1 shows a test function that sends an HTTP GET request to the bad test server.
Get Practical Go now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.