diff --git a/customerio/client_base.py b/customerio/client_base.py index 388ad14..ecb2370 100644 --- a/customerio/client_base.py +++ b/customerio/client_base.py @@ -4,10 +4,12 @@ from __future__ import division from datetime import datetime, timezone import math +import socket from requests import Session from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry +from urllib3.connection import HTTPConnection class CustomerIOException(Exception): @@ -18,6 +20,14 @@ def __init__(self, retries=3, timeout=10, backoff_factor=0.02): self.timeout = timeout self.retries = retries + # Set the TCP keepalive settings to the values dicated by our server-side configuration. + HTTPConnection.default_socket_options = ( HTTPConnection.default_socket_options + [ + (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), + (socket.SOL_TCP, socket.TCP_KEEPIDLE, 300), + (socket.SOL_TCP, socket.TCP_KEEPINTVL, 60) + ] + ) + self.http = Session() # Retry request a number of times before raising an exception # also define backoff_factor to delay each retry