Class: LaunchDarkly::Impl::DataSource::StatusProviderV2 Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
LaunchDarkly::Interfaces::DataSource::StatusProvider
Defined in:
lib/ldclient-rb/impl/data_source/status_provider.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides status tracking and listener management for data sources.

This class implements the LaunchDarkly::Interfaces::DataSource::StatusProvider interface. It maintains the current status of the data source and broadcasts status changes to listeners.

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(status_broadcaster) ⇒ StatusProviderV2

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new status provider.

Parameters:

Since:

  • 5.5.0



27
28
29
30
31
32
33
34
35
# File 'lib/ldclient-rb/impl/data_source/status_provider.rb', line 27

def initialize(status_broadcaster)
  @status_broadcaster = status_broadcaster
  @status = LaunchDarkly::Interfaces::DataSource::Status.new(
    LaunchDarkly::Interfaces::DataSource::Status::INITIALIZING,
    Time.now,
    nil
  )
  @lock = Concurrent::ReadWriteLock.new
end

Instance Method Details

#add_listener(listener) ⇒ Object Originally defined in module LaunchDarkly::Interfaces::DataSource::StatusProvider

Subscribes for notifications of status changes.

The listener will be notified whenever any property of the status has changed. See LaunchDarkly::Interfaces::DataSource::Status for an explanation of the meaning of each property and what could cause it to change.

Notifications will be dispatched on a worker thread. It is the listener's responsibility to return as soon as possible so as not to block subsequent notifications.

Parameters:

  • the (#update)

    listener to add

#remove_listener(listener) ⇒ Object Originally defined in module LaunchDarkly::Interfaces::DataSource::StatusProvider

Unsubscribes from notifications of status changes.

#statusStatus

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current status of the data source.

All of the built-in data source implementations are guaranteed to update this status whenever they successfully initialize, encounter an error, or recover after an error.

For a custom data source implementation, it is the responsibility of the data source to push status updates to the SDK; if it does not do so, the status will always be reported as Status::INITIALIZING.

Returns:

  • (Status)

Since:

  • 5.5.0



38
39
40
41
42
# File 'lib/ldclient-rb/impl/data_source/status_provider.rb', line 38

def status
  @lock.with_read_lock do
    @status
  end
end

#update_status(new_state, new_error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Informs the SDK of a change in the data source's status.

Data source implementations should use this method if they have any concept of being in a valid state, a temporarily disconnected state, or a permanently stopped state.

If new_state is different from the previous state, and/or new_error is non-null, the SDK will start returning the new status (adding a timestamp for the change) from LaunchDarkly::Impl::DataSource::StatusProvider#status, and will trigger status change events to any registered listeners.

A special case is that if new_state is Status::INTERRUPTED, but the previous state was Status::INITIALIZING, the state will remain at Status::INITIALIZING because Status::INTERRUPTED is only meaningful after a successful startup.

Parameters:

  • new_state (Symbol)
  • new_error (ErrorInfo, nil)

Since:

  • 5.5.0



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ldclient-rb/impl/data_source/status_provider.rb', line 45

def update_status(new_state, new_error)
  status_to_broadcast = nil

  @lock.with_write_lock do
    old_status = @status

    # Special handling: INTERRUPTED during INITIALIZING stays INITIALIZING
    if new_state == LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED &&
        old_status.state == LaunchDarkly::Interfaces::DataSource::Status::INITIALIZING
      new_state = LaunchDarkly::Interfaces::DataSource::Status::INITIALIZING
    end

    # No change if state is the same and no error
    return if new_state == old_status.state && new_error.nil?

    new_since = new_state == old_status.state ? @status.state_since : Time.now
    new_error = @status.last_error if new_error.nil?

    @status = LaunchDarkly::Interfaces::DataSource::Status.new(
      new_state,
      new_since,
      new_error
    )

    status_to_broadcast = @status
  end

  @status_broadcaster.broadcast(status_to_broadcast) if status_to_broadcast
end