Skip to content

Commit

Permalink
Use a requests session to limit opened sessions
Browse files Browse the repository at this point in the history
We have some issues with a very high throughput of jobs and Odoo's
anonymous sessions. Every job pushed to Odoo uses a new anonymous
sessions, which creates a tremendous amount of session files.

Using a requests.session means that the session will keep the same
cookie once it receives the first response.

There is 2 problems with this implementation though:

* requests.Session is not proved to be threadsafe [0], but maybe we can
manually retrieve the session id and craft the next messages with it.
Anyway as they are all anonymous, if we lose a session at some point
there is no big deal.
* the main issue is that we don't wait for an answer so until we have a
requests responding before the timeout we won't have any session id

[0] psf/requests#2766
  • Loading branch information
guewen authored and nguyenminhchien committed Nov 24, 2023
1 parent 401ea34 commit 2d08252
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion queue_job/jobrunner/runner.py
Expand Up @@ -176,6 +176,9 @@ def _odoo_now():
return _datetime_to_epoch(dt)


session = requests.Session()


def _async_http_get(scheme, host, port, user, password, db_name, job_uuid):
# Method to set failed job (due to timeout, etc) as pending,
# to avoid keeping it as enqueued.
Expand All @@ -202,7 +205,7 @@ def urlopen():
auth = (user, password)
# we are not interested in the result, so we set a short timeout
# but not too short so we trap and log hard configuration errors
response = requests.get(url, timeout=1, auth=auth)
response = session.get(url, timeout=1, auth=auth)

# raise_for_status will result in either nothing, a Client Error
# for HTTP Response codes between 400 and 500 or a Server Error
Expand Down

0 comments on commit 2d08252

Please sign in to comment.