Class: Kameleoon::ClientReadiness

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/client_readiness.rb

Overview

ClientReadiness is a cross-thread latch signalling that the SDK has successfully loaded its configuration.

It is created on the thread that builds the client but released (set) from the background thread(s) performing configuration fetches, so it relies on Concurrent::Event (which allows cross-thread release) rather than a read/write lock (whose write lock must be released by the acquiring thread).

A failed fetch does NOT release the latch: the client keeps retrying in the background and the latch is released as soon as any fetch succeeds. Callers should bound their wait with a timeout so they are never blocked forever when the configuration can never be loaded.

Instance Method Summary collapse

Constructor Details

#initializeClientReadiness

Returns a new instance of ClientReadiness.



19
20
21
22
# File 'lib/kameleoon/client_readiness.rb', line 19

def initialize
  @success = false
  @event = Concurrent::Event.new
end

Instance Method Details

#set(success) ⇒ Object

Record a fetch outcome. A successful fetch marks the client ready and unblocks all waiting threads; a failed fetch is ignored so that a later successful retry can still release the latch. Idempotent and safe to call from any thread.



33
34
35
36
37
38
# File 'lib/kameleoon/client_readiness.rb', line 33

def set(success)
  return unless success

  @success = true
  @event.set
end

#successBoolean

Returns whether the SDK has become ready (configuration loaded).

Returns:

  • (Boolean)

    whether the SDK has become ready (configuration loaded).



25
26
27
# File 'lib/kameleoon/client_readiness.rb', line 25

def success
  @success
end

#wait(timeout = nil) ⇒ Boolean

Block until the SDK becomes ready or the timeout elapses.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil)

    maximum number of seconds to wait; nil waits indefinitely.

Returns:

  • (Boolean)

    whether the SDK is ready.



45
46
47
48
# File 'lib/kameleoon/client_readiness.rb', line 45

def wait(timeout = nil)
  @event.wait(timeout)
  @success
end