kameleoon-client-ruby 3.22.1 → 3.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04efd3ab470f352395de3161c329086e07893f9a29d2e8d55085cc4ab00788c3
4
- data.tar.gz: 62d498e4f19ec285358b29672d622e0476e70ae31cbf39bd480bf33dbe5b335b
3
+ metadata.gz: 5e99837e0d5fa905c2635ec24e42867b55d5fba98a68283b79b59ffb51d95312
4
+ data.tar.gz: e0375cd6f8aca052dbb50cc839f41ce853d34bf48b0ee52ae92b841f24b5dc26
5
5
  SHA512:
6
- metadata.gz: 0431f162cc71fb10db0c3920bef2a9d04c35713dfdfd467fc750ac16345c3cfd5965b8abb17c335ad8a7e7226d85c776a14d5d76030c7173969b00a61b309fd1
7
- data.tar.gz: 683b9a9e65838df67344bc3b5ff05d8b04eb37fe5328ec4532f4d18b141d9957ca6709b3373adc22ce4f52791d3d0e90be8927693acfe159add2ba991812dc16
6
+ metadata.gz: a432523e9510c65671d627f55250169b550feea3026004e908191828baccb6e75c43b345701041894aeaa180e2ff700dfe2b693e64e1236091620a15f5c4d572
7
+ data.tar.gz: 9da0e374af6713aa7e15ae4e2e2e1f4726bcc7322a7a764143f05e0e7c7bf8402c60fc8ef924b360532b738a4a66ff12560ebc023813d29f9c0694d5e0ae8918
@@ -8,22 +8,25 @@ module Kameleoon
8
8
  # KameleoonConfigurationSettings is used for saving setting's parameters, e.g
9
9
  # state of real time update for site code and etc
10
10
  class Settings
11
- attr_accessor :real_time_update
11
+ # `true` when configuration updates are to be received as streaming events rather than polled.
12
+ attr_reader :streaming_mode
12
13
  attr_reader :is_consent_required, :blocking_behaviour_if_consent_not_given, :data_api_domain
13
14
 
15
+ alias streaming_mode? streaming_mode
16
+
14
17
  def to_s
15
- "Settings{real_time_update:#{@real_time_update},is_consent_required:#{@is_consent_required}," \
18
+ "Settings{streaming_mode:#{@streaming_mode},is_consent_required:#{@is_consent_required}," \
16
19
  "data_api_domain:#{@data_api_domain}}"
17
20
  end
18
21
 
19
22
  def initialize(configuration = nil)
20
23
  if configuration.nil?
21
- @real_time_update = false
24
+ @streaming_mode = false
22
25
  @is_consent_required = false
23
26
  @blocking_behaviour_if_consent_not_given = ConsentBlockingBehaviour::PARTIALLY_BLOCKED
24
27
  @data_api_domain = nil
25
28
  else
26
- @real_time_update = configuration['realTimeUpdate'] || false
29
+ @streaming_mode = ConfigUpdateMode.streaming?(configuration['configUpdateMode'])
27
30
  @is_consent_required = configuration['consentType'] == 'REQUIRED'
28
31
  @blocking_behaviour_if_consent_not_given =
29
32
  ConsentBlockingBehaviour.from_str(configuration['consentOptOutBehavior'])
@@ -32,6 +35,27 @@ module Kameleoon
32
35
  end
33
36
  end
34
37
 
38
+ # ConfigUpdateMode is the way the SDK receives configuration updates, as set in the configuration.
39
+ module ConfigUpdateMode
40
+ POLLING = 'POLLING'
41
+ STREAMING = 'STREAMING'
42
+ # Streaming for server-side SDKs only, this one included
43
+ STREAMING_SERVER_ONLY = 'STREAMING_SERVER_ONLY'
44
+
45
+ def self.streaming?(str)
46
+ mode = str.is_a?(String) ? str.upcase : nil
47
+ case mode
48
+ when STREAMING, STREAMING_SERVER_ONLY
49
+ true
50
+ when POLLING
51
+ false
52
+ else
53
+ Logging::KameleoonLogger.warning("Unexpected ConfigUpdateMode '%s'", str)
54
+ false
55
+ end
56
+ end
57
+ end
58
+
35
59
  # ConsentBlockingBehaviour
