Class: LaunchDarkly::StreamProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, config, diagnostic_accumulator = nil) ⇒ StreamProcessor



25
26
27
28
29
30
31
32
33
34
# File 'lib/ldclient-rb/stream.rb', line 25

def initialize(sdk_key, config, diagnostic_accumulator = nil)
  @sdk_key = sdk_key
  @config = config
  @feature_store = config.feature_store
  @initialized = Concurrent::AtomicBoolean.new(false)
  @started = Concurrent::AtomicBoolean.new(false)
  @stopped = Concurrent::AtomicBoolean.new(false)
  @ready = Concurrent::Event.new
  @connection_attempt_start_time = 0
end

Instance Method Details

#initialized?Boolean



36
37
38
# File 'lib/ldclient-rb/stream.rb', line 36

def initialized?
  @initialized.value
end

#startObject



40
41
42
43
44
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
# File 'lib/ldclient-rb/stream.rb', line 40

def start
  return @ready unless @started.make_true

  @config.logger.info { "[LDClient] Initializing stream connection" }
  
  headers = Impl::Util.default_http_headers(@sdk_key, @config)
  opts = {
    headers: headers,
    read_timeout: READ_TIMEOUT_SECONDS,
    logger: @config.logger,
    socket_factory: @config.socket_factory,
    reconnect_time: @config.initial_reconnect_delay
  }
  log_connection_started
  @es = SSE::Client.new(@config.stream_uri + "/all", **opts) do |conn|
    conn.on_event { |event| process_message(event) }
    conn.on_error { |err|
      log_connection_result(false)
      case err
      when SSE::Errors::HTTPStatusError
        status = err.status
        message = Util.http_error_message(status, "streaming connection", "will retry")
        @config.logger.error { "[LDClient] #{message}" }
        if !Util.http_error_recoverable?(status)
          @ready.set  # if client was waiting on us, make it stop waiting - has no effect if already set
          stop
        end
      end
    }
  end
  
  @ready
end

#stopObject



74
75
76
77
78
79
# File 'lib/ldclient-rb/stream.rb', line 74

def stop
  if @stopped.make_true
    @es.close
    @config.logger.info { "[LDClient] Stream connection stopped" }
  end
end