-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from nats-io/nuid
Adopt NUID for inboxes generation
- Loading branch information
Showing
9 changed files
with
200 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ gemspec | |
group :test do | ||
gem 'rake' | ||
gem 'rspec' | ||
gem 'benchmark-ips' | ||
end |
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,20 @@ | ||
require 'securerandom' | ||
require 'nats/nuid' | ||
require 'benchmark/ips' | ||
|
||
Benchmark.ips do |x| | ||
x.report "NUID based inboxes with locked instance" do |t| | ||
t.times { "_INBOX.#{NATS::NUID.next}" } | ||
end | ||
|
||
x.report "NUID based inboxes with owned instance" do |t| | ||
nuid = NATS::NUID.new | ||
t.times { "_INBOX.#{nuid.next}" } | ||
end | ||
|
||
x.report "SecureRandom based inboxes" do |t| | ||
t.times { "_INBOX.#{::SecureRandom.hex(11)}" } | ||
end | ||
|
||
x.compare! | ||
end |
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,81 @@ | ||
# 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. | ||
# | ||
require 'securerandom' | ||
|
||
module NATS | ||
class NUID | ||
DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('') | ||
BASE = 62 | ||
PREFIX_LENGTH = 12 | ||
SEQ_LENGTH = 10 | ||
TOTAL_LENGTH = PREFIX_LENGTH + SEQ_LENGTH | ||
MAX_SEQ = BASE**10 | ||
MIN_INC = 33 | ||
MAX_INC = 333 | ||
INC = MAX_INC - MIN_INC | ||
|
||
def initialize | ||
@prand = Random.new | ||
@seq = @prand.rand(MAX_SEQ) | ||
@inc = MIN_INC + @prand.rand(INC) | ||
@prefix = '' | ||
randomize_prefix! | ||
end | ||
|
||
def next | ||
@seq += @inc | ||
if @seq >= MAX_SEQ | ||
randomize_prefix! | ||
reset_sequential! | ||
end | ||
l = @seq | ||
|
||
# Do this inline 10 times to avoid even more extra allocs, | ||
# then use string interpolation of everything which works | ||
# faster for doing concat. | ||
s_10 = DIGITS[l % BASE]; | ||
|
||
# Ugly, but parallel assignment is slightly faster here... | ||
s_09, s_08, s_07, s_06, s_05, s_04, s_03, s_02, s_01 = \ | ||
(l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]),\ | ||
(l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]),\ | ||
(l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]) | ||
"#{@prefix}#{s_01}#{s_02}#{s_03}#{s_04}#{s_05}#{s_06}#{s_07}#{s_08}#{s_09}#{s_10}" | ||
end | ||
|
||
def randomize_prefix! | ||
@prefix = \ | ||
SecureRandom.random_bytes(PREFIX_LENGTH).each_byte | ||
.reduce('') do |prefix, n| | ||
prefix << DIGITS[n % BASE] | ||
end | ||
end | ||
|
||
private | ||
|
||
def reset_sequential! | ||
@seq = @prand.rand(MAX_SEQ) | ||
@inc = MIN_INC + @prand.rand(INC) | ||
end | ||
|
||
class << self | ||
@@nuid = NUID.new.extend(MonitorMixin) | ||
def next | ||
@@nuid.synchronize do | ||
@@nuid.next | ||
end | ||
end | ||
end | ||
end | ||
end |
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,86 @@ | ||
# 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. | ||
# | ||
|
||
require 'spec_helper' | ||
|
||
describe 'NUID' do | ||
it "should have a fixed length and be unique" do | ||
nuid = NATS::NUID.new | ||
entries = [] | ||
total = 500_000 | ||
total.times do | ||
entry = nuid.next | ||
expect(entry.size).to eql(NATS::NUID::TOTAL_LENGTH) | ||
entries << entry | ||
end | ||
entries.uniq! | ||
expect(entries.count).to eql(total) | ||
end | ||
|
||
it "should be unique after 1M entries" do | ||
total = 1_000_000 | ||
entries = [] | ||
nuid = NATS::NUID.new | ||
total.times do | ||
entries << nuid.next | ||
end | ||
entries.uniq! | ||
expect(entries.count).to eql(total) | ||
end | ||
|
||
it "should randomize the prefix after sequence is done" do | ||
nuid = NATS::NUID.new | ||
seq_a = nuid.instance_variable_get('@seq') | ||
inc_a = nuid.instance_variable_get('@inc') | ||
a = nuid.next | ||
|
||
seq_b = nuid.instance_variable_get('@seq') | ||
inc_b = nuid.instance_variable_get('@inc') | ||
expect(seq_a < seq_b).to eql(true) | ||
expect(seq_b).to eql(seq_a + inc_a) | ||
b = nuid.next | ||
|
||
nuid.instance_variable_set('@seq', NATS::NUID::MAX_SEQ+1) | ||
c = nuid.next | ||
l = NATS::NUID::PREFIX_LENGTH | ||
expect(a[0..l]).to eql(b[0..l]) | ||
expect(a[0..l]).to_not eql(c[0..l]) | ||
end | ||
|
||
context "when using the NUID.next" do | ||
it "should be thread safe" do | ||
ts = Hash.new { |h,k| h[k] = { }} | ||
total = 100_000 | ||
10.times do |n| | ||
ts[n][:thread] = Thread.new do | ||
sleep 0.01 | ||
total.times do | ||
ts[n][:entries] ||= [] | ||
ts[n][:entries] << NATS::NUID.next | ||
end | ||
end | ||
end | ||
|
||
total_entries = [] | ||
ts.each do |k, t| | ||
t[:thread].join | ||
expect(t[:entries].count).to eql(total) | ||
total_entries << t[:entries] | ||
end | ||
total_entries.flatten! | ||
total_entries.uniq! | ||
expect(total_entries.count).to eql(total * 10) | ||
end | ||
end | ||
end |
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