Timing Out HTTP Connections

Let’s learn how to time out HTTP connections.

This lesson presents techniques for timing out HTTP connections that take too long to finish and work either on the server or the client side.

Using SetDeadline()

The SetDeadline() function is used by net to set the read and write deadlines of network connections. Due to the way the SetDeadline() function works, we need to call SetDeadline() before any read or write operation. Keep in mind that Go uses deadlines to implement timeouts, so we do not need to reset the timeout every time our application receives or sends any data.

Coding example

The use of SetDeadline() is illustrated in withDeadline.go and, more specifically, in the implementation of the Timeout() function:

Get hands-on with 1400+ tech skills courses.