36
60
  module ConsentBlockingBehaviour
37
61
  PARTIALLY_BLOCKED = 0
@@ -3,7 +3,7 @@
3
3
  require 'json'
4
4
 
5
5
  module Kameleoon
6
- # UserAgent uses for changing User-Agent header for tracking calls
6
+ # UserAgent of a visitor for tracking requests
7
7
  class UserAgent
8
8
  attr_reader :value
9
9
 
@@ -29,7 +29,7 @@ require 'kameleoon/network/activity_event'
29
29
  require 'kameleoon/network/network_manager'
30
30
  require 'kameleoon/network/url_provider'
31
31
  require 'kameleoon/network/cookie/cookie_manager'
32
- require 'kameleoon/real_time/real_time_event_service'
32
+ require 'kameleoon/network/streaming/streaming_service'
33
33
  require 'kameleoon/targeting/models'
34
34
  require 'kameleoon/targeting/targeting_manager'
35
35
  require 'kameleoon/types/variable'
@@ -65,9 +65,11 @@ module Kameleoon
65
65
  @site_code = site_code
66
66
  @config = config
67
67
  @disposed = false
68
- @real_time_event_service = nil
68
+ @streaming_service = nil
69
69
  @update_configuration_handler = nil
70
70
  @fetch_configuration_update_job = nil
71
+ @configuration_update_mutex = Mutex.new
72
+ @apply_configuration_mutex = Mutex.new
71
73
  data_file = Configuration::DataFile.new(config.environment, nil)
72
74
  @data_manager = Managers::Data::DataManager.new(data_file)
