Skip to content

Commit

Permalink
Fix: gradual rollout
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Oct 31, 2023
1 parent b0213b2 commit 1e24d77
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn partial_rollout(group: &str, variable: Option<&String>, rollout: u32) ->
100 => true,
rollout => {
if let Ok(normalised) = normalised_hash(group, variable, 100) {
rollout > normalised
rollout >= normalised
} else {
false
}
Expand All @@ -119,7 +119,7 @@ pub fn partial_rollout(group: &str, variable: Option<&String>, rollout: u32) ->
/// required for extension strategies, but reusing this is probably a good idea
/// for consistency across implementations.
pub fn normalised_hash(group: &str, identifier: &str, modulus: u32) -> std::io::Result<u32> {
normalised_hash_internal(group, identifier, modulus, 0, 0)
normalised_hash_internal(group, identifier, modulus, 0)
}

const VARIANT_NORMALIZATION_SEED: u32 = 86028157;
Expand All @@ -133,23 +133,22 @@ pub fn normalised_variant_hash(
identifier: &str,
modulus: u32,
) -> std::io::Result<u32> {
normalised_hash_internal(group, identifier, modulus, VARIANT_NORMALIZATION_SEED, 1)
normalised_hash_internal(group, identifier, modulus, VARIANT_NORMALIZATION_SEED)
}

fn normalised_hash_internal(
group: &str,
identifier: &str,
modulus: u32,
seed: u32,
additional: u32,
) -> std::io::Result<u32> {
// See https://github.com/stusmall/murmur3/pull/16 : .chain may avoid
// copying in the general case, and may be faster (though perhaps
// benchmarking would be useful - small datasizes here could make the best
// path non-obvious) - but until murmur3 is fixed, we need to provide it
// with a single string no matter what.
let mut reader = Cursor::new(format!("{}:{}", &group, &identifier));
murmur3_32(&mut reader, seed).map(|hash_result| (hash_result % modulus) + additional)
murmur3_32(&mut reader, seed).map(|hash_result| hash_result % modulus + 1)
}

// Build a closure to handle session id rollouts, parameterised by groupId and a
Expand Down

0 comments on commit 1e24d77

Please sign in to comment.