Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@ def __init__(self, installation_store: AsyncInstallationStore):
def logger(self) -> Logger:
return self.underlying.logger

async def async_save(self, installation: Installation):
# Invalidate cache data for update operations
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-"
def _evict_team_cache(self, enterprise_id: Optional[str], team_id: Optional[str]) -> None:
team_key = f"{enterprise_id or ''}-{team_id or ''}"
self.cached_bots.pop(team_key, None)
for key in list(self.cached_installations.keys()):
if key.startswith(key_prefix):
if key.startswith(f"{team_key}-"):
self.cached_installations.pop(key)

async def async_save(self, installation: Installation):
self._evict_team_cache(installation.enterprise_id, installation.team_id)
return await self.underlying.async_save(installation)

async def async_save_bot(self, bot: Bot):
# Invalidate cache data for update operations
key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
self._evict_team_cache(bot.enterprise_id, bot.team_id)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

note: The evict-after-save follow-up from #2051 applies here too: a lookup that lands during this await can cache the old bot again. Fine to keep it for that follow-up and not hold this PR.

return await self.underlying.async_save_bot(bot)

async def async_find_bot(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,19 @@ def __init__(self, installation_store: InstallationStore):
def logger(self) -> Logger:
return self.underlying.logger

def save(self, installation: Installation):
# Invalidate cache data for update operations
key = f"{installation.enterprise_id or ''}-{installation.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
key_prefix = f"{installation.enterprise_id or ''}-{installation.team_id or ''}-"
def _evict_team_cache(self, enterprise_id: Optional[str], team_id: Optional[str]) -> None:
team_key = f"{enterprise_id or ''}-{team_id or ''}"
self.cached_bots.pop(team_key, None)
for key in list(self.cached_installations.keys()):
if key.startswith(key_prefix):
if key.startswith(f"{team_key}-"):
self.cached_installations.pop(key)

def save(self, installation: Installation):
self._evict_team_cache(installation.enterprise_id, installation.team_id)
return self.underlying.save(installation)

def save_bot(self, bot: Bot):
# Invalidate cache data for update operations
key = f"{bot.enterprise_id or ''}-{bot.team_id or ''}"
if key in self.cached_bots:
self.cached_bots.pop(key)
self._evict_team_cache(bot.enterprise_id, bot.team_id)
return self.underlying.save_bot(bot)

def find_bot(
Expand Down
20 changes: 18 additions & 2 deletions tests/slack_sdk/oauth/installation_store/test_simple_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from slack_sdk.oauth.installation_store.sqlite3 import SQLite3InstallationStore


def build_installation(*, user_id: str, bot_token: str) -> Installation:
def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") -> Installation:
return Installation(
app_id="A111",
enterprise_id="E111",
team_id="T111",
team_id=team_id,
user_id=user_id,
bot_id="B111",
bot_token=bot_token,
Expand Down Expand Up @@ -170,3 +170,19 @@ def test_save_evicts_stale_installations(self):
store.save(build_installation(user_id="U222", bot_token="xoxb-u222"))
i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-u222")

def test_save_bot_evicts_stale_installations(self):
base_dir = tempfile.TemporaryDirectory()
self.addCleanup(base_dir.cleanup)
file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222")
store = CacheableInstallationStore(file_store)

store.save(build_installation(user_id="U111", bot_token="xoxb-initial"))
store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
store.save(build_installation(user_id="U111", bot_token="xoxb-other-team", team_id="T1111"))
store.find_installation(enterprise_id="E111", team_id="T1111", user_id="U111")

store.save_bot(build_installation(user_id="U111", bot_token="xoxb-refreshed").to_bot())
i = store.find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-refreshed")

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.

🧪 suggestion (non-blocking): Could we also cache a second workspace (say T222) and assert it survives save_bot()? That pins the trailing - in the prefix, so over-eviction can't slip in.

self.assertIn("E111-T1111-U111", store.cached_installations)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from tests.helpers import async_test


def build_installation(*, user_id: str, bot_token: str) -> Installation:
def build_installation(*, user_id: str, bot_token: str, team_id: str = "T111") -> Installation:
return Installation(
app_id="A111",
enterprise_id="E111",
team_id="T111",
team_id=team_id,
user_id=user_id,
bot_id="B111",
bot_token=bot_token,
Expand Down Expand Up @@ -122,3 +122,20 @@ async def test_save_evicts_stale_installations(self):
await store.async_save(build_installation(user_id="U222", bot_token="xoxb-u222"))
i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-u222")

@async_test
async def test_save_bot_evicts_stale_installations(self):
base_dir = tempfile.TemporaryDirectory()
self.addCleanup(base_dir.cleanup)
file_store = FileInstallationStore(base_dir=base_dir.name, client_id="111.222")
store = AsyncCacheableInstallationStore(file_store)

await store.async_save(build_installation(user_id="U111", bot_token="xoxb-initial"))
await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
await store.async_save(build_installation(user_id="U111", bot_token="xoxb-other-team", team_id="T1111"))
await store.async_find_installation(enterprise_id="E111", team_id="T1111", user_id="U111")

await store.async_save_bot(build_installation(user_id="U111", bot_token="xoxb-refreshed").to_bot())
i = await store.async_find_installation(enterprise_id="E111", team_id="T111", user_id="U111")
self.assertEqual(i.bot_token, "xoxb-refreshed")

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.

🧪 suggestion (non-blocking): Same here, a second cached workspace that survives async_save_bot().

self.assertIn("E111-T1111-U111", store.cached_installations)
Loading