73
75
  @visitor_manager = Kameleoon::DataManager::VisitorManager.new(
@@ -886,27 +888,34 @@ module Kameleoon
886
888
  private
887
889
 
888
890
  HYBRID_EXPIRATION_TIME = 5
891
+ # Delay before the first poll after a fallback from the streaming mode, so that the streaming is
892
+ # retried soon (each successful poll switches back to it when it is enabled), unless the polls are
893
+ # even more frequent.
894
+ STREAMING_RETRY_DELAY_SECONDS = 60
889
895
 
890
- # Fetches the configuration in a background thread; used for the initial fetch,
891
- # the polling schedule, and the streaming events alike. The fetch outcome settles
892
- # the client readiness, and a failed fetch always falls back to the polling mode:
893
- # a streaming event means the configuration has changed, so staying in the
894
- # streaming mode would leave the SDK serving the outdated configuration until
895
- # the next event arrives. The server-provided settings are not mutated, so a
896
- # later successful fetch switches back to streaming when requested.
896
+ # Fetches the configuration in a background thread; used for the initial fetch and
897
+ # the streaming events, while the polling job fetches in its own thread.
897
898
  def fetch_configuration_job(time_stamp = nil)
898
- Thread.new do
899
- cause = nil
900
- ok = false
901
- begin
902
- ok = obtain_configuration(time_stamp)
903
- rescue StandardError => e
904
- cause = e
905
- Logging::KameleoonLogger.error('Error occurred during configuration fetching: %s', e)
906
- end
907
- settle_readiness(ok, cause)
908
- manage_configuration_update(ok && @data_manager.data_file.settings.real_time_update)
899
+ Thread.new { fetch_configuration_sync(time_stamp) }
900
+ end
901
+
902
+ # Fetches the configuration and applies the outcome. The fetch outcome settles the
903
+ # client readiness, and a failed fetch always falls back to the polling mode: a
904
+ # streaming event means the configuration has changed, so staying in the streaming
905
+ # mode would leave the SDK serving the outdated configuration until the next event
906
+ # arrives. The server-provided settings are not mutated, so a later successful fetch
907
+ # switches back to streaming when requested.
908
+ def fetch_configuration_sync(time_stamp = nil)
909
+ cause = nil
910
+ ok = false
911
+ begin
912
+ ok = obtain_configuration(time_stamp)
913
+ rescue StandardError => e
914
+ cause = e
915
+ Logging::KameleoonLogger.error('Error occurred during configuration fetching: %s', e)
909
916
  end
917
+ settle_readiness(ok, cause)
918
+ manage_configuration_update(ok && @data_manager.data_file.settings.streaming_mode?)
910
919
  end
911
920
 
912
921
  # A successful fetch marks the SDK ready, a failed one reports the failure so
@@ -921,13 +930,18 @@ module Kameleoon
921
930
  end
922
931
  end
923
932
 
924
- def start_polling_datafile_update
933
+ # @param first_in [Numeric | NilClass] Delay in seconds before the first poll; the refresh interval by
934
+ # default. Ignored while the polling is already scheduled.
935
+ def start_polling_datafile_update(first_in = nil)
925
936
  return unless @fetch_configuration_update_job.nil?
926
937
 
927
- @fetch_configuration_update_job = @scheduler.schedule_every @config.refresh_interval_second do
928
- Logging::KameleoonLogger.info('Scheduled job to fetch configuration is started.')
929
- fetch_configuration_job
930
- end
938
+ # The fetch runs in the job's thread, so `overlap: false` skips the ticks which come while a slow
939
+ # fetch is still in progress instead of piling up fetches
940
+ @fetch_configuration_update_job =
941
+ @scheduler.schedule_every(@config.refresh_interval_second, first_in: first_in, overlap: false) do
942
+ Logging::KameleoonLogger.info('Scheduled job to fetch configuration is started.')
943
+ fetch_configuration_sync
944
+ end
931
945
  end
932
946
 
933
947
  def stop_polling_datafile_update
@@ -938,34 +952,76 @@ module Kameleoon
938
952
  Logging::KameleoonLogger.info('Scheduled job to fetch configuration is stopped.')
939
953
  end
940
954
 
941
- def start_real_time_event_service_if_needed
942
- return unless @real_time_event_service.nil?
955
+ # Returns whether the service runs. A service which gave up reconnecting is replaced right away; its
956
+ # pending fallback notification is then ignored, see `on_streaming_unavailable`.
957
+ def start_streaming_service
958
+ return true unless @streaming_service.nil? || @streaming_service.closed?
943
959
 
944
960
  url = @network_manager.url_provider.make_real_time_url
945
- fetch_func = proc { |real_time_event| fetch_configuration_job(real_time_event.time_stamp) }
946
- @real_time_event_service =
947
- Kameleoon::RealTime::RealTimeEventService.new(url, fetch_func)
961
+ @streaming_service = Kameleoon::Network::Streaming::StreamingService.new(
962
+ url, method(:on_data_file_update_message), streaming_unavailable_handler: method(:on_streaming_unavailable)
963
+ )
964
+ true
965
+ rescue StandardError => e # e.g. `ThreadError` when no thread can be created
966
+ Logging::KameleoonLogger.error('Failed to start the streaming service: %s; switching to polling', e)
967
+ false
968
+ end
969
+
970
+ # Every event (a configuration update, or the connection open event sent on every (re)connection)
971
+ # carries the timestamp of the current configuration, so the SDK fetches it only when it is newer
972
+ # than the one it holds.
973
+ def on_data_file_update_message(message)
974
+ time_stamp = message.time_stamp
975
+ date_modified = @data_manager.data_file.date_modified
976
+ if time_stamp <= date_modified
977
+ Logging::KameleoonLogger.debug(
978
+ 'Configuration is up to date (ts: %s, dateModified: %s), skipping update', time_stamp, date_modified
979
+ )
980
+ return
981
+ end
982
+ fetch_configuration_job(time_stamp)
948
983
  end
949
984
 
950
- def stop_real_time_event_service_if_needed
951
- @real_time_event_service&.close
952
- @real_time_event_service = nil
985
+ # Called by the service which gave up reconnecting. A fetch completing meanwhile may have replaced
986
+ # the service already, in which case there is nothing to fall back from.
987
+ def on_streaming_unavailable(service)
988
+ @configuration_update_mutex.synchronize do
989
+ return if @disposed || !service.equal?(@streaming_service)
990
+
991
+ stop_streaming_service
992
+ start_polling_datafile_update(streaming_retry_delay)
993
+ end
994
+ end
995
+
996
+ def streaming_retry_delay
997
+ [STREAMING_RETRY_DELAY_SECONDS, @config.refresh_interval_second].min
998
+ end
999
+
1000
+ def stop_streaming_service
1001
+ @streaming_service&.close
1002
+ @streaming_service = nil
953
1003
  end
954
1004
 
955
1005
  # A disposed client must not (re)start the update services: a fetch which was
956
1006
  # in flight when `dispose` ran would otherwise resurrect them. `@disposed` is
957
1007
  # set before `dispose` stops the services, so a fetch completing after that
958
- # point can only stop things.
959
- def manage_configuration_update(is_real_time_update)
960
- if @disposed
961
- stop_polling_datafile_update
962
- stop_real_time_event_service_if_needed
963
- elsif is_real_time_update
964
- stop_polling_datafile_update
965
- start_real_time_event_service_if_needed
966
- else
967
- stop_real_time_event_service_if_needed
968
- start_polling_datafile_update
1008
+ # point can only stop things. The polling is stopped only once the streaming
1009
+ # runs, so that a failed start never leaves the SDK without updates. Whenever
1010
+ # the current settings enable streaming but it is not running (a failed fetch,
1011
+ # a failed start), the first poll comes soon, so that streaming is retried
1012
+ # without waiting a full refresh interval.
1013
+ def manage_configuration_update(streaming_mode)
1014
+ @configuration_update_mutex.synchronize do
1015
+ if @disposed
1016
+ stop_polling_datafile_update
1017
+ stop_streaming_service
1018
+ elsif streaming_mode && start_streaming_service
1019
+ stop_polling_datafile_update
1020
+ else
1021
+ stop_streaming_service
1022
+ first_in = @data_manager.data_file.settings.streaming_mode? ? streaming_retry_delay : nil
1023
+ start_polling_datafile_update(first_in)
1024
+ end
969
1025
  end
970
1026
  end
971
1027
 
@@ -1000,13 +1056,27 @@ module Kameleoon
1000
1056
  true
1001
1057
  end
1002
1058
 
1059
+ # The data file is only ever replaced by one at least as new: fetches may run concurrently (a
1060
+ # streaming event can arrive while another fetch is in flight) and finish out of order, and a stale
1061
+ # reply must not roll the configuration back. The comparison and the replacement are atomic, and
1062
+ # the update event fires under the same lock, so handlers see the data files in the applied order.
1003
1063
  def apply_new_configuration(data_file, source)
1004
1064
  Logging::KameleoonLogger.debug('CALL: KameleoonClient.apply_new_configuration(data_file: %s, source: %s)',
1005
1065
  data_file, source)
1006
- @data_manager.data_file = data_file
1007
- @network_manager.url_provider.apply_data_api_domain(data_file.settings.data_api_domain)
1008
- @event_manager.fire_data_file_update(Events::DataFileUpdateEvent.new(source, data_file.date_modified))
1009
- call_deprecated_update_handler if source == Events::DataFileUpdateEvent::Source::STREAMING
1066
+ @apply_configuration_mutex.synchronize do
1067
+ current_date_modified = @data_manager.data_file.date_modified
1068
+ if data_file.date_modified < current_date_modified
1069
+ Logging::KameleoonLogger.info(
1070
+ 'Fetched configuration (dateModified: %s) is older than the current one (dateModified: %s), skipping',
1071
+ data_file.date_modified, current_date_modified
1072
+ )
1073
+ else
1074
+ @data_manager.data_file = data_file
1075
+ @network_manager.url_provider.apply_data_api_domain(data_file.settings.data_api_domain)
1076
+ @event_manager.fire_data_file_update(Events::DataFileUpdateEvent.new(source, data_file.date_modified))
1077
+ call_deprecated_update_handler if source == Events::DataFileUpdateEvent::Source::STREAMING
1078
+ end
1079
+ end
1010
1080
  Logging::KameleoonLogger.debug('RETURN: KameleoonClient.apply_new_configuration(data_file: %s, source: %s)',
1011
1081
  data_file, source)
1012
1082
  end
@@ -1028,11 +1098,14 @@ module Kameleoon
1028
1098
  def dispose(_object_id = nil)
1029
1099
  Logging::KameleoonLogger.debug('CALL: KameleoonClient.dispose')
1030
1100
  @disposed = true
1031
- stop_polling_datafile_update
1032
- stop_real_time_event_service_if_needed
1101
+ @configuration_update_mutex.synchronize do
1102
+ stop_polling_datafile_update
1103
+ stop_streaming_service
1104
+ end
1033
1105
  @visitor_manager.stop
1034
1106
  @tracking_manager.stop
1035
1107
  @scheduler.shutdown
1108
+ @network_manager.close
1036
1109
  Logging::KameleoonLogger.debug('RETURN: KameleoonClient.dispose')
1037
1110
  end
1038
1111
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'concurrent/executor/thread_pool_executor'
3
4
  require 'kameleoon/data/data'
4
5
  require 'kameleoon/logging/kameleoon_logger'
5
6
  require 'kameleoon/managers/tracking/tracking_builder'
@@ -12,9 +13,13 @@ module Kameleoon
12
13
  LINES_DELIMETER = "\n"
13
14
  REQUEST_SIZE_LIMIT = 2560 * 1024 # 2.5 * 1024^2 characters
14
15
  PROBE_MAX_VISITORS = 1
16
+ MAX_SEND_THREADS = 8
17
+ MAX_QUEUED_REQUESTS = 32
18
+ SEND_THREAD_IDLE_SECONDS = 60
19
+ STOP_WAIT_SECONDS = 1
15
20
 
16
21
  def initialize(
17
- data_manager, network_manager, visitor_manager, track_interval_seconds, scheduler
22
+ data_manager, network_manager, visitor_manager, track_interval_seconds, scheduler, executor: nil
18
23
  )
19
24
  Logging::KameleoonLogger.debug(
20
25
  'CALL: TrackingManager.new(data_manager, network_manager. visitor_manager, ' \
@@ -25,6 +30,10 @@ module Kameleoon
25
30
  @data_manager = data_manager
26
31
  @network_manager = network_manager
27
32
  @visitor_manager = visitor_manager
33
+ @executor = executor || Concurrent::ThreadPoolExecutor.new(
34
+ name: 'kameleoon-tracking', min_threads: 0, max_threads: MAX_SEND_THREADS,
35
+ max_queue: MAX_QUEUED_REQUESTS, idletime: SEND_THREAD_IDLE_SECONDS, fallback_policy: :abort
36
+ )
28
37
  @track_all_job = scheduler.schedule_every(track_interval_seconds, method(:track_all)) unless scheduler.nil?
29
38
  Logging::KameleoonLogger.debug(
30
39
  'RETURN: TrackingManager.new(data_manager, network_manager. visitor_manager, ' \
@@ -36,6 +45,10 @@ module Kameleoon
36
45
  Logging::KameleoonLogger.debug('CALL: TrackingManager.stop')
37
46
  @track_all_job&.unschedule
38
47
  @track_all_job = nil
48
+ # Queued requests still run; a later `track` is rejected and keeps its data (see `perform_tracking_request`).
49
+ # The wait lets in-flight requests finish before the client disposal closes the connections.
50
+ @executor.shutdown
51
+ @executor.wait_for_termination(STOP_WAIT_SECONDS)
39
52
  Logging::KameleoonLogger.debug('RETURN: TrackingManager.stop')
40
53
  end
41
54
 
@@ -97,25 +110,42 @@ module Kameleoon
97
110
  lines = tracking_lines.join(LINES_DELIMETER)
98
111
  # mark unsent data as transmitted
99
112
  visitor_data.each { |d| d.mark_as_transmitting if d.is_a?(Kameleoon::Data) }
100
- Thread.new do
101
- result = @network_manager.send_tracking_data(lines)
102
- if result != false
103
- Logging::KameleoonLogger.info('Successful request for tracking visitors: %s, data: %s',
104
- visitor_codes, visitor_data)
105
- visitor_data.each { |d| d.mark_as_sent if d.is_a?(Kameleoon::Data) }
106
- leave_probe_mode
107
- else
108
- Logging::KameleoonLogger.error('Tracking request failed')
109
- Logging::KameleoonLogger.info('Failed request for tracking visitors: %s, data: %s',
110
- visitor_codes, visitor_data)
111
- visitor_data.each { |d| d.mark_as_unsent if d.is_a?(Kameleoon::Data) }
112
- enter_probe_mode
113
- @tracking_visitors.add_all(visitor_codes)
114
- end
113
+ begin
114
+ @executor.post { send_tracking_request(visitor_codes, visitor_data, lines) }
115
+ rescue Concurrent::RejectedExecutionError
116
+ Logging::KameleoonLogger.warning(
117
+ 'Tracking request pool is full or stopped: data of %s visitors is kept to be sent later',
118
+ visitor_codes.size
119
+ )
120
+ keep_for_later(visitor_codes, visitor_data)
115
121
  end
116
122
  true
117
123
  end
118
124
 
125
+ def send_tracking_request(visitor_codes, visitor_data, lines)
126
+ result = @network_manager.send_tracking_data(lines)
127
+ if result != false
128
+ Logging::KameleoonLogger.info('Successful request for tracking visitors: %s, data: %s',
129
+ visitor_codes, visitor_data)
130
+ visitor_data.each { |d| d.mark_as_sent if d.is_a?(Kameleoon::Data) }
131
+ leave_probe_mode
132
+ else
133
+ Logging::KameleoonLogger.error('Tracking request failed')
134
+ Logging::KameleoonLogger.info('Failed request for tracking visitors: %s, data: %s',
135
+ visitor_codes, visitor_data)
136
+ enter_probe_mode
137
+ keep_for_later(visitor_codes, visitor_data)
138
+ end
139
+ rescue StandardError => e
140
+ Logging::KameleoonLogger.error('Error occurred within tracking request: %s', e)
141
+ keep_for_later(visitor_codes, visitor_data)
142
+ end
143
+
144
+ def keep_for_later(visitor_codes, visitor_data)
145
+ visitor_data.each { |d| d.mark_as_unsent if d.is_a?(Kameleoon::Data) }
146
+ @tracking_visitors.add_all(visitor_codes)
147
+ end
148
+
119
149
  def enter_probe_mode
120
150
  return if @probe_mode
121
151
 
@@ -3,7 +3,6 @@
3
3
  module Kameleoon
4
4
  module Network
5
5
  module ContentType
6
- WILDCARD = '*/*'
7
6
  TEXT = 'text/plain'
8
7
  JSON = 'application/json'
9
8
  FORM = 'application/x-www-form-urlencoded'
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'net/http'
4
+ require 'net/http/persistent'
4
5
  require 'kameleoon/version'
5
6
  require 'kameleoon/network/response'
7
+ require 'kameleoon/logging/kameleoon_logger'
6
8
  require 'kameleoon/exceptions'
7
9
 
8
10
  module Kameleoon
@@ -12,6 +14,9 @@ module Kameleoon
12
14
  raise KameleoonError, 'Call of not implemented method!'
13
15
  end
14
16
 
17
+ # Releases the resources held by the provider (e.g. kept-alive connections); no-op by default.
18
+ def close; end
19
+
15
20
  private
16
21
 
17
22
  def collect_headers(request)
@@ -29,7 +34,27 @@ module Kameleoon
29
34
  end
30
35
  end
31
36
 
37
+ # Performs requests over kept-alive connections managed by `Net::HTTP::Persistent`.
38
+ #
39
+ # `Net::HTTP::Persistent` timeouts are per client, while the SDK timeout is per request, so one client (with its
40
+ # own connection pool) is kept per distinct timeout value. In practice that is the default timeout plus the few
41
+ # values callers pass explicitly.
32
42
  class SyncNetProvider < NetProvider
43
+ # Idle connections are reconnected before use after this long; keeps a request from hitting a connection the
44
+ # server has already dropped.
45
+ IDLE_TIMEOUT = 15 # seconds
46
+ # Guards against a caller passing a different timeout on every request (each value would keep its own idle
47
+ # connections). Requests with further timeout values are sent on a one-off connection with their own timeout.
48
+ MAX_CLIENTS = 16
49
+
50
+ def initialize
51
+ super()
52
+ @clients = {}
53
+ @clients_mutex = Mutex.new
54
+ @overflow_warned = false
55
+ @closed = false
56
+ end
57
+
33
58
  def make_request(request)
34
59
  for _ in 0..8 do
35
60
  response = make_single_request(request)
@@ -44,10 +69,17 @@ module Kameleoon
44
69
  response
45
70
  end
46
71
 
72
+ def close
73
+ @clients_mutex.synchronize do
74
+ @closed = true
75
+ @clients.each_value(&:shutdown)
76
+ @clients.clear
77
+ end
78
+ end
79
+
47
80
  private
48
81
 
49
82
  def make_single_request(request)
50
- resp = nil
51
83
  case request.method
52
84
  when Method::GET
53
85
  req = Net::HTTP::Get.new(request.url)
@@ -57,21 +89,71 @@ module Kameleoon
57
89
  else
58
90
  return unknown_method_response(request.method, request)
59
91
  end
60
- timeout = request.timeout.to_f / 1000.0
61
- headers = collect_headers(request)
62
- headers.each { |k, v| req[k] = v }
92
+ collect_headers(request).each { |k, v| req[k] = v }
63
93
  uri = URI(request.url)
64
- Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, open_timeout: timeout,
65
- read_timeout: timeout, ssl_timeout: timeout) do |http|
66
- resp = http.request(req)
67
- end
94
+ timeout = request.timeout.to_f / 1000.0
95
+ client = client_for(timeout)
96
+ resp = client ? client.request(uri, req) : request_once(uri, req, timeout)
68
97
  body = resp.body
69
98
  body = nil if body&.empty?
70
- headers = resp.to_hash
71
- Response.new(nil, resp.code.to_i, body, headers, request)
99
+ Response.new(nil, resp.code.to_i, body, resp.to_hash, request)
72
100
  rescue StandardError => e
73
101
  Response.new(e, nil, nil, nil, request)
74
102
  end
103
+
104
+ def client_for(timeout)
105
+ @clients_mutex.synchronize do
106
+ return nil if @closed
107
+
108
+ client = @clients[timeout]
109
+ return client if client
110
+
111
+ if @clients.size >= MAX_CLIENTS
112
+ unless @overflow_warned
113
+ @overflow_warned = true
114
+ Logging::KameleoonLogger.warning(
115
+ 'More than %s distinct request timeouts in use; requests with further timeout values ' \
116
+ 'are sent without connection reuse', MAX_CLIENTS
117
+ )
118
+ end
119
+ return nil
120
+ end
121
+
122
+ @clients[timeout] = build_client(timeout)
123
+ end
124
+ end
125
+
126
+ # Sends the request on a connection which is closed afterwards, honouring `timeout` (seconds).
127
+ def request_once(uri, req, timeout)
128
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https', open_timeout: timeout,
129
+ read_timeout: timeout, write_timeout: timeout) do |http|
130
+ http.request(req)
131
+ end
132
+ end
133
+
134
+ # The timeout is applied as given, also when it is zero or negative (as `Net::HTTP` did before pooling):
135
+ # validating it is the callers' concern.
136
+ def build_client(timeout)
137
+ http = Net::HTTP::Persistent.new(name: "kameleoon-sdk-#{timeout}", proxy: proxy_setting)
138
+ http.idle_timeout = IDLE_TIMEOUT
139
+ http.open_timeout = timeout
140
+ http.read_timeout = timeout
141
+ http.write_timeout = timeout
142
+ http
143
+ end
144
+
145
+ # `:ENV` keeps the `http_proxy`/`no_proxy` support that plain `Net::HTTP` has by default. net-http-persistent
146
+ # 4.0.8 parses `no_proxy` with `Array#filter_map` (Ruby 2.7+), so on older Rubies with `no_proxy` set the proxy
147
+ # URI is passed directly, without the bypass list (Kameleoon hosts are not expected to be in it anyway).
148
+ def proxy_setting
149
+ no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']
150
+ return :ENV if RUBY_VERSION >= '2.7' || no_proxy.nil? || no_proxy.empty?
151
+
152
+ env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
153
+ return nil if env_proxy.nil? || env_proxy.empty? || no_proxy == '*'
154
+
155
+ URI(env_proxy.match?(%r{\Ahttps?://}) ? env_proxy : "http://#{env_proxy}")
156
+ end
75
157
  end
76
158
  end
77
159
  end
@@ -69,7 +69,7 @@ module Kameleoon
69
69
 
70
70
  url = @url_provider.make_tracking_url
71
71
  timeout = ensure_timeout(timeout)
72
- request = Request.new(Method::POST, url, ContentType::WILDCARD, timeout, data: lines)
72
+ request = Request.new(Method::POST, url, ContentType::TEXT, timeout, data: lines)
73
73
  unwrap_response(*make_call(Events::RequestType::TRACKING, request, true,
74
74
  TRACKING_CALL_ATTEMPT_NUMBER - 1, TRACKING_CALL_RETRY_DELAY))
75
75
  end
@@ -86,6 +86,12 @@ module Kameleoon
86
86
  unwrap_response(*make_call(Events::RequestType::ACCESS_TOKEN, request, false))
87
87
  end
88
88
 
89
+ # Closes the kept-alive connections. A request still running completes on its connection; a request started
90
+ # afterwards fails with an error response.
91
+ def close
92
+ @sync_net_provider.close
93
+ end
94
+
89
95
  private
90
96
 
91
97
  def make_call(request_type, request, try_access_token_auth, retry_limit = 0, retry_delay = 0)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Kameleoon Data File Update Message
4
+
5
+ module Kameleoon
6
+ module Network
7
+ module Streaming
8
+ ##
9
+ # DataFileUpdateMessage contains information about timestamp when configuration was updated.
10
+ # Timestamp parameter is used to fetch the latest configuration.
11
+ class DataFileUpdateMessage
12
+ attr_reader :time_stamp
13
+
14
+ ##
15
+ # Parametrized initializer. Gets the new time stamp from a JSON message.
16
+ #
17
+ # @param event_dict [Hash] Hash of SSE message data.
18
+ def initialize(event_dict)
19
+ # Both hold milliseconds; `ts` is only read while the server still sends events without `tsMs`
20
+ @time_stamp = event_dict['tsMs'] || event_dict['ts']
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end