Skip to content
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

feat: Add PingingPool check on session age #1069

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion google/cloud/spanner_v1/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ class PingingPool(AbstractSessionPool):
:param database_role: (Optional) user-assigned database_role for the session.
"""

SESSION_MAX_AGE = 28 * 24 * 60 * 60

def __init__(
self,
size=10,
Expand Down Expand Up @@ -448,8 +450,9 @@ def get(self, timeout=None):
timeout = self.default_timeout

ping_after, session = self._sessions.get(block=True, timeout=timeout)
session_age = (_NOW() - session._created_at).total_seconds()

if _NOW() > ping_after:
if _NOW() > ping_after or session_age >= self.SESSION_MAX_AGE:
# Using session.exists() guarantees the returned session exists.
# session.ping() uses a cached result in the backend which could
# result in a recently deleted session being returned.
Expand Down Expand Up @@ -481,6 +484,11 @@ def clear(self):
else:
session.delete()

def _new_session(self):
session = super()._new_session()
session._created_at = _NOW()
return session

def ping(self):
"""Refresh maybe-expired sessions in the pool.

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,48 @@ def test_get_hit_w_ping_expired(self):
self.assertTrue(SESSIONS[0]._exists_checked)
self.assertFalse(pool._sessions.full())

def test_get_hit_w_created(self):
import datetime

pool = self._make_one(size=4)
database = _Database("name")
SESSIONS = [_Session(database)] * 4
database._sessions.extend(SESSIONS)
pool.bind(database)

session_max_age = 28 * 24 * 60 * 60
SESSIONS[0]._created_at = datetime.datetime.utcnow() - datetime.timedelta(
seconds=session_max_age + 10
)

session = pool.get()

self.assertIs(session, SESSIONS[0])
self.assertTrue(session._exists_checked)
self.assertFalse(pool._sessions.full())

def test_get_hit_w_created_expired(self):
import datetime

pool = self._make_one(size=4)
database = _Database("name")
SESSIONS = [_Session(database)] * 5
database._sessions.extend(SESSIONS)
pool.bind(database)

session_max_age = 28 * 24 * 60 * 60
SESSIONS[0]._created_at = datetime.datetime.utcnow() - datetime.timedelta(
seconds=session_max_age
)
SESSIONS[0]._exists = False

session = pool.get()

self.assertIs(session, SESSIONS[4])
session.create.assert_called()
self.assertTrue(SESSIONS[0]._exists_checked)
self.assertFalse(pool._sessions.full())

def test_get_empty_default_timeout(self):
import queue

Expand Down