Skip to content

Commit

Permalink
feat: limit client message cache to 1k per channel
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Apr 27, 2024
1 parent a5efdac commit 421d05d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/dumdum/client/chat_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, app: TkApp):
self.grid_rowconfigure(0, weight=1)

self.channels: list[Channel] = []
self.message_cache = MessageCache()
self.message_cache = MessageCache(max_messages=1000)

self.channel_list = ChannelList(self)
self.channel_list.grid(row=0, column=0, rowspan=2, padx=(0, 10), sticky="nesw")
Expand Down Expand Up @@ -214,17 +214,23 @@ def wrap_to_width(self, width: int) -> None:


class MessageCache:
def __init__(self) -> None:
self.channel_messages: dict[str, list[Message]] = collections.defaultdict(list)
_channel_messages: dict[str, collections.deque[Message]]

def __init__(self, *, max_messages: int) -> None:
self.max_messages = max_messages
self._channel_messages = collections.defaultdict(self._create_message_queue)

def add_message(self, message: Message) -> None:
self.channel_messages[message.channel_name].append(message)
self._channel_messages[message.channel_name].append(message)

def get_messages(self, channel: Channel) -> list[Message]:
messages = self.channel_messages.get(channel.name)
messages = self._channel_messages.get(channel.name)
if messages is None:
return []
return messages.copy()
return list(messages)

def _create_message_queue(self) -> collections.deque[Message]:
return collections.deque(maxlen=self.max_messages)


class SendBox(Frame):
Expand Down

0 comments on commit 421d05d

Please sign in to comment.