diff --git a/launchdarkly-server-sdk.gemspec b/launchdarkly-server-sdk.gemspec index 67125390..196e8a53 100644 --- a/launchdarkly-server-sdk.gemspec +++ b/launchdarkly-server-sdk.gemspec @@ -31,6 +31,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "timecop", "~> 0.9" spec.add_development_dependency "listen", "~> 3.3" # see file_data_source.rb spec.add_development_dependency "webrick", "~> 1.7" + spec.add_development_dependency "byebug" # required by dynamodb spec.add_development_dependency "oga", "~> 2.2" diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index 3cfbf882..ebe367ca 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -98,6 +98,19 @@ def initialize(opts = {}) # attr_reader :events_uri + # + # Whether the client has detected any HTTP or HTTPS proxy settings. This doesn't mean a proxy + # will always be used as the no_proxy settings are also respected when it comes to perform a + # request. + # @return [Boolean] + # + def using_proxy? + ENV.has_key?('http_proxy') || + ENV.has_key?('https_proxy') || + ENV.has_key?('HTTP_PROXY') || + ENV.has_key?('HTTPS_PROXY') + end + # # Whether streaming mode should be enabled. Streaming mode asynchronously updates # feature flags in real-time using server-sent events. Streaming is enabled by default, and @@ -122,7 +135,7 @@ def stream? def use_ldd? @use_ldd end - + # # Whether the client should be initialized in offline mode. In offline mode, default values are # returned for all flags and no remote network requests are made. @@ -219,7 +232,7 @@ def offline? # @see #all_attributes_private # attr_reader :private_attribute_names - + # # Whether to send events back to LaunchDarkly. This differs from {#offline?} in that it affects # only the sending of client-side events, not streaming or polling for events from the server. @@ -277,7 +290,7 @@ def offline? # @deprecated This is replaced by {#data_source}. attr_reader :update_processor - + # @deprecated This is replaced by {#data_source}. attr_reader :update_processor_factory @@ -409,8 +422,8 @@ def self.default_connect_timeout # def self.default_logger if defined?(Rails) && Rails.respond_to?(:logger) - Rails.logger - else + Rails.logger + else log = ::Logger.new($stdout) log.level = ::Logger::WARN log diff --git a/lib/ldclient-rb/impl/diagnostic_events.rb b/lib/ldclient-rb/impl/diagnostic_events.rb index 13a55756..acdd42cc 100644 --- a/lib/ldclient-rb/impl/diagnostic_events.rb +++ b/lib/ldclient-rb/impl/diagnostic_events.rb @@ -34,7 +34,7 @@ def create_init_event(config) platform: DiagnosticAccumulator.make_platform_data } end - + def record_stream_init(timestamp, failed, duration_millis) @lock.synchronize do @stream_inits.push({ timestamp: timestamp, failed: failed, durationMillis: duration_millis }) @@ -79,7 +79,7 @@ def self.make_config_data(config) streamingDisabled: !config.stream?, userKeysCapacity: config.user_keys_capacity, userKeysFlushIntervalMillis: self.seconds_to_millis(config.user_keys_flush_interval), - usingProxy: ENV.has_key?('http_proxy') || ENV.has_key?('https_proxy') || ENV.has_key?('HTTP_PROXY') || ENV.has_key?('HTTPS_PROXY'), + usingProxy: config.using_proxy?, usingRelayDaemon: config.use_ldd?, } ret diff --git a/lib/ldclient-rb/impl/event_sender.rb b/lib/ldclient-rb/impl/event_sender.rb index 442af033..8c2836df 100644 --- a/lib/ldclient-rb/impl/event_sender.rb +++ b/lib/ldclient-rb/impl/event_sender.rb @@ -47,6 +47,7 @@ def send_event_data(event_data, description, is_diagnostic) headers["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s headers["X-LaunchDarkly-Payload-ID"] = payload_id end + require 'byebug'; byebug response = http_client.request("POST", uri, { headers: headers, body: event_data diff --git a/lib/ldclient-rb/util.rb b/lib/ldclient-rb/util.rb index 7bd56959..70598066 100644 --- a/lib/ldclient-rb/util.rb +++ b/lib/ldclient-rb/util.rb @@ -21,6 +21,24 @@ def self.stringify_attrs(hash, attrs) def self.new_http_client(uri_s, config) http_client_options = {} + + uri = URI(uri_s) + if config.using_proxy? + if uri.scheme == 'http' && ENV["http_proxy"] || ENV["HTTP_PROXY"] + proxy_uri = URI(ENV["http_proxy"] || ENV["HTTP_PROXY"]) + elsif uri.scheme == 'https' && ENV["https_proxy"] || ENV["HTTPS_PROXY"] + proxy_uri = URI(ENV["https_proxy"] || ENV["HTTPS_PROXY"]) + end + + if proxy_uri + # TODO: respect no_proxy settings + http_client_options["proxy"] = { + :proxy_address => proxy_uri.host, + :proxy_port => proxy_uri.port + } + end + end + if config.socket_factory http_client_options["socket_class"] = config.socket_factory end @@ -29,7 +47,7 @@ def self.new_http_client(uri_s, config) read: config.read_timeout, connect: config.connect_timeout }) - .persistent(uri_s) + .persistent(uri) end def self.log_exception(logger, message, exc) diff --git a/spec/event_sender_spec.rb b/spec/event_sender_spec.rb index 31bfb6ae..76d43561 100644 --- a/spec/event_sender_spec.rb +++ b/spec/event_sender_spec.rb @@ -14,7 +14,7 @@ module Impl let(:fake_data) { '{"things":[]}' } def make_sender(server) - subject.new(sdk_key, Config.new(events_uri: server.base_uri.to_s, logger: $null_log), nil, 0.1) + subject.new(sdk_key, Config.new(events_uri: server.base_uri.to_s, logger: Logger.new($stdout)), nil, 0.1) end def with_sender_and_server @@ -107,7 +107,7 @@ def with_sender_and_server it "can use a proxy server" do with_server do |server| server.setup_ok_response("/bulk", "") - + # require 'byebug'; byebug with_server(StubProxyServer.new) do |proxy| begin ENV["http_proxy"] = proxy.base_uri.to_s @@ -115,18 +115,20 @@ def with_sender_and_server es = make_sender(server) result = es.send_event_data(fake_data, "", false) - + require 'byebug'; byebug expect(result.success).to be true req, body = server.await_request_with_body expect(body).to eq fake_data + expect(proxy.request_count).to eq(1) ensure ENV["http_proxy"] = nil end end + end end - + [400, 408, 429, 500].each do |status| it "handles recoverable error #{status}" do with_sender_and_server do |es, server| diff --git a/spec/http_util.rb b/spec/http_util.rb index 1a789772..a182fe74 100644 --- a/spec/http_util.rb +++ b/spec/http_util.rb @@ -14,7 +14,7 @@ def initialize BindAddress: '127.0.0.1', Port: @port, AccessLog: [], - Logger: NullLogger.new, + Logger: Logger.new($stdout, progname: self.class.to_s), RequestCallback: method(:record_request) } @server = create_server(@port, base_opts) @@ -61,6 +61,7 @@ def setup_status_response(uri_path, status, headers={}) def setup_ok_response(uri_path, body, content_type=nil, headers={}) setup_response(uri_path) do |req, res| + require 'byebug'; byebug res.status = 200 res.content_type = content_type if !content_type.nil? res.body = body @@ -69,6 +70,7 @@ def setup_ok_response(uri_path, body, content_type=nil, headers={}) end def record_request(req, res) + # require 'byebug'; byebug @requests.push(req) @requests_queue << [req, req.body] end @@ -99,6 +101,9 @@ def create_server(port, base_opts) if !@connect_status.nil? res.status = @connect_status end + puts '*' * 80 + + require 'byebug'; byebug @request_count += 1 end })) @@ -129,4 +134,4 @@ def initialize(ports = {}) def open(uri, timeout) TCPSocket.new 'localhost', @ports[uri] end -end \ No newline at end of file +end diff --git a/spec/requestor_spec.rb b/spec/requestor_spec.rb index c224b22a..6cb3ead1 100644 --- a/spec/requestor_spec.rb +++ b/spec/requestor_spec.rb @@ -84,7 +84,7 @@ def with_requestor(base_uri, opts = {}) end end end - + it "can reuse cached data" do etag = "xyz" expected_data = { flags: { x: { key: "x" } } } @@ -199,6 +199,7 @@ def with_requestor(base_uri, opts = {}) data = requestor.request_all_data expect(data).to eq(LaunchDarkly::Impl::Model.make_all_store_data(expected_data)) end + expect(proxy.request_count).to eq(1) ensure ENV["http_proxy"] = nil end