Class: SplitIoClient::SSE::EventSource::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/sse/event_source/client.rb

Constant Summary collapse

DEFAULT_READ_TIMEOUT =
70
CONNECT_TIMEOUT =
30_000
OK_CODE =
200
KEEP_ALIVE_RESPONSE =
"c\r\n:keepalive\n\n\r\n".freeze
ERROR_EVENT_TYPE =
'error'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, api_key, telemetry_runtime_producer, event_parser, notification_manager_keeper, notification_processor, status_queue, read_timeout: DEFAULT_READ_TIMEOUT) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/splitclient-rb/sse/event_source/client.rb', line 16

def initialize(config,
               api_key,
               telemetry_runtime_producer,
               event_parser,
               notification_manager_keeper,
               notification_processor,
               status_queue,
               read_timeout: DEFAULT_READ_TIMEOUT)
  @config = config
  @api_key = api_key
  @telemetry_runtime_producer = telemetry_runtime_producer
  @event_parser = event_parser
  @notification_manager_keeper = notification_manager_keeper
  @notification_processor = notification_processor
  @status_queue = status_queue
  @read_timeout = read_timeout
  @connected = Concurrent::AtomicBoolean.new(false)
  @first_event = Concurrent::AtomicBoolean.new(true)
  @socket = nil
end

Instance Method Details

#close(status = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/splitclient-rb/sse/event_source/client.rb', line 37

def close(status = nil)
  unless connected?
    @config.logger.error('SSEClient already disconected.') if @config.debug_enabled
    return
  end

  @connected.make_false
  @socket&.close
  push_status(status)
rescue StandardError => e
  @config.logger.error("SSEClient close Error: #{e.inspect}")
end

#connected?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/splitclient-rb/sse/event_source/client.rb', line 69

def connected?
  @connected.value
end

#start(url) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/splitclient-rb/sse/event_source/client.rb', line 50

def start(url)
  if connected?
    @config.logger.debug('SSEClient already running.')
    return true
  end

  @uri = URI(url)
  latch = Concurrent::CountDownLatch.new(1)

  connect_thread(latch)

  return false unless latch.wait(CONNECT_TIMEOUT)

  connected?
rescue StandardError => e
  @config.logger.error("SSEClient start Error: #{e.inspect}")
  connected?
end