Class: LaunchDarkly::Impl::DataSystem::PollingDataSource Private

Inherits:
Object
  • Object
show all
Includes:
LaunchDarkly::Interfaces::DataSystem::Initializer, LaunchDarkly::Interfaces::DataSystem::Synchronizer
Defined in:
lib/ldclient-rb/impl/data_system/polling.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.

PollingDataSource is a data source that can retrieve information from LaunchDarkly either as an Initializer or as a Synchronizer.

Since:

  • 5.5.0

API:

  • private

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(poll_interval, requester, logger) ⇒ PollingDataSource

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 a new instance of PollingDataSource.

Since:

  • 5.5.0

Parameters:

  • Polling interval in seconds

  • The requester to use for fetching data

  • The logger

API:

  • private



65
66
67
68
69
70
71
72
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 65

def initialize(poll_interval, requester, logger)
  @requester = requester
  @poll_interval = poll_interval
  @logger = logger
  @interrupt_event = Concurrent::Event.new
  @stop = Concurrent::Event.new
  @name = "PollingDataSourceV2"
end

Instance Attribute Details

#nameObject (readonly)

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.

Since:

  • 5.5.0

API:

  • private



58
59
60
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 58

def name
  @name
end

Instance Method Details

#fetch(ss) ⇒ LaunchDarkly::Interfaces::DataSystem::Basis?

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.

Fetch returns a Basis, or an error if the Basis could not be retrieved.

Since:

  • 5.5.0

Parameters:

Returns:

API:

  • private



80
81
82
83
84
85
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 80

def fetch(ss)
  poll(ss)
ensure
  # Ensure the requester is stopped to avoid leaving open connections.
  @requester.stop if @requester.respond_to?(:stop)
end

#stopObject

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.

Stops the synchronizer.

Since:

  • 5.5.0

API:

  • private



198
199
200
201
202
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 198

def stop
  @logger.info { "[LDClient] Stopping PollingDataSourceV2 synchronizer" }
  @interrupt_event.set
  @stop.set
end

#sync(ss) {|update| ... } ⇒ 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.

sync begins the synchronization process for the data source, yielding Update objects until the connection is closed or an unrecoverable error occurs.

Yield Parameters:

Since:

  • 5.5.0

Parameters:

API:

  • private



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 95

def sync(ss)
  @logger.info { "[LDClient] Starting PollingDataSourceV2 synchronizer" }

  until @stop.set?
    result = @requester.fetch(ss.selector)

    if !result.success?
      fallback = false
      envid = nil

      if result.headers
        fallback = result.headers[LD_FD_FALLBACK_HEADER] == 'true'
        envid = result.headers[LD_ENVID_HEADER]
      end

      if result.exception.is_a?(LaunchDarkly::Impl::DataSource::UnexpectedResponseError)
        error_info = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(
          LaunchDarkly::Interfaces::DataSource::ErrorInfo::ERROR_RESPONSE,
          result.exception.status,
          Impl::Util.http_error_message(
            result.exception.status, "polling request", "will retry"
          ),
          Time.now
        )

        status_code = result.exception.status
        if Impl::Util.http_error_recoverable?(status_code)
          # If fallback is requested, send OFF status to signal shutdown
          if fallback
            yield LaunchDarkly::Interfaces::DataSystem::Update.new(
              state: LaunchDarkly::Interfaces::DataSource::Status::OFF,
              error: error_info,
              environment_id: envid,
              revert_to_fdv1: true
            )
            break
          end

          yield LaunchDarkly::Interfaces::DataSystem::Update.new(
            state: LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED,
            error: error_info,
            environment_id: envid,
            revert_to_fdv1: false
          )
          @interrupt_event.wait(@poll_interval)
          next
        end

        yield LaunchDarkly::Interfaces::DataSystem::Update.new(
          state: LaunchDarkly::Interfaces::DataSource::Status::OFF,
          error: error_info,
          environment_id: envid,
          revert_to_fdv1: fallback
        )
        break
      end

      error_info = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(
        LaunchDarkly::Interfaces::DataSource::ErrorInfo::NETWORK_ERROR,
        0,
        result.error,
        Time.now
      )

    # If fallback is requested, send OFF status to signal shutdown
      if fallback
        yield LaunchDarkly::Interfaces::DataSystem::Update.new(
          state: LaunchDarkly::Interfaces::DataSource::Status::OFF,
          error: error_info,
          environment_id: envid,
          revert_to_fdv1: true
        )
        break
      end

      yield LaunchDarkly::Interfaces::DataSystem::Update.new(
        state: LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED,
        error: error_info,
        environment_id: envid,
        revert_to_fdv1: false
      )
    else
      change_set, headers = result.value
      fallback = headers[LD_FD_FALLBACK_HEADER] == 'true'
      yield LaunchDarkly::Interfaces::DataSystem::Update.new(
        state: LaunchDarkly::Interfaces::DataSource::Status::VALID,
        change_set: change_set,
        environment_id: headers[LD_ENVID_HEADER],
        revert_to_fdv1: fallback
      )
    end

    break if fallback
    break if @interrupt_event.wait(@poll_interval)
  end
ensure
  # Ensure the requester is stopped to avoid leaving open connections.
  @requester.stop if @requester.respond_to?(:stop)
end