Skip to content

Commit 30b6be8

Browse files
committed
Add optional retries to connection attempts
1 parent a97c1d8 commit 30b6be8

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

splunklib/binding.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import socket
3232
import ssl
3333
import sys
34+
import time
3435
from base64 import b64encode
3536
from contextlib import contextmanager
3637
from datetime import datetime
@@ -454,6 +455,11 @@ class Context(object):
454455
:type splunkToken: ``string``
455456
:param headers: List of extra HTTP headers to send (optional).
456457
:type headers: ``list`` of 2-tuples.
458+
:param retires: Number of retries for each HTTP connection (optional, the default is 0).
459+
NOTE THAT THIS MAY INCREASE THE NUMBER OF ROUND TRIP CONNECTIONS TO THE SPLUNK SERVER.
460+
:type retries: ``int``
461+
:param retryBackoff: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
462+
:type retryBackoff: ``int`` (in seconds)
457463
:param handler: The HTTP request handler (optional).
458464
:returns: A ``Context`` instance.
459465
@@ -471,7 +477,8 @@ class Context(object):
471477
"""
472478
def __init__(self, handler=None, **kwargs):
473479
self.http = HttpLib(handler, kwargs.get("verify", False), key_file=kwargs.get("key_file"),
474-
cert_file=kwargs.get("cert_file")) # Default to False for backward compat
480+
cert_file=kwargs.get("cert_file"), # Default to False for backward compat
481+
retries=kwargs.get("retries", 0), retryBackoff=kwargs.get("retryBackoff", 10))
475482
self.token = kwargs.get("token", _NoAuthenticationToken)
476483
if self.token is None: # In case someone explicitly passes token=None
477484
self.token = _NoAuthenticationToken
@@ -1137,12 +1144,14 @@ class HttpLib(object):
11371144
11381145
If using the default handler, SSL verification can be disabled by passing verify=False.
11391146
"""
1140-
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None):
1147+
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None, retries=0, retryBackoff=10):
11411148
if custom_handler is None:
11421149
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file)
11431150
else:
11441151
self.handler = custom_handler
11451152
self._cookies = {}
1153+
self.retries = retries
1154+
self.retryBackoff = retryBackoff
11461155

11471156
def delete(self, url, headers=None, **kwargs):
11481157
"""Sends a DELETE request to a URL.
@@ -1256,7 +1265,16 @@ def request(self, url, message, **kwargs):
12561265
its structure).
12571266
:rtype: ``dict``
12581267
"""
1259-
response = self.handler(url, message, **kwargs)
1268+
while True:
1269+
try:
1270+
response = self.handler(url, message, **kwargs)
1271+
break
1272+
except Exception:
1273+
if self.retries <= 0:
1274+
raise
1275+
else:
1276+
time.sleep(self.retryBackoff)
1277+
self.retries -= 1
12601278
response = record(response)
12611279
if 400 <= response.status:
12621280
raise HTTPError(response)

splunklib/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ def connect(**kwargs):
318318
:type username: ``string``
319319
:param `password`: The password for the Splunk account.
320320
:type password: ``string``
321+
:param retires: Number of retries for each HTTP connection (optional, the default is 0).
322+
NOTE THAT THIS MAY INCREASE THE NUMBER OF ROUND TRIP CONNECTIONS TO THE SPLUNK SERVER.
323+
:type retries: ``int``
324+
:param retryBackoff: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
325+
:type retryBackoff: ``int`` (in seconds)
321326
:return: An initialized :class:`Service` connection.
322327
323328
**Example**::
@@ -384,6 +389,11 @@ class Service(_BaseService):
384389
:param `password`: The password, which is used to authenticate the Splunk
385390
instance.
386391
:type password: ``string``
392+
:param retires: Number of retries for each HTTP connection (optional, the default is 0).
393+
NOTE THAT THIS MAY INCREASE THE NUMBER OF ROUND TRIP CONNECTIONS TO THE SPLUNK SERVER.
394+
:type retries: ``int``
395+
:param retryBackoff: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
396+
:type retryBackoff: ``int`` (in seconds)
387397
:return: A :class:`Service` instance.
388398
389399
**Example**::

0 commit comments

Comments
 (0)