Sha256: 188a176e1acadec1ee8c4b649e99adc0606f989a2e909134f6340e92fb7345c5

Contents?: true

Size: 639 Bytes

Versions: 1

Compression:

Stored size: 639 Bytes

Contents

# frozen_string_literal: true

module Logux
  class Node
    include Singleton

    attr_accessor :last_time, :sequence
    attr_writer :node_id

    def generate_action_id
      mutex.synchronize do
        if last_time && now_time <= last_time
          @sequence += 1
        else
          @sequence = 0
          @last_time = now_time
        end

        "#{last_time} #{node_id} #{sequence}"
      end
    end

    def node_id
      @node_id ||= "server:#{Nanoid.generate(size: 8)}"
    end

    private

    def now_time
      Time.now.to_datetime.strftime('%Q')
    end

    def mutex
      @mutex ||= Mutex.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logux_rails-0.1.0 lib/logux/node.rb