lib/propertybase_id/generator.rb in propertybase_id-0.5.0 vs lib/propertybase_id/generator.rb in propertybase_id-0.6.0

- old
+ new

@@ -1,25 +1,17 @@ require "digest/sha1" require "socket" +require "securerandom" class PropertybaseId class Generator - def initialize - @counter = 0 - @mutex = Mutex.new - end - def generate(object: ) - @mutex.synchronize do - PropertybaseId.new( - object_id: pb_object_id(object), - host_id: host_id, - time: ::Time.now.to_i, - process_id: process_id, - counter: next_counter - ) - end + PropertybaseId.new( + object_id: pb_object_id(object), + time: ::Time.now.to_i, + random_int: random_32 + ) end private def pb_object_id(object) @@ -28,9 +20,13 @@ end end def host_id @_host_id ||= Digest::SHA1.hexdigest(Socket.gethostname).to_i(16) % PropertybaseId.max_value(2) + end + + def random_32 + SecureRandom::random_number("zzzzzz".to_i(36) + 1) end def next_counter @counter = (@counter + 1) % PropertybaseId.max_value(5) end