diff --git a/lib/unleash/client.rb b/lib/unleash/client.rb index 62b75f35..c4f9d86a 100644 --- a/lib/unleash/client.rb +++ b/lib/unleash/client.rb @@ -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? diff --git a/lib/unleash/feature_toggle.rb b/lib/unleash/feature_toggle.rb index 2818f02b..ec064b34 100644 --- a/lib/unleash/feature_toggle.rb +++ b/lib/unleash/feature_toggle.rb @@ -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 diff --git a/spec/unleash/client_spec.rb b/spec/unleash/client_spec.rb index 9fb8108d..b95f9af6 100644 --- a/spec/unleash/client_spec.rb +++ b/spec/unleash/client_spec.rb @@ -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" + } + ] } ] }' @@ -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')