Skip to content

Commit

Permalink
refactor: add cache for NATS log subject calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik committed Jun 11, 2024
1 parent 56dffa9 commit 77d5560
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 6 additions & 4 deletions faststream/nats/subscriber/usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,12 @@ def add_prefix(self, prefix: str) -> None:
for subject in (self.config.filter_subjects or ())
]

@cached_property
def _resolved_subject_string(self) -> str:
return self.subject or ", ".join(self.config.filter_subjects or ())

def __hash__(self) -> int:
return self.get_routing_hash(
self.subject or "".join(self.config.filter_subjects or ())
)
return self.get_routing_hash(self._resolved_subject_string)

@staticmethod
def get_routing_hash(
Expand Down Expand Up @@ -558,7 +560,7 @@ def get_log_context(
"""Log context factory using in `self.consume` scope."""
return self.build_log_context(
message=message,
subject=self.subject or ", ".join(self.config.filter_subjects or ()),
subject=self._resolved_subject_string,
queue=self.queue,
stream=self.stream.name,
)
Expand Down
1 change: 0 additions & 1 deletion tests/brokers/nats/test_consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def subscriber(m):
await br.start()
await asyncio.wait(
(
asyncio.create_task(br.publish(1, f"{queue}.b")),
asyncio.create_task(br.publish(2, f"{queue}.a")),
asyncio.create_task(event.wait()),
),
Expand Down

0 comments on commit 77d5560

Please sign in to comment.