Skip to content

Commit 1cdd852

Browse files
authored
Merge pull request #293 from mew1033/add-http-retries
Add optional retries to connection attempts
2 parents c163daf + 1c1fa67 commit 1cdd852

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

splunklib/binding.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import logging
3131
import socket
3232
import ssl
33+
import sys
34+
import time
3335
from base64 import b64encode
3436
from contextlib import contextmanager
3537
from datetime import datetime
@@ -453,6 +455,11 @@ class Context(object):
453455
:type splunkToken: ``string``
454456
:param headers: List of extra HTTP headers to send (optional).
455457
: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)
456463
:param handler: The HTTP request handler (optional).
457464
:returns: A ``Context`` instance.
458465
@@ -470,7 +477,8 @@ class Context(object):
470477
"""
471478
def __init__(self, handler=None, **kwargs):
472479
self.http = HttpLib(handler, kwargs.get("verify", False), key_file=kwargs.get("key_file"),
473-
cert_file=kwargs.get("cert_file"), context=kwargs.get("context")) # Default to False for backward compat
480+
cert_file=kwargs.get("cert_file"), context=kwargs.get("context"), # Default to False for backward compat
481+
retries=kwargs.get("retries", 0), retryBackoff=kwargs.get("retryBackoff", 10))
474482
self.token = kwargs.get("token", _NoAuthenticationToken)
475483
if self.token is None: # In case someone explicitly passes token=None
476484
self.token = _NoAuthenticationToken
@@ -1157,12 +1165,14 @@ class HttpLib(object):
11571165
11581166
If using the default handler, SSL verification can be disabled by passing verify=False.
11591167
"""
1160-
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None, context=None):
1168+
def __init__(self, custom_handler=None, verify=False, key_file=None, cert_file=None, context=None, retries=0, retryBackoff=10):
11611169
if custom_handler is None:
11621170
self.handler = handler(verify=verify, key_file=key_file, cert_file=cert_file, context=context)
11631171
else:
11641172
self.handler = custom_handler
11651173
self._cookies = {}
1174+
self.retries = retries
1175+
self.retryBackoff = retryBackoff
11661176

11671177
def delete(self, url, headers=None, **kwargs):
11681178
"""Sends a DELETE request to a URL.
@@ -1276,7 +1286,16 @@ def request(self, url, message, **kwargs):
12761286
its structure).
12771287
:rtype: ``dict``
12781288
"""
1279-
response = self.handler(url, message, **kwargs)
1289+
while True:
1290+
try:
1291+
response = self.handler(url, message, **kwargs)
1292+
break
1293+
except Exception:
1294+
if self.retries <= 0:
1295+
raise
1296+
else:
1297+
time.sleep(self.retryBackoff)
1298+
self.retries -= 1
12801299
response = record(response)
12811300
if 400 <= response.status:
12821301
raise HTTPError(response)

splunklib/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ def connect(**kwargs):
320320
:type username: ``string``
321321
:param `password`: The password for the Splunk account.
322322
:type password: ``string``
323+
:param retires: Number of retries for each HTTP connection (optional, the default is 0).
324+
NOTE THAT THIS MAY INCREASE THE NUMBER OF ROUND TRIP CONNECTIONS TO THE SPLUNK SERVER.
325+
:type retries: ``int``
326+
:param retryBackoff: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
327+
:type retryBackoff: ``int`` (in seconds)
323328
:param `context`: The SSLContext that can be used when setting verify=True (optional)
324329
:type context: ``SSLContext``
325330
:return: An initialized :class:`Service` connection.
@@ -388,6 +393,11 @@ class Service(_BaseService):
388393
:param `password`: The password, which is used to authenticate the Splunk
389394
instance.
390395
:type password: ``string``
396+
:param retires: Number of retries for each HTTP connection (optional, the default is 0).
397+
NOTE THAT THIS MAY INCREASE THE NUMBER OF ROUND TRIP CONNECTIONS TO THE SPLUNK SERVER.
398+
:type retries: ``int``
399+
:param retryBackoff: How long to wait between connection attempts if `retries` > 0 (optional, defaults to 10s).
400+
:type retryBackoff: ``int`` (in seconds)
391401
:return: A :class:`Service` instance.
392402
393403
**Example**::

0 commit comments

Comments
 (0)