class Onliest::Snowflake

Constants

EPOCH

This comes from twitter snowflake. I’m not sure how they arrived at this number. November 3, 2010 at 11:42:55

Attributes

sequence[R]
worker_number[R]

Public Class Methods

new(worker_number) click to toggle source
Calls superclass method Onliest::new
# File lib/onliest/snowflake.rb, line 11
def initialize(worker_number)
  @worker_number = worker_number
  @sequence = 0
  @older_time = 0
  super(fields: [{ bits: 1, generator: ->{ 0 } },
                 { bits: 41, generator: ->{ time } },
                 { bits: 10, generator: ->{ worker_number } },
                 { bits: 12, generator: ->{ sequence_nextval } }])
end

Private Instance Methods

sequence_nextval() click to toggle source
# File lib/onliest/snowflake.rb, line 23
def sequence_nextval
  i = @sequence
  @sequence = @sequence.succ
  i
end
time() click to toggle source
# File lib/onliest/snowflake.rb, line 29
def time
  while (t = time_now) < @older_time
    #puts @older_time
    #puts t
    sleep((@older_time - t) / 1000)
  end
  @older_time = t
  t
end
time_now() click to toggle source
# File lib/onliest/snowflake.rb, line 39
def time_now
  ((Time.now.to_f * 1000).to_i - EPOCH)
end