From fa9130db0a4d5a9d9c87f91602552159e9af7e74 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Wed, 29 May 2024 16:42:38 +0000 Subject: [PATCH 01/11] update adding nuid usage as well as reply_to_prefix --- faststream/nats/publisher/producer.py | 5 +- faststream/redis/publisher/producer.py | 5 +- faststream/utils/nuid.py | 70 ++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 faststream/utils/nuid.py diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index 5f08774651..f3a1947b9b 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -134,6 +134,7 @@ async def publish( # type: ignore[override] stream: Optional[str] = None, timeout: Optional[float] = None, rpc: bool = False, + reply_to_prefix: str = "" rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, ) -> Optional[Any]: @@ -148,8 +149,8 @@ async def publish( # type: ignore[override] if rpc: if reply_to: raise WRONG_PUBLISH_ARGS - - reply_to = str(uuid4()) + nuid = NUID() + reply_to = f"{reply_to_prefix}{str(nuid.next(),"utf-8")})" future: asyncio.Future[Msg] = asyncio.Future() sub = await self._connection._nc.subscribe( reply_to, future=future, max_msgs=1 diff --git a/faststream/redis/publisher/producer.py b/faststream/redis/publisher/producer.py index ce807aeab8..6656222f1f 100644 --- a/faststream/redis/publisher/producer.py +++ b/faststream/redis/publisher/producer.py @@ -56,6 +56,7 @@ async def publish( # type: ignore[override] maxlen: Optional[int] = None, headers: Optional["AnyDict"] = None, reply_to: str = "", + reply_to_prefix: str = "" rpc: bool = False, rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, @@ -67,8 +68,8 @@ async def publish( # type: ignore[override] if rpc: if reply_to: raise WRONG_PUBLISH_ARGS - - reply_to = str(uuid4()) + nuid = NUID() + reply_to = f"{reply_to_prefix}{str(nuid.next(),"utf-8")})" psub = self._connection.pubsub() await psub.subscribe(reply_to) diff --git a/faststream/utils/nuid.py b/faststream/utils/nuid.py new file mode 100644 index 0000000000..76c300dd75 --- /dev/null +++ b/faststream/utils/nuid.py @@ -0,0 +1,70 @@ +# Copyright 2016-2018 The NATS Authors +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from __future__ import annotations + +from random import Random +from secrets import randbelow, token_bytes +from sys import maxsize as MaxInt + +DIGITS = b'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' +BASE = 62 +PREFIX_LENGTH = 12 +SEQ_LENGTH = 10 +MAX_SEQ = 839299365868340224 # BASE**10 +MIN_INC = 33 +MAX_INC = 333 +INC = MAX_INC - MIN_INC +TOTAL_LENGTH = PREFIX_LENGTH + SEQ_LENGTH + + +class NUID: + """ + NUID is an implementation of the approach for fast generation of + unique identifiers used for inboxes in NATS. + """ + + def __init__(self) -> None: + self._prand = Random(randbelow(MaxInt)) + self._seq = self._prand.randint(0, MAX_SEQ) + self._inc = MIN_INC + self._prand.randint(BASE + 1, INC) + self._prefix = bytearray() + self.randomize_prefix() + + def next(self) -> bytearray: + """ + next returns the next unique identifier. + """ + self._seq += self._inc + if self._seq >= MAX_SEQ: + self.randomize_prefix() + self.reset_sequential() + + l = self._seq + prefix = self._prefix[:] + suffix = bytearray(SEQ_LENGTH) + for i in reversed(range(SEQ_LENGTH)): + suffix[i] = DIGITS[int(l) % BASE] + l //= BASE + + prefix.extend(suffix) + return prefix + + def randomize_prefix(self) -> None: + random_bytes = token_bytes(PREFIX_LENGTH) + self._prefix = bytearray(DIGITS[c % BASE] for c in random_bytes) + + def reset_sequential(self) -> None: + self._seq = self._prand.randint(0, MAX_SEQ) + self._inc = MIN_INC + self._prand.randint(0, INC) \ No newline at end of file From ae3cee69b8948924245c424b8ce3751e60cafcc6 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Wed, 29 May 2024 16:48:50 +0000 Subject: [PATCH 02/11] add missing commas --- faststream/nats/publisher/producer.py | 2 +- faststream/redis/publisher/producer.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index f3a1947b9b..aa58caebda 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -134,7 +134,7 @@ async def publish( # type: ignore[override] stream: Optional[str] = None, timeout: Optional[float] = None, rpc: bool = False, - reply_to_prefix: str = "" + reply_to_prefix: str = "", rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, ) -> Optional[Any]: diff --git a/faststream/redis/publisher/producer.py b/faststream/redis/publisher/producer.py index 6656222f1f..239790cde8 100644 --- a/faststream/redis/publisher/producer.py +++ b/faststream/redis/publisher/producer.py @@ -56,7 +56,7 @@ async def publish( # type: ignore[override] maxlen: Optional[int] = None, headers: Optional["AnyDict"] = None, reply_to: str = "", - reply_to_prefix: str = "" + reply_to_prefix: str = "", rpc: bool = False, rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, From 81b754ffd426a34dba3355fd9b4e79eb0035b00a Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Wed, 29 May 2024 16:50:51 +0000 Subject: [PATCH 03/11] update reply_to nuid generation, add missing imports --- faststream/nats/publisher/producer.py | 4 +++- faststream/redis/publisher/producer.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index aa58caebda..9a46d62024 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Any, Dict, Optional from uuid import uuid4 +from faststream.utils.nuid import NUID import nats from typing_extensions import override @@ -150,7 +151,8 @@ async def publish( # type: ignore[override] if reply_to: raise WRONG_PUBLISH_ARGS nuid = NUID() - reply_to = f"{reply_to_prefix}{str(nuid.next(),"utf-8")})" + rpc_nuid = str(nuid.next(),"utf-8") + reply_to = f"{reply_to_prefix}{rpc_nuid}" future: asyncio.Future[Msg] = asyncio.Future() sub = await self._connection._nc.subscribe( reply_to, future=future, max_msgs=1 diff --git a/faststream/redis/publisher/producer.py b/faststream/redis/publisher/producer.py index 239790cde8..13c9e4aaca 100644 --- a/faststream/redis/publisher/producer.py +++ b/faststream/redis/publisher/producer.py @@ -10,6 +10,7 @@ from faststream.redis.parser import RawMessage, RedisPubSubParser from faststream.redis.schemas import INCORRECT_SETUP_MSG from faststream.utils.functions import timeout_scope +from faststream.utils.nuid import NUID if TYPE_CHECKING: from redis.asyncio.client import PubSub, Redis @@ -69,7 +70,8 @@ async def publish( # type: ignore[override] if reply_to: raise WRONG_PUBLISH_ARGS nuid = NUID() - reply_to = f"{reply_to_prefix}{str(nuid.next(),"utf-8")})" + rpc_nuid = str(nuid.next(),"utf-8") + reply_to = f"{reply_to_prefix}{rpc_nuid}" psub = self._connection.pubsub() await psub.subscribe(reply_to) From 94855b7252d8f4f33338a757f6052fc90944b553 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Wed, 29 May 2024 16:52:48 +0000 Subject: [PATCH 04/11] rename reply_to_prefix to rpc_prefix --- faststream/nats/publisher/producer.py | 4 ++-- faststream/redis/publisher/producer.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index 9a46d62024..3f71bfa9c4 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -135,7 +135,7 @@ async def publish( # type: ignore[override] stream: Optional[str] = None, timeout: Optional[float] = None, rpc: bool = False, - reply_to_prefix: str = "", + rpc_prefix: str = "", rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, ) -> Optional[Any]: @@ -152,7 +152,7 @@ async def publish( # type: ignore[override] raise WRONG_PUBLISH_ARGS nuid = NUID() rpc_nuid = str(nuid.next(),"utf-8") - reply_to = f"{reply_to_prefix}{rpc_nuid}" + reply_to = f"{rpc_prefix}{rpc_nuid}" future: asyncio.Future[Msg] = asyncio.Future() sub = await self._connection._nc.subscribe( reply_to, future=future, max_msgs=1 diff --git a/faststream/redis/publisher/producer.py b/faststream/redis/publisher/producer.py index 13c9e4aaca..ffd8e18323 100644 --- a/faststream/redis/publisher/producer.py +++ b/faststream/redis/publisher/producer.py @@ -57,7 +57,7 @@ async def publish( # type: ignore[override] maxlen: Optional[int] = None, headers: Optional["AnyDict"] = None, reply_to: str = "", - reply_to_prefix: str = "", + rpc_prefix: str = "", rpc: bool = False, rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, @@ -71,7 +71,7 @@ async def publish( # type: ignore[override] raise WRONG_PUBLISH_ARGS nuid = NUID() rpc_nuid = str(nuid.next(),"utf-8") - reply_to = f"{reply_to_prefix}{rpc_nuid}" + reply_to = f"{rpc_prefix}{rpc_nuid}" psub = self._connection.pubsub() await psub.subscribe(reply_to) From 3dcf57616562248e221ff08bf8f9da8f1deb3f25 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Wed, 29 May 2024 19:44:12 +0000 Subject: [PATCH 05/11] refactor rpc reply_to address generation. --- faststream/nats/publisher/producer.py | 5 ++--- faststream/redis/publisher/producer.py | 3 +-- faststream/utils/nuid.py | 25 ++++++++++++------------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index 3f71bfa9c4..45ec369752 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -1,9 +1,7 @@ import asyncio from secrets import token_hex from typing import TYPE_CHECKING, Any, Dict, Optional -from uuid import uuid4 -from faststream.utils.nuid import NUID import nats from typing_extensions import override @@ -13,6 +11,7 @@ from faststream.exceptions import WRONG_PUBLISH_ARGS from faststream.nats.parser import NatsParser from faststream.utils.functions import timeout_scope +from faststream.utils.nuid import NUID if TYPE_CHECKING: from nats.aio.client import Client @@ -151,7 +150,7 @@ async def publish( # type: ignore[override] if reply_to: raise WRONG_PUBLISH_ARGS nuid = NUID() - rpc_nuid = str(nuid.next(),"utf-8") + rpc_nuid = str(nuid.next(), "utf-8") reply_to = f"{rpc_prefix}{rpc_nuid}" future: asyncio.Future[Msg] = asyncio.Future() sub = await self._connection._nc.subscribe( diff --git a/faststream/redis/publisher/producer.py b/faststream/redis/publisher/producer.py index ffd8e18323..658aef2e49 100644 --- a/faststream/redis/publisher/producer.py +++ b/faststream/redis/publisher/producer.py @@ -1,5 +1,4 @@ from typing import TYPE_CHECKING, Any, Optional -from uuid import uuid4 from typing_extensions import override @@ -70,7 +69,7 @@ async def publish( # type: ignore[override] if reply_to: raise WRONG_PUBLISH_ARGS nuid = NUID() - rpc_nuid = str(nuid.next(),"utf-8") + rpc_nuid = str(nuid.next(), "utf-8") reply_to = f"{rpc_prefix}{rpc_nuid}" psub = self._connection.pubsub() await psub.subscribe(reply_to) diff --git a/faststream/utils/nuid.py b/faststream/utils/nuid.py index 76c300dd75..a688812e60 100644 --- a/faststream/utils/nuid.py +++ b/faststream/utils/nuid.py @@ -16,9 +16,9 @@ from random import Random from secrets import randbelow, token_bytes -from sys import maxsize as MaxInt +from sys import maxsize as max_int -DIGITS = b'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' +DIGITS = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" BASE = 62 PREFIX_LENGTH = 12 SEQ_LENGTH = 10 @@ -30,33 +30,32 @@ class NUID: - """ - NUID is an implementation of the approach for fast generation of - unique identifiers used for inboxes in NATS. + """NUID created is a utility to create a new id. + + NUID is an implementation of the approach for fast generation + of unique identifiers used for inboxes in NATS. """ def __init__(self) -> None: - self._prand = Random(randbelow(MaxInt)) + self._prand = Random(randbelow(max_int)) self._seq = self._prand.randint(0, MAX_SEQ) self._inc = MIN_INC + self._prand.randint(BASE + 1, INC) self._prefix = bytearray() self.randomize_prefix() def next(self) -> bytearray: - """ - next returns the next unique identifier. - """ + """Next returns the next unique identifier.""" self._seq += self._inc if self._seq >= MAX_SEQ: self.randomize_prefix() self.reset_sequential() - l = self._seq + l_seq = self._seq prefix = self._prefix[:] suffix = bytearray(SEQ_LENGTH) for i in reversed(range(SEQ_LENGTH)): - suffix[i] = DIGITS[int(l) % BASE] - l //= BASE + suffix[i] = DIGITS[int(l_seq) % BASE] + l_seq //= BASE prefix.extend(suffix) return prefix @@ -67,4 +66,4 @@ def randomize_prefix(self) -> None: def reset_sequential(self) -> None: self._seq = self._prand.randint(0, MAX_SEQ) - self._inc = MIN_INC + self._prand.randint(0, INC) \ No newline at end of file + self._inc = MIN_INC + self._prand.randint(0, INC) From 91f2ba65cc4f88fc7f5b01c187f316124135a6f5 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Fri, 31 May 2024 15:43:20 +0000 Subject: [PATCH 06/11] remove reply_to, update nats implentation --- faststream/nats/publisher/producer.py | 6 ++---- faststream/redis/publisher/producer.py | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index 45ec369752..2b8a01829d 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -52,7 +52,6 @@ async def publish( # type: ignore[override] *, correlation_id: str, headers: Optional[Dict[str, str]] = None, - reply_to: str = "", rpc: bool = False, rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, @@ -71,9 +70,8 @@ async def publish( # type: ignore[override] if reply_to: raise WRONG_PUBLISH_ARGS - token = client._nuid.next() - token.extend(token_hex(2).encode()) - reply_to = token.decode() + reply_to = client.new_inbox() + future: asyncio.Future[Msg] = asyncio.Future() sub = await client.subscribe(reply_to, future=future, max_msgs=1) diff --git a/faststream/redis/publisher/producer.py b/faststream/redis/publisher/producer.py index 658aef2e49..1fb31d07a1 100644 --- a/faststream/redis/publisher/producer.py +++ b/faststream/redis/publisher/producer.py @@ -56,7 +56,6 @@ async def publish( # type: ignore[override] maxlen: Optional[int] = None, headers: Optional["AnyDict"] = None, reply_to: str = "", - rpc_prefix: str = "", rpc: bool = False, rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, @@ -70,7 +69,7 @@ async def publish( # type: ignore[override] raise WRONG_PUBLISH_ARGS nuid = NUID() rpc_nuid = str(nuid.next(), "utf-8") - reply_to = f"{rpc_prefix}{rpc_nuid}" + reply_to = rpc_nuid psub = self._connection.pubsub() await psub.subscribe(reply_to) From 657e40a85dbbacd7252a3f73cf51d8eda7713789 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Fri, 31 May 2024 15:46:26 +0000 Subject: [PATCH 07/11] add back reply_to --- faststream/nats/publisher/producer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index 2b8a01829d..d03d87d3de 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -52,6 +52,7 @@ async def publish( # type: ignore[override] *, correlation_id: str, headers: Optional[Dict[str, str]] = None, + reply_to: str = "", rpc: bool = False, rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, From 586372e2cf3caa975555614ce161fe01cc537374 Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Fri, 31 May 2024 15:52:40 +0000 Subject: [PATCH 08/11] remove rpc_prefix, update NatsJSFastProducer --- faststream/nats/publisher/producer.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index d03d87d3de..b27636d57e 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -73,7 +73,6 @@ async def publish( # type: ignore[override] reply_to = client.new_inbox() - future: asyncio.Future[Msg] = asyncio.Future() sub = await client.subscribe(reply_to, future=future, max_msgs=1) await sub.unsubscribe(limit=1) @@ -133,7 +132,6 @@ async def publish( # type: ignore[override] stream: Optional[str] = None, timeout: Optional[float] = None, rpc: bool = False, - rpc_prefix: str = "", rpc_timeout: Optional[float] = 30.0, raise_timeout: bool = False, ) -> Optional[Any]: @@ -148,9 +146,7 @@ async def publish( # type: ignore[override] if rpc: if reply_to: raise WRONG_PUBLISH_ARGS - nuid = NUID() - rpc_nuid = str(nuid.next(), "utf-8") - reply_to = f"{rpc_prefix}{rpc_nuid}" + reply_to = self._connection._nc.new_inbox() future: asyncio.Future[Msg] = asyncio.Future() sub = await self._connection._nc.subscribe( reply_to, future=future, max_msgs=1 From ce04176195d08a44a174aada4e4d8bb3ab35adaa Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:30:17 +0000 Subject: [PATCH 09/11] Add nats client inbox_prefix test --- tests/brokers/nats/test_test_client.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/brokers/nats/test_test_client.py b/tests/brokers/nats/test_test_client.py index ebbd1c7887..0d80638398 100644 --- a/tests/brokers/nats/test_test_client.py +++ b/tests/brokers/nats/test_test_client.py @@ -80,6 +80,18 @@ def subscriber(m): assert event.is_set() + @pytest.mark.nats() + async def test_inbox_prefix_with_real( + self, + queue: str, + ): + broker = NatsBroker(inbox_prefix="test") + + async with TestNatsBroker(broker, with_real=True) as br: + assert br._connection._inbox_prefix == "test" + assert "test" in str(br._connection.new_inbox) + + async def test_respect_middleware(self, queue): routes = [] From 8dd2b2673e3eb3c523697c15a5b9064b6ddd3c2d Mon Sep 17 00:00:00 2001 From: akardasz <34469992+aKardasz@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:37:26 +0000 Subject: [PATCH 10/11] fixes to inbox_prefix test --- tests/brokers/nats/test_test_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/brokers/nats/test_test_client.py b/tests/brokers/nats/test_test_client.py index 0d80638398..30613bc429 100644 --- a/tests/brokers/nats/test_test_client.py +++ b/tests/brokers/nats/test_test_client.py @@ -88,8 +88,8 @@ async def test_inbox_prefix_with_real( broker = NatsBroker(inbox_prefix="test") async with TestNatsBroker(broker, with_real=True) as br: - assert br._connection._inbox_prefix == "test" - assert "test" in str(br._connection.new_inbox) + assert br._connection._inbox_prefix == b'test' + assert "test" in str(br._connection.new_inbox()) async def test_respect_middleware(self, queue): From a9f678177e2b07fe364f094b8f2d8f22482f3720 Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Mon, 17 Jun 2024 22:00:55 +0300 Subject: [PATCH 11/11] lint: fix bandit --- docs/docs/SUMMARY.md | 2 ++ docs/docs/en/api/faststream/utils/nuid/NUID.md | 11 +++++++++++ faststream/nats/publisher/producer.py | 2 -- faststream/utils/nuid.py | 3 +-- tests/brokers/nats/test_test_client.py | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 docs/docs/en/api/faststream/utils/nuid/NUID.md diff --git a/docs/docs/SUMMARY.md b/docs/docs/SUMMARY.md index e71b34d952..92a04da868 100644 --- a/docs/docs/SUMMARY.md +++ b/docs/docs/SUMMARY.md @@ -1007,6 +1007,8 @@ search: - [to_async](api/faststream/utils/functions/to_async.md) - no_cast - [NoCast](api/faststream/utils/no_cast/NoCast.md) + - nuid + - [NUID](api/faststream/utils/nuid/NUID.md) - path - [compile_path](api/faststream/utils/path/compile_path.md) - Contributing diff --git a/docs/docs/en/api/faststream/utils/nuid/NUID.md b/docs/docs/en/api/faststream/utils/nuid/NUID.md new file mode 100644 index 0000000000..4e43844efe --- /dev/null +++ b/docs/docs/en/api/faststream/utils/nuid/NUID.md @@ -0,0 +1,11 @@ +--- +# 0.5 - API +# 2 - Release +# 3 - Contributing +# 5 - Template Page +# 10 - Default +search: + boost: 0.5 +--- + +::: faststream.utils.nuid.NUID diff --git a/faststream/nats/publisher/producer.py b/faststream/nats/publisher/producer.py index b27636d57e..a7b2a82702 100644 --- a/faststream/nats/publisher/producer.py +++ b/faststream/nats/publisher/producer.py @@ -1,5 +1,4 @@ import asyncio -from secrets import token_hex from typing import TYPE_CHECKING, Any, Dict, Optional import nats @@ -11,7 +10,6 @@ from faststream.exceptions import WRONG_PUBLISH_ARGS from faststream.nats.parser import NatsParser from faststream.utils.functions import timeout_scope -from faststream.utils.nuid import NUID if TYPE_CHECKING: from nats.aio.client import Client diff --git a/faststream/utils/nuid.py b/faststream/utils/nuid.py index a688812e60..d804d1a19c 100644 --- a/faststream/utils/nuid.py +++ b/faststream/utils/nuid.py @@ -10,7 +10,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# from __future__ import annotations @@ -37,7 +36,7 @@ class NUID: """ def __init__(self) -> None: - self._prand = Random(randbelow(max_int)) + self._prand = Random(randbelow(max_int)) # nosec B311 self._seq = self._prand.randint(0, MAX_SEQ) self._inc = MIN_INC + self._prand.randint(BASE + 1, INC) self._prefix = bytearray() diff --git a/tests/brokers/nats/test_test_client.py b/tests/brokers/nats/test_test_client.py index ef1c727381..94a4be5dac 100644 --- a/tests/brokers/nats/test_test_client.py +++ b/tests/brokers/nats/test_test_client.py @@ -88,7 +88,7 @@ async def test_inbox_prefix_with_real( broker = NatsBroker(inbox_prefix="test") async with TestNatsBroker(broker, with_real=True) as br: - assert br._connection._inbox_prefix == b'test' + assert br._connection._inbox_prefix == b"test" assert "test" in str(br._connection.new_inbox())