Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bootstrapped variants when client is disabled #138

Merged
merged 5 commits into from
May 15, 2023
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
5 changes: 0 additions & 5 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ def if_disabled(feature, context = nil, default_value = true, &blk)
def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}"

if Unleash.configuration.disable_client
Unleash.logger.debug "unleash_client is disabled! Always returning #{fallback_variant} for feature #{feature}!"
return fallback_variant
end

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first

if toggle_as_hash.nil?
Expand Down
13 changes: 9 additions & 4 deletions lib/unleash/feature_toggle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ def sum_variant_defs_weights
end

def variant_salt(context, stickiness = "default")
return context.get_by_name(stickiness) unless stickiness == "default"
return context.user_id unless context.user_id.to_s.empty?
return context.session_id unless context.session_id.to_s.empty?
return context.remote_address unless context.remote_address.to_s.empty?
begin
return context.get_by_name(stickiness) if !context.nil? && stickiness != "default"
rescue KeyError
Unleash.logger.warn "Custom stickiness key (#{stickiness}) not found in the provided context #{context}. " \
"Falling back to default behavior."
end
return context.user_id unless context&.user_id.to_s.empty?
return context.session_id unless context&.session_id.to_s.empty?
return context.remote_address unless context&.remote_address.to_s.empty?

SecureRandom.random_number
end
Expand Down
26 changes: 26 additions & 0 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@
"name": "featureX",
"enabled": true,
"strategies": [{ "name": "default" }]
},
{
"enabled": true,
"name": "featureVariantX",
"strategies": [{ "name": "default" }],
"variants": [
{
"name": "default-value",
"payload": {
"type": "string",
"value": "info"
},
"stickiness": "custom_attribute",
"weight": 100,
"weightType": "variable"
}
]
}
]
}'
Expand All @@ -209,6 +226,15 @@
unleash_client.is_enabled?('featureX', {}, false)
).to be true

default_variant = Unleash::Variant.new(
name: 'featureVariantX',
enabled: false,
payload: { type: 'string', value: 'bogus' }
)
variant = unleash_client.get_variant('featureVariantX', nil, default_variant)
expect(variant.enabled).to be true
expect(variant.payload.fetch('value')).to eq('info')

expect(WebMock).not_to have_requested(:get, 'http://test-url/')
expect(WebMock).not_to have_requested(:post, 'http://test-url/client/register')
expect(WebMock).not_to have_requested(:get, 'http://test-url/client/features')
Expand Down
Loading