Skip to content

Commit 441f017

Browse files
committed
Merge pull request splunk#130 from splunk/bugfix/handle-keep-alive-connections
Add better handling for Keep-Alive connections
2 parents d9249bc + 836ce8a commit 441f017

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

splunklib/binding.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,9 @@ class ResponseReader(io.RawIOBase):
12351235
# For testing, you can use a StringIO as the argument to
12361236
# ``ResponseReader`` instead of an ``httplib.HTTPResponse``. It
12371237
# will work equally well.
1238-
def __init__(self, response):
1238+
def __init__(self, response, connection=None):
12391239
self._response = response
1240+
self._connection = connection
12401241
self._buffer = ''
12411242

12421243
def __str__(self):
@@ -1262,6 +1263,8 @@ def peek(self, size):
12621263

12631264
def close(self):
12641265
"""Closes this response."""
1266+
if _connection:
1267+
_connection.close()
12651268
self._response.close()
12661269

12671270
def read(self, size = None):
@@ -1332,25 +1335,29 @@ def request(url, message, **kwargs):
13321335
"Host": host,
13331336
"User-Agent": "splunk-sdk-python/1.5.0",
13341337
"Accept": "*/*",
1338+
"Connection": "Close",
13351339
} # defaults
13361340
for key, value in message["headers"]:
13371341
head[key] = value
13381342
method = message.get("method", "GET")
13391343

13401344
connection = connect(scheme, host, port)
1345+
is_keepalive = False
13411346
try:
13421347
connection.request(method, path, body, head)
13431348
if timeout is not None:
13441349
connection.sock.settimeout(timeout)
13451350
response = connection.getresponse()
1351+
is_keepalive = "keep-alive" in response.getheader("connection", default="close").lower()
13461352
finally:
1347-
connection.close()
1353+
if not is_keepalive:
1354+
connection.close()
13481355

13491356
return {
13501357
"status": response.status,
13511358
"reason": response.reason,
13521359
"headers": response.getheaders(),
1353-
"body": ResponseReader(response),
1360+
"body": ResponseReader(response, connection if is_keepalive else None),
13541361
}
13551362

13561363
return request

0 commit comments

Comments
 (0)