Sha256: 598165fa0bf47d719438be18a5de052fbd493630e821ad7cb1d2125e47a27b56

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

require 'securerandom'

# Borrowed from Celluloid. Using thread locals instead of extending the base
# Thread class, though.
module Agent
  module UUID
    values = SecureRandom.hex(9).match(/(.{8})(.{4})(.{3})(.{3})/)
    PREFIX = "#{values[1]}_#{values[2]}_4#{values[3]}_8#{values[4]}".freeze
    BLOCK_SIZE = 0x10000

    @counter = 0
    @counter_mutex = Mutex.new

    def self.generate
      thread = Thread.current

      unless thread[:__agent_uuid_limit__]
        @counter_mutex.synchronize do
          block_base = @counter
          @counter += BLOCK_SIZE
          thread[:__agent_uuid_counter__] = block_base
          thread[:__agent_uuid_limit__]   = @counter - 1
        end
      end

      counter = thread[:__agent_uuid_counter__]
      if thread[:__agent_uuid_counter__] >= thread[:__agent_uuid_limit__]
        thread[:__agent_uuid_counter__] = thread[:__agent_uuid_limit__] = nil
      else
        thread[:__agent_uuid_counter__] += 1
      end

      "#{PREFIX}_#{sprintf("%012x", counter)}".freeze
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
agent-0.12.0 lib/agent/uuid.rb
agent-0.11.0 lib/agent/uuid.rb
agent-0.10.0 lib/agent/uuid.rb
agent-0.9.1 lib/agent/uuid.rb
agent-0.9.0 lib/agent/uuid.rb