Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the TCP keepalive socket options #70

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions customerio/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down