-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve Issue 1386, Add rpc_prefix (#1484)
* update adding nuid usage as well as reply_to_prefix * add missing commas * update reply_to nuid generation, add missing imports * rename reply_to_prefix to rpc_prefix * refactor rpc reply_to address generation. * remove reply_to, update nats implentation * add back reply_to * remove rpc_prefix, update NatsJSFastProducer * Add nats client inbox_prefix test * fixes to inbox_prefix test * lint: fix bandit --------- Co-authored-by: Pastukhov Nikita <[email protected]> Co-authored-by: Nikita Pastukhov <[email protected]>
- Loading branch information
1 parent
dfec397
commit 1e87e76
Showing
6 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
# 0.5 - API | ||
# 2 - Release | ||
# 3 - Contributing | ||
# 5 - Template Page | ||
# 10 - Default | ||
search: | ||
boost: 0.5 | ||
--- | ||
|
||
::: faststream.utils.nuid.NUID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 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 max_int | ||
|
||
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 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(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() | ||
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_seq = self._seq | ||
prefix = self._prefix[:] | ||
suffix = bytearray(SEQ_LENGTH) | ||
for i in reversed(range(SEQ_LENGTH)): | ||
suffix[i] = DIGITS[int(l_seq) % BASE] | ||
l_seq //= 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters