Skip to content

Commit b82a50b

Browse files
committed
Imporoved stringified id generation
1 parent 4da254d commit b82a50b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

cppsrc/main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ Napi::FunctionReference Snowflake::constructor;
117117
* Next 10 bits are filled with the node/machine id (max size can be 1024)
118118
* Next 12 bits are filled with sequence which ensures that even if timestamp didn't change the value will be generated
119119
*
120-
* Function can theorotically generate 1024 unique values within a millisecond without repeating values
120+
* Function can theorotically generate 4096 unique values within a millisecond without repeating values
121121
*/
122122
Napi::Value Snowflake::getUniqueIDBigInt(const Napi::CallbackInfo &info)
123123
{
124124
Napi::Env env = info.Env();
125125

126-
uint64_t currentTimestamp = getCurrentTime();
126+
uint64_t currentTimestamp = getCurrentTime() - this->_CUSTOM_EPOCH;
127127

128128
if (currentTimestamp == this->_lastTimestamp)
129129
{
@@ -161,7 +161,10 @@ Napi::Value Snowflake::getUniqueID(const Napi::CallbackInfo &info)
161161
{
162162
this->_sequence = (this->_sequence + 1) & maxSequence;
163163
if (this->_sequence == 0)
164-
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
164+
{
165+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
166+
currentTimestamp++;
167+
}
165168
}
166169
else
167170
{

0 commit comments

Comments
 (0)