Skip to content

Commit

Permalink
Split timeouts and adjust them
Browse files Browse the repository at this point in the history
  • Loading branch information
aursulis committed May 3, 2024
1 parent 0d4c460 commit 42d0593
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 7 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
olimp-control (1.3-1) unstable; urgency=low

* Split timeouts into connection and read
* Adjust the parameters

-- LMIO TC <[email protected]> Fri, 03 May 2024 14:23:00 +0000

olimp-control (1.2-1) unstable; urgency=low

* Use session object with retry and shorter timeout
Expand Down
2 changes: 1 addition & 1 deletion makedeb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://www.debian.org/doc/manuals/maint-guide/
# https://wiki.debian.org/Packaging

ver='1.2'
ver='1.3'
projdir="olimp-control-${ver}"
projtar="olimp-control_${ver}.orig.tar.gz"
builddir="build"
Expand Down
21 changes: 14 additions & 7 deletions olimp-control.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class LmioCtrlApi:
_FAILED_AUTH = 'Response failed authentication validation'
_NO_TICKETS = 'No tickets available'

def __init__(self, url, key, mid, timeout):
def __init__(self, url, key, mid, connect_timeout, read_timeout):
self._url = url
self._key = key.encode('utf-8')
self._mid = mid
self._timeout = timeout if timeout > 0.0 else None
self._timeout = (connect_timeout, read_timeout)

def _get_body_hmac(self, body):
return hmac.HMAC(self._key, body, 'sha1').hexdigest()
Expand Down Expand Up @@ -52,7 +52,7 @@ def _validate_response(self, body, timestamp, headers):

def get_session(self):
session = requests.Session()
retry = requests.adapters.Retry(connect=5, backoff_factor=0.5)
retry = requests.adapters.Retry(connect=5, backoff_factor=0.1)
adapter = requests.adapters.HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
Expand Down Expand Up @@ -146,8 +146,8 @@ def get_machine_id():
all_macs = subprocess.check_output("cat /sys/class/net/*/address | sort", shell=True)
return hashlib.sha1(all_macs).hexdigest()

def main_loop(url, key, mid, poll_frequency, timeout, exit_event):
api = LmioCtrlApi(url, key, mid, timeout)
def main_loop(url, key, mid, poll_frequency, connect_timeout, read_timeout, exit_event):
api = LmioCtrlApi(url, key, mid, connect_timeout, read_timeout)
while True:
session = api.get_session()
api.do_ping(session)
Expand All @@ -166,18 +166,25 @@ def main_loop(url, key, mid, poll_frequency, timeout, exit_event):
parser.add_argument('-f', '--poll-frequency', default=60.0, type=float,
help='frequency of server check-in, in seconds')
parser.add_argument('-k', '--key-file', default='/etc/olimp-control/key', help='path to key file')
parser.add_argument('-t', '--timeout', default=20.0, type=float, help='timeout for API operations, in seconds')
parser.add_argument('--connect-timeout', default=5.0, type=float, help='timeout for connections, in seconds')
parser.add_argument('--read-timeout', default=20.0, type=float, help='timeout for read, in seconds')
parser.add_argument('url', nargs='?', default='https://ctrl.lmio.lt/olimp/api',
help='url of control api base')

args = parser.parse_args()
if args.url[-1] == '/':
args.url = args.url[:-1]

if args.connect_timeout <= 0.0:
args.connect_timeout = None
if args.read_timeout <= 0.0:
args.read_timeout = None

shutdown_event = threading.Event()
sigterm_handler = lambda sig, stack: shutdown_event.set()

signal.signal(signal.SIGINT, sigterm_handler)
signal.signal(signal.SIGTERM, sigterm_handler)

main_loop(args.url, get_key(args.key_file), get_machine_id(), args.poll_frequency, args.timeout, shutdown_event)
main_loop(args.url, get_key(args.key_file), get_machine_id(), args.poll_frequency, args.connect_timeout,
args.read_timeout, shutdown_event)

0 comments on commit 42d0593

Please sign in to comment.