Skip to Content
Practical Go
book

Practical Go

by Amit Saha
December 2021
Beginner to intermediate
416 pages
9h 36m
English
Wiley
Content preview from Practical Go

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Go in Practice

Go in Practice

Matt Butcher, Matt Farina
Efficient Go

Efficient Go

Bartlomiej Plotka
Black Hat Go

Black Hat Go

Tom Steele, Chris Patten, Dan Kottmann
Go in Action

Go in Action

Brian Ketelsen, Erik St. Martin, William Kennedy

Publisher Resources

ISBN: 9781119773818Purchase Link