From b4113b926e117e2e04dddf4a5bdb2126a832047c Mon Sep 17 00:00:00 2001 From: Lefteris Zafiris Date: Wed, 28 Jul 2021 13:20:30 +0300 Subject: [PATCH 1/2] Set the TCP keepalive socket options Configure the TCP keepalive timeout and interval in a way that matches our server side behaviour. opsbugs #2100 --- customerio/client_base.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/customerio/client_base.py b/customerio/client_base.py index 388ad14..396b8e6 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, 550), + (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 From 6f672acfc3be9c43e08b0111e103c199f98a8a33 Mon Sep 17 00:00:00 2001 From: Lefteris Zafiris Date: Wed, 28 Jul 2021 18:23:07 +0300 Subject: [PATCH 2/2] Set TCP keepalive timeout to 300 sec While testing we found out that the TCP timeout on GCP loadbalancers is 360 sec. The 600 sec value refers to connections from the LB to backends. --- customerio/client_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/customerio/client_base.py b/customerio/client_base.py index 396b8e6..ecb2370 100644 --- a/customerio/client_base.py +++ b/customerio/client_base.py @@ -23,7 +23,7 @@ def __init__(self, retries=3, timeout=10, backoff_factor=0.02): # 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, 550), + (socket.SOL_TCP, socket.TCP_KEEPIDLE, 300), (socket.SOL_TCP, socket.TCP_KEEPINTVL, 60) ] )