Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/splitclient-rb/engine/sync_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def initialize(
@sse_handler = SplitIoClient::SSE::SSEHandler.new(
config,
@synchronizer,
repositories[:splits],
repositories[:segments],
notification_manager_keeper
repositories,
notification_manager_keeper,
api_key
) do |handler|
handler.on_action { |action| process_action(action) }
end
Expand Down
7 changes: 6 additions & 1 deletion lib/splitclient-rb/sse/event_source/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class Client
KEEP_ALIVE_RESPONSE = "c\r\n:keepalive\n\n\r\n".freeze
ERROR_EVENT_TYPE = 'error'.freeze

def initialize(config, read_timeout: DEFAULT_READ_TIMEOUT)
def initialize(config, api_key, read_timeout: DEFAULT_READ_TIMEOUT)
@config = config
@read_timeout = read_timeout
@connected = Concurrent::AtomicBoolean.new(false)
@first_event = Concurrent::AtomicBoolean.new(true)
@socket = nil
@event_parser = SSE::EventSource::EventParser.new(config)
@on = { event: ->(_) {}, action: ->(_) {} }
@api_key = api_key

yield self if block_given?
end
Expand Down Expand Up @@ -142,6 +143,10 @@ def build_request(uri)
req = "GET #{uri.request_uri} HTTP/1.1\r\n"
req << "Host: #{uri.host}\r\n"
req << "Accept: text/event-stream\r\n"
req << "SplitSDKVersion: #{@config.language}-#{@config.version}\r\n"
req << "SplitSDKMachineIP: #{@config.machine_ip}\r\n"
req << "SplitSDKMachineName: #{@config.machine_name}\r\n"
req << "SplitSDKClientKey: #{@api_key.split(//).last(4).join}\r\n" unless @api_key.nil?
req << "Cache-Control: no-cache\r\n\r\n"
@config.logger.debug("Request info: #{req}") if @config.debug_enabled
req
Expand Down
8 changes: 4 additions & 4 deletions lib/splitclient-rb/sse/sse_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ module SSE
class SSEHandler
attr_reader :sse_client

def initialize(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper)
def initialize(config, synchronizer, repositories, notification_manager_keeper, api_key)
@config = config
@notification_manager_keeper = notification_manager_keeper
@splits_worker = SplitIoClient::SSE::Workers::SplitsWorker.new(synchronizer, config, splits_repository)
@segments_worker = SplitIoClient::SSE::Workers::SegmentsWorker.new(synchronizer, config, segments_repository)
@splits_worker = SplitIoClient::SSE::Workers::SplitsWorker.new(synchronizer, config, repositories[:splits])
@segments_worker = SplitIoClient::SSE::Workers::SegmentsWorker.new(synchronizer, config, repositories[:segments])
@notification_processor = SplitIoClient::SSE::NotificationProcessor.new(config, @splits_worker, @segments_worker)
@sse_client = SSE::EventSource::Client.new(@config) do |client|
@sse_client = SSE::EventSource::Client.new(@config, api_key) do |client|
client.on_event { |event| handle_incoming_message(event) }
client.on_action { |action| process_action(action) }
end
Expand Down
24 changes: 12 additions & 12 deletions spec/engine/push_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
sse_handler = SplitIoClient::SSE::SSEHandler.new(
config,
synchronizer,
splits_repository,
segments_repository,
notification_manager_keeper
repositories,
notification_manager_keeper,
api_key
) do |handler|
handler.on_action { |action| action_event = action }
end
Expand All @@ -70,9 +70,9 @@
sse_handler = SplitIoClient::SSE::SSEHandler.new(
config,
synchronizer,
splits_repository,
segments_repository,
notification_manager_keeper
repositories,
notification_manager_keeper,
api_key
) do |handler|
handler.on_action { |action| action_event = action }
end
Expand All @@ -96,9 +96,9 @@
sse_handler = SplitIoClient::SSE::SSEHandler.new(
config,
synchronizer,
splits_repository,
segments_repository,
notification_manager_keeper
repositories,
notification_manager_keeper,
api_key
) do |handler|
handler.on_action { |action| action_event = action }
end
Expand Down Expand Up @@ -130,9 +130,9 @@
sse_handler = SplitIoClient::SSE::SSEHandler.new(
config,
synchronizer,
splits_repository,
segments_repository,
notification_manager_keeper
repositories,
notification_manager_keeper,
api_key
) do |handler|
handler.on_action { |action| action_event = action }
end
Expand Down
18 changes: 10 additions & 8 deletions spec/sse/event_source/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
let(:log) { StringIO.new }
let(:config) { SplitIoClient::SplitConfig.new(logger: Logger.new(log)) }

let(:api_token) { 'api-token-test' }

let(:event_split_update) { "fb\r\nid: 123\nevent: message\ndata: {\"id\":\"1\",\"clientId\":\"emptyClientId\",\"connectionId\":\"1\",\"timestamp\":1582045421733,\"channel\":\"channel-test\",\"data\":\"{\\\"type\\\" : \\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\": 5564531221}\",\"name\":\"asdasd\"}\n\n\r\n" }
let(:event_split_kill) { "fb\r\nid: 123\nevent: message\ndata: {\"id\":\"1\",\"clientId\":\"emptyClientId\",\"connectionId\":\"1\",\"timestamp\":1582045421733,\"channel\":\"channel-test\",\"data\":\"{\\\"type\\\" : \\\"SPLIT_KILL\\\",\\\"changeNumber\\\": 5564531221, \\\"defaultTreatment\\\" : \\\"off\\\", \\\"splitName\\\" : \\\"split-test\\\"}\",\"name\":\"asdasd\"}\n\n\r\n" }
let(:event_segment_update) { "fb\r\nid: 123\nevent: message\ndata: {\"id\":\"1\",\"clientId\":\"emptyClientId\",\"connectionId\":\"1\",\"timestamp\":1582045421733,\"channel\":\"channel-test\",\"data\":\"{\\\"type\\\" : \\\"SEGMENT_UPDATE\\\",\\\"changeNumber\\\": 5564531221, \\\"segmentName\\\" : \\\"segment-test\\\"}\",\"name\":\"asdasd\"}\n\n\r\n" }
Expand All @@ -24,7 +26,7 @@
end
event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand Down Expand Up @@ -55,7 +57,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand Down Expand Up @@ -88,7 +90,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand Down Expand Up @@ -120,7 +122,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand Down Expand Up @@ -151,7 +153,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand All @@ -178,7 +180,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand Down Expand Up @@ -208,7 +210,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand All @@ -229,7 +231,7 @@

event_queue = Queue.new
action_event = ''
sse_client = subject.new(config) do |client|
sse_client = subject.new(config, api_token) do |client|
client.on_event { |event| event_queue << event }
client.on_action { |action| action_event = action }
end
Expand Down
14 changes: 7 additions & 7 deletions spec/sse/sse_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down Expand Up @@ -92,7 +92,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down Expand Up @@ -122,7 +122,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down Expand Up @@ -154,7 +154,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down Expand Up @@ -188,7 +188,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down Expand Up @@ -216,7 +216,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down Expand Up @@ -246,7 +246,7 @@

config.streaming_service_url = server.base_uri
action_event = ''
sse_handler = subject.new(config, synchronizer, splits_repository, segments_repository, notification_manager_keeper) do |handler|
sse_handler = subject.new(config, synchronizer, repositories, notification_manager_keeper, api_key) do |handler|
handler.on_action { |action| action_event = action }
end

Expand Down