Sha256: 8baa618f05d536e38ffe125bd6f6925a7b0de7798f54078d8ae6d1fb747019c0

Contents?: true

Size: 1.08 KB

Versions: 41

Compression:

Stored size: 1.08 KB

Contents

require 'securerandom'

module Celluloid
  # Clearly Ruby doesn't have enough UUID libraries
  # This one aims to be fast and simple with good support for multiple threads
  # If there's a better UUID library I can use with similar multithreaded
  # performance, I certainly wouldn't mind using a gem for this!
  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.uuid_limit
        @counter_mutex.synchronize do
          block_base = @counter
          @counter += BLOCK_SIZE
          thread.uuid_counter = block_base
          thread.uuid_limit   = @counter - 1
        end
      end

      counter = thread.uuid_counter
      if thread.uuid_counter >= thread.uuid_limit
        thread.uuid_counter = thread.uuid_limit = nil
      else
        thread.uuid_counter += 1
      end

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

Version data entries

41 entries across 39 versions & 7 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/uuid.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/celluloid-0.16.0/lib/celluloid/uuid.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/celluloid-0.16.0/lib/celluloid/uuid.rb
scoot-0.0.4 .bundle/gems/ruby/2.2.0/gems/celluloid-0.16.0/lib/celluloid/uuid.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/celluloid-0.16.0/lib/celluloid/uuid.rb
celluloid-0.16.0 lib/celluloid/uuid.rb
celluloid-0.16.0.pre3 lib/celluloid/uuid.rb
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/celluloid-0.15.2/lib/celluloid/uuid.rb
celluloid-0.16.0.pre2 lib/celluloid/uuid.rb
celluloid-0.16.0.pre lib/celluloid/uuid.rb
celluloid-0.15.2 lib/celluloid/uuid.rb
celluloid-0.15.1 lib/celluloid/uuid.rb
celluloid-0.15.0 lib/celluloid/uuid.rb
celluloid-0.15.0.pre2 lib/celluloid/uuid.rb
celluloid-0.15.0.pre lib/celluloid/uuid.rb
celluloid-0.14.1 lib/celluloid/uuid.rb
celluloid-0.14.1.pre lib/celluloid/uuid.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/uuid.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/uuid.rb
celluloid-0.14.0 lib/celluloid/uuid.rb