Skip to content

[CHANGE] Reuse the same request session on same instance - #110

Open
pablo-estevez-lrn wants to merge 4 commits into
masterfrom
reuse-same-request-session
Open

pablo-estevez-lrn wants to merge 4 commits into
masterfrom
reuse-same-request-session

Conversation

@pablo-estevez-lrn

@pablo-estevez-lrn pablo-estevez-lrn commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor
  • Reuse the same request session on the same Data API instance
  • Added a close() public method to close the session

Checklist

  • Feature

  • Bug

  • Security

  • Documentation

  • ChangeLog.md updated

  • Tests added

  • All testsuites passed

  • make dist completed successfully

  • Bump package version if release coming

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Session cleanup needs a deterministic public API, and the reuse behavior lacks regression coverage.

3 open findings
What changed in this PR

Reuses one HTTP session per DataApi instance to enable connection pooling.

Changes:

  • Adds session creation and cleanup.
  • Routes requests through the shared session.
  • Updates the changelog.
File Description
learnosity_sdk/​request/​dataapi.py Introduces and uses a shared request session.
ChangeLog.md Records session reuse.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread learnosity_sdk/request/dataapi.py Outdated
Comment thread ChangeLog.md Outdated
Comment thread learnosity_sdk/request/dataapi.py

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Core behavior is correctly tested; only a minor public-API documentation clarification remains.

1 open finding
3 resolved since last review

🧠 Review effort: Balanced

Comment thread learnosity_sdk/request/dataapi.py
@pablo-estevez-lrn pablo-estevez-lrn changed the title [CHANGE] Eeuse the same request session on same instance [CHANGE] Reuse the same request session on same instance Oct 13, 2026
Add docstring to the close method for clarity.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@michael-linnane-lrn michael-linnane-lrn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pablo-estevez-lrn, two comments. We need to be careful when altering existing classes since we don't know how the integrations might be used already. Should be easy to fix with backwards compatibility though

Comment on lines +66 to +68
session = getattr(self, '_session', None)
if session is not None:
session.close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Question: Why allow later requests? Would we not just fully drop it and have an actual blank slate?

Suggested change
session = getattr(self, '_session', None)
if session is not None:
session.close()
if getattr(self, '_session', None) is not None:
self._session.close()
self._session = None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 reasons:

  1. To avoid an invalid state after close(), when we raise an error None does not have an attribute post. (after the last commit, will be creating a new session instead)
  2. To use the same known semantic that requests, where close is closing the transport, but not converting to a blank slate.
    If you want to destroy the object, you can always use del()

it will be functionally equivalent, so happy to change if you want

Comment on lines +15 to +16
def __init__(self) -> None:
self._session = requests.Session()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ Pressing: Adding __init__ here makes super().__init__() mandatory for subclasses. DataApi previously inherited object.__init__, and since we could have customers that don't have a version pinned and did the above we would actually break their integration. We might need to make it Optional

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, if _session is not an attr when we try to do the request, we can create the request. Fixed on f2b5117

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants