Skip to content

Commit

Permalink
Async Mutex Lock on Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
VVoruganti committed Sep 1, 2023
1 parent 5b7796d commit 46a5aad
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dotenv import load_dotenv

load_dotenv()
CACHE, (THOUGHT_CHANNEL, TOKEN) = init()
CACHE, LOCK, (THOUGHT_CHANNEL, TOKEN) = init()

intents = discord.Intents.default()
intents.messages = True
Expand Down
7 changes: 5 additions & 2 deletions bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import discord
from __main__ import (
CACHE,
LOCK,
THOUGHT_CHANNEL,
)
from discord.ext import commands
Expand Down Expand Up @@ -44,7 +45,8 @@ async def on_message(self, message):
return

# Get cache for conversation
CONVERSATION = CACHE.get_or_create(location_id=str(message.channel.id), user_id=f"discord_{str(message.author.id)}")
async with LOCK:
CONVERSATION = CACHE.get_or_create(location_id=str(message.channel.id), user_id=f"discord_{str(message.author.id)}")

# Get the message content but remove any mentions
inp = message.content.replace(str('<@' + str(self.bot.user.id) + '>'), '')
Expand Down Expand Up @@ -141,7 +143,8 @@ async def restart(self, ctx: discord.ApplicationContext, respond: Optional[bool]
Args:
ctx: context, necessary for bot commands
"""
CONVERSATION = CACHE.get_or_create(location_id=str(ctx.channel_id), user_id=f"discord_{str(ctx.author.id)}", restart=True)
async with LOCK:
CONVERSATION = CACHE.get_or_create(location_id=str(ctx.channel_id), user_id=f"discord_{str(ctx.author.id)}", restart=True)

if respond:
msg = "Great! The conversation has been restarted. What would you like to talk about?"
Expand Down
7 changes: 4 additions & 3 deletions common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from agent.cache import LRUCache, LayeredLRUCache
from agent.chain import BloomChain
from agent.mediator import SupabaseMediator
import asyncio


def init():
THOUGHT_CHANNEL = os.environ["THOUGHT_CHANNEL_ID"]
TOKEN = os.environ['BOT_TOKEN']
# MEDIATOR = PostgresChatMessageHistoryMediator()
MEDIATOR = SupabaseMediator()
CACHE = LayeredLRUCache(50, MEDIATOR) # Support 1000 concurrent active conversations cached in memory
CACHE = LayeredLRUCache(50, MEDIATOR) # Support 50 concurrent active conversations cached in memory
LOCK = asyncio.Lock()

return CACHE, (THOUGHT_CHANNEL, TOKEN)
return CACHE, LOCK, (THOUGHT_CHANNEL, TOKEN)
2 changes: 1 addition & 1 deletion www/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import uuid

load_dotenv()
CACHE, _ = init()
CACHE, LOCK, _ = init()

st.set_page_config(
page_title="Bloom - Learning. Reimagined.",
Expand Down

0 comments on commit 46a5aad

Please sign in to comment.