Class: Aptabase::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/aptabase/session.rb

Overview

Session tracking with the same semantics as the other Aptabase SDKs: a session id is reused until one hour of inactivity, then rotated.

Constant Summary collapse

TIMEOUT =

seconds

60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(clock: -> { Time.now }) ⇒ Session

Returns a new instance of Session.



9
10
11
12
13
14
# File 'lib/aptabase/session.rb', line 9

def initialize(clock: -> { Time.now })
  @clock = clock
  @mutex = Mutex.new
  @id = nil
  @last_touched = nil
end

Instance Method Details

#idObject



16
17
18
19
20
21
22
23
# File 'lib/aptabase/session.rb', line 16

def id
  @mutex.synchronize do
    now = @clock.call
    @id = generate_id(now) if @id.nil? || now - @last_touched > TIMEOUT
    @last_touched = now
    @id
  end
end