Skip to content

Commit

Permalink
feat: unleash-interval header
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Feb 28, 2025
1 parent 978dfe2 commit db3b949
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def set_defaults
self.project_name = nil
self.disable_client = false
self.disable_metrics = false
self.refresh_interval = 10
self.refresh_interval = 15
self.metrics_interval = 60
self.timeout = 30
self.retry_limit = Float::INFINITY
Expand Down
4 changes: 3 additions & 1 deletion lib/unleash/metrics_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def post
return
end

response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, report.to_json)
headers = (Unleash.configuration.http_headers || {}).dup
headers.merge!( { 'UNLEASH-INTERVAL' => Unleash.configuration.metrics_interval.to_s })
response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, report.to_json, headers)

if ['200', '202'].include? response.code
Unleash.logger.debug "Report sent to unleash server successfully. Server responded with http code #{response.code}"
Expand Down
4 changes: 3 additions & 1 deletion lib/unleash/toggle_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def fetch
Unleash.logger.debug "fetch()"
return if Unleash.configuration.disable_client

response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag)
headers = (Unleash.configuration.http_headers || {}).dup
headers.merge!( { 'UNLEASH-INTERVAL' => Unleash.configuration.refresh_interval.to_s })
response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag, headers)

if response.code == '304'
Unleash.logger.debug "No changes according to the unleash server, nothing to do."
Expand Down
4 changes: 2 additions & 2 deletions lib/unleash/util/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def self.get(uri, etag = nil, headers_override = nil)
http.request(request)
end

def self.post(uri, body)
def self.post(uri, body, headers_override = nil)
http = http_connection(uri)

request = Net::HTTP::Post.new(uri.request_uri, http_headers)
request = Net::HTTP::Post.new(uri.request_uri, http_headers(nil, headers_override))
request.body = body

http.request(request)
Expand Down
23 changes: 16 additions & 7 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}"
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'Unleash-Interval' => '60'
}
)
.to_return(status: 200, body: "", headers: {})
Expand All @@ -56,7 +57,8 @@
'Unleash-Connection-Id' => fixed_uuid,
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: simple_features.to_json, headers: {})
Expand Down Expand Up @@ -92,6 +94,7 @@
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'UNLEASH-CONNECTION-ID': fixed_uuid })
.with(headers: { 'UNLEASH-INTERVAL': '15' })
).to have_been_made.once

# Test now sending of metrics
Expand All @@ -104,6 +107,7 @@
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'UNLEASH-CONNECTION-ID': fixed_uuid })
.with(headers: { 'UNLEASH-INTERVAL': '60' })
).not_to have_been_made

# Sending metrics, if they have been evaluated:
Expand All @@ -117,6 +121,7 @@
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'UNLEASH-CONNECTION-ID': fixed_uuid })
.with(headers: { 'UNLEASH-INTERVAL': '60' })
.with{ |request| JSON.parse(request.body)['bucket']['toggles']['Feature.A']['yes'] == 2 }
).to have_been_made.once
end
Expand Down Expand Up @@ -174,7 +179,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: features_response_body, headers: {})
Expand Down Expand Up @@ -273,7 +279,7 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
}
)
.to_return(status: 200, body: "", headers: {})
Expand All @@ -288,7 +294,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: "", headers: {})
Expand Down Expand Up @@ -348,7 +355,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: features_response_body, headers: {})
Expand Down Expand Up @@ -642,7 +650,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: body, headers: {})
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
expect(config.custom_http_headers).to eq({})
expect(config.disable_metrics).to be_falsey

expect(config.refresh_interval).to eq(10)
expect(config.refresh_interval).to eq(15)
expect(config.metrics_interval).to eq(60)
expect(config.timeout).to eq(30)
expect(config.retry_limit).to eq(Float::INFINITY)
Expand Down
3 changes: 2 additions & 1 deletion spec/unleash/metrics_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
'Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}"
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'Unleash-Interval' => "60"
}
)
.to_return(status: 200, body: "", headers: {})
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/toggle_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
describe '#fetch!' do
let(:engine) { YggdrasilEngine.new }

context 'when fetching toggles succeds' do
context 'when fetching toggles succeeds' do
before do
_toggle_fetcher = described_class.new engine
end
Expand Down

0 comments on commit db3b949

Please sign in to comment.