Skip to content

Commit cec08b0

Browse files
authored
Merge pull request #242 from joeltgray/adding-timeout-to-session-object-for-hanging-connections
Adding timeout to session object for hanging connections
2 parents 74624bc + 9a80141 commit cec08b0

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pyArango/connection.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ class AikidoSession:
3434
"""
3535

3636
class Holder(object):
37-
def __init__(self, fct, auth, max_conflict_retries=5, verify=True):
37+
def __init__(self, fct, auth, max_conflict_retries=5, verify=True, timeout=30):
3838
self.fct = fct
3939
self.auth = auth
4040
self.max_conflict_retries = max_conflict_retries
4141
if not isinstance(verify, bool) and not isinstance(verify, CA_Certificate) and not not isinstance(verify, str) :
4242
raise ValueError("'verify' argument can only be of type: bool, CA_Certificate or str ")
4343
self.verify = verify
44+
self.timeout = timeout
4445

4546
def __call__(self, *args, **kwargs):
4647
if self.auth:
@@ -50,10 +51,12 @@ def __call__(self, *args, **kwargs):
5051
else :
5152
kwargs["verify"] = self.verify
5253

54+
kwargs["timeout"] = self.timeout
55+
5356
try:
5457
do_retry = True
5558
retry = 0
56-
while do_retry and retry < self.max_conflict_retries :
59+
while do_retry and retry < self.max_conflict_retries:
5760
ret = self.fct(*args, **kwargs)
5861
do_retry = ret.status_code == 1200
5962
try :
@@ -84,7 +87,8 @@ def __init__(
8487
max_retries=5,
8588
single_session=True,
8689
log_requests=False,
87-
pool_maxsize=10
90+
pool_maxsize=10,
91+
timeout=30,
8892
):
8993
if username:
9094
self.auth = (username, password)
@@ -95,6 +99,7 @@ def __init__(
9599
self.max_retries = max_retries
96100
self.log_requests = log_requests
97101
self.max_conflict_retries = max_conflict_retries
102+
self.timeout = timeout
98103

99104
self.session = None
100105
if single_session:
@@ -133,12 +138,13 @@ def __getattr__(self, request_function_name):
133138

134139
auth = object.__getattribute__(self, "auth")
135140
verify = object.__getattribute__(self, "verify")
141+
timeout = object.__getattribute__(self, "timeout")
136142
if self.log_requests:
137143
log = object.__getattribute__(self, "log")
138144
log["nb_request"] += 1
139145
log["requests"][request_function.__name__] += 1
140146

141-
return AikidoSession.Holder(request_function, auth, max_conflict_retries=self.max_conflict_retries, verify=verify)
147+
return AikidoSession.Holder(request_function, auth, max_conflict_retries=self.max_conflict_retries, verify=verify, timeout=timeout)
142148

143149
def disconnect(self):
144150
pass
@@ -180,6 +186,8 @@ class Connection(object):
180186
max number of requests for a conflict error (1200 arangodb error). Does not work with gevents (grequests),
181187
pool_maxsize: int
182188
max number of open connections. (Not intended for grequest)
189+
timeout: int
190+
number of seconds to wait on a hanging connection before giving up
183191
"""
184192

185193
LOAD_BLANCING_METHODS = {'round-robin', 'random'}
@@ -199,7 +207,8 @@ def __init__(
199207
use_lock_for_reseting_jwt=True,
200208
max_retries=5,
201209
max_conflict_retries=5,
202-
pool_maxsize=10
210+
pool_maxsize=10,
211+
timeout=30
203212
):
204213

205214
if loadBalancing not in Connection.LOAD_BLANCING_METHODS:
@@ -215,6 +224,7 @@ def __init__(
215224
self.max_retries = max_retries
216225
self.max_conflict_retries = max_conflict_retries
217226
self.action = ConnectionAction(self)
227+
self.timeout = timeout
218228

219229
self.databases = {}
220230
self.verbose = verbose
@@ -295,7 +305,8 @@ def create_aikido_session(
295305
max_conflict_retries=self.max_conflict_retries,
296306
max_retries=self.max_retries,
297307
log_requests=False,
298-
pool_maxsize=self.pool_maxsize
308+
pool_maxsize=self.pool_maxsize,
309+
timeout=self.timeout
299310
)
300311

301312
def create_grequest_session(

pyArango/tests/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest, copy
22
import os
3+
from unittest.mock import MagicMock, patch
34

45
from pyArango.connection import *
56
from pyArango.database import *
@@ -1072,7 +1073,14 @@ def test_tasks(self):
10721073
db_tasks.delete(task_id)
10731074
self.assertListEqual(db_tasks(), [])
10741075

1076+
def test_timeout_parameter(self):
1077+
# Create a Connection object with the desired timeout
1078+
timeout = 120
1079+
connection = Connection(arangoURL=ARANGODB_URL, username=ARANGODB_ROOT_USERNAME, password=ARANGODB_ROOT_PASSWORD, timeout=timeout)
10751080

1081+
# Verify that the Connection session was created with the correct timeout
1082+
assert connection.session.timeout == timeout
1083+
10761084
if __name__ == "__main__":
10771085
# Change default username/password in bash like this:
10781086
# export ARANGODB_ROOT_USERNAME=myUserName

0 commit comments

Comments
 (0)