Quick Tip: Adding keep alive to urllib (Node JS library)

Matthew Barben
Driver Lane
Published in
2 min readOct 9, 2022
Photo by Frankie Lopez on Unsplash

I use urllib as my HTTP client library of choice at the moment in a number of my developments. It is simple enough to use and it also provides a nice timing library to assist with monitoring performance.

There are other alternatives out there, but not all the serverless environments are using node 18 yet so I can not make my transition to a built-in request library.

One downside is that the library doesn't default its HTTP agent to keep alive. The impact of this is that you will receive connection resets.

In this quick tip, we create a basic class that will handle an auth request to OTDS and we will add the keep alive component to it.

Add http and https Agent

The fix for this is reasonably simple. To add the keep alive we will overwrite the http agent- we do this by

  • importing the http and https library
  • Creating new https and http agents
  • applying the agents to the requests using urllib

The example class is updated below to display this.

And that is it — a simple fix but now you can reduce the amount of reset connections coming from both OTCS and OTDS

Connect with Driver Lane on Twitter, and LinkedIn, or directly on our website.

--

--