Skip to content

Commit

Permalink
Implement stickiness on variants #72
Browse files Browse the repository at this point in the history
  • Loading branch information
daveleek committed May 9, 2021
1 parent 706eaba commit fad242a
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Unleash/Variants/VariantUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static Variant SelectVariant(FeatureToggle featureToggle, UnleashContext
return variantOverride.ToVariant();
}

var target = StrategyUtils.GetNormalizedNumber(GetIdentifier(context), featureToggle.Name, totalWeight);
var stickiness = variantDefinitions[0].Stickiness ?? "default";
var target = StrategyUtils.GetNormalizedNumber(GetIdentifier(context, stickiness), featureToggle.Name, totalWeight);

var counter = 0;
foreach (var variantDefinition in variantDefinitions)
Expand Down Expand Up @@ -70,12 +71,23 @@ private static Func<VariantOverride, bool> OverrideMatchesContext(UnleashContext
};
}

private static string GetIdentifier(UnleashContext context)
private static string GetIdentifier(UnleashContext context, string stickiness)
{
if (stickiness != "default")
{
var stickinessValue = context.GetByName(stickiness);
return stickinessValue ?? GetRandomValue();
}

return context.UserId
?? context.SessionId
?? context.RemoteAddress
?? new Random().NextDouble().ToString();
?? GetRandomValue();
}

private static string GetRandomValue()
{
return new Random().NextDouble().ToString();
}
}
}

0 comments on commit fad242a

Please sign in to comment.