Skip to content
Closed
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
1 change: 1 addition & 0 deletions launchdarkly-server-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
23 changes: 18 additions & 5 deletions lib/ldclient-rb/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/ldclient-rb/impl/diagnostic_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/ldclient-rb/impl/event_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion lib/ldclient-rb/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions spec/event_sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,26 +107,28 @@ 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

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|
Expand Down
9 changes: 7 additions & 2 deletions spec/http_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}))
Expand Down Expand Up @@ -129,4 +134,4 @@ def initialize(ports = {})
def open(uri, timeout)
TCPSocket.new 'localhost', @ports[uri]
end
end
end
3 changes: 2 additions & 1 deletion spec/requestor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" } } }
Expand Down Expand Up @@ -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
Expand Down