-
Notifications
You must be signed in to change notification settings - Fork 80
feat: renew session for expiration, re-execute when error #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ba27e4c
76cfe92
b442a55
e583412
b0c5aca
f89f964
adf40f0
72b5307
9091cc8
e59f07a
03db353
54ba2c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from nebula3.Exception import ( | ||
IOErrorException, | ||
NotValidConnectionException, | ||
ExecutionErrorException, | ||
) | ||
|
||
from nebula3.data.ResultSet import ResultSet | ||
|
@@ -18,14 +19,34 @@ | |
|
||
|
||
class Session(object): | ||
def __init__(self, connection, auth_result: AuthResult, pool, retry_connect=True): | ||
def __init__( | ||
self, | ||
connection, | ||
auth_result: AuthResult, | ||
pool, | ||
retry_connect=True, | ||
retry_times=3, | ||
retry_interval_sec=1, | ||
): | ||
""" | ||
Initialize the Session object. | ||
|
||
:param connection: The connection object associated with the session. | ||
:param auth_result: The result of the authentication process. | ||
:param pool: The pool object where the session was created. | ||
:param retry_connect: A boolean indicating whether to retry the connection if it fails. | ||
:param retry_times: The number of times to retry the connection. | ||
:param retry_interval_sec: The interval between connection retries in seconds. | ||
""" | ||
self._session_id = auth_result.get_session_id() | ||
self._timezone_offset = auth_result.get_timezone_offset() | ||
self._connection = connection | ||
self._timezone = 0 | ||
# connection the where the session was created, if session pool was used | ||
self._pool = pool | ||
self._retry_connect = retry_connect | ||
self._retry_times = retry_times | ||
self._retry_interval_sec = retry_interval_sec | ||
# the time stamp when the session was added to the idle list of the session pool | ||
self._idle_time_start = 0 | ||
|
||
|
@@ -65,6 +86,27 @@ def execute_parameter(self, stmt, params): | |
timezone_offset=self._timezone_offset, | ||
) | ||
raise | ||
except ExecutionErrorException as eee: | ||
retry_count = 0 | ||
while retry_count < self._retry_times: | ||
try: | ||
# TODO: add exponential backoff | ||
time.sleep(self._retry_interval_sec) | ||
resp = self._connection.execute_parameter( | ||
self._session_id, stmt, params | ||
) | ||
end_time = time.time() | ||
return ResultSet( | ||
resp, | ||
all_latency=int((end_time - start_time) * 1000000), | ||
timezone_offset=self._timezone_offset, | ||
) | ||
except ExecutionErrorException: | ||
if retry_count >= self._retry_times - 1: | ||
raise eee | ||
else: | ||
retry_count += 1 | ||
continue | ||
except Exception: | ||
raise | ||
|
||
|
@@ -222,6 +264,22 @@ def execute_json_with_parameter(self, stmt, params): | |
) | ||
return resp_json | ||
raise | ||
except ExecutionErrorException as eee: | ||
retry_count = 0 | ||
while retry_count < self._retry_times: | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sleep before retry execution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! |
||
# TODO: add exponential backoff | ||
time.sleep(self._retry_interval_sec) | ||
resp = self._connection.execute_json_with_parameter( | ||
self._session_id, stmt, params | ||
) | ||
return resp | ||
except ExecutionErrorException: | ||
if retry_count >= self._retry_times - 1: | ||
raise eee | ||
else: | ||
retry_count += 1 | ||
continue | ||
except Exception: | ||
raise | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.