Did you know that the PagerDuty REST API supports HTTP connection reuse? This allows you to make your API calls more efficiently and with less network traffic. Without it, each of the following operations would have to be performed as often as once for each API call:
- (If DNS caching isn’t used) make a DNS request to resolve
- Establish a TCP connection
- Perform a TLS handshake
Each of these prerequisite steps require round-trip network communication and take additional time. Thus, without connection reuse, network latency much more greatly affects the performance of your API calls.
However, by setting the header
Connection: keep-alive and using the same network socket for subsequent HTTP requests, the client can avoid going through all the initiation steps in connecting to the REST API in each API call.
Testing your Python code for connection reuse
If you are using Requests, you can check to see if your Python code is properly leveraging the library’s connection-reuse abilities by examining info-level log messages from the underlying
urllib3 library. The following code will enable info logging to STDERR and provide such visibility:
import logging logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler()) # API calls here
Among output, you should see only once for multiple API calls a message that reads:
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.pagerduty.com
Python REST API sessions with automatic connection reuse
Not long ago, we found out that our internal Python-based REST API tools did not reuse connections when making multiple API requests. This had some unfortunate consequences: our scripts for bulk operations were quite slow. Moreover, one time we ran a lot of operations in parallel on a single host, and all threads halted with
urllib3.ConnectionError exceptions that indicated an error attempting to resolve the host
api.pagerduty.com. Apparently, we had reached the DNS resolver’s rate limit before hitting the PagerDuty REST API rate limit!
To address this issue and eliminate code duplication in our tooling, we created
pdpyras, a dead-simple REST API client based on requests.Session. It provides a convenient interface for making basic HTTP requests to the REST API, while enforcing connection reuse.
For more information on this module and how to install and use it, see: