Class ConsistentPercentageStrategy

java.lang.Object
cloud.opencode.base.feature.strategy.ConsistentPercentageStrategy
All Implemented Interfaces:
EnableStrategy

public class ConsistentPercentageStrategy extends Object implements EnableStrategy
Consistent Percentage Strategy 一致性哈希百分比策略

Strategy using consistent hashing to prevent users from bypassing by ID manipulation.

使用一致性哈希防止用户通过修改ID绕过的策略。

Features | 主要功能:

  • SHA-256 consistent hashing - SHA-256一致性哈希
  • Salt for security - 加盐安全
  • Feature-specific distribution - 按功能分布
  • Strict user requirement - 严格要求用户ID

Comparison with PercentageStrategy | 与PercentageStrategy对比:

AspectPercentageStrategyConsistentPercentageStrategy
Hash algorithmhashCode()SHA-256
No userIdRandomReturns false
Feature-specificNoYes (includes feature key)
Salt supportNoYes

When to use | 何时使用:

  • Use when you need cryptographic security against user ID manipulation
  • 当需要防止用户ID操纵的加密安全时使用
  • Use when different features should have different user distributions
  • 当不同功能需要不同的用户分布时使用
  • Use when anonymous users should NOT be allowed
  • 当不允许匿名用户时使用
  • Use PercentageStrategy for simpler rollouts with anonymous support
  • 对于支持匿名用户的简单灰度发布,使用PercentageStrategy

Usage Examples | 使用示例:

// 10% with salt
Feature feature = Feature.builder("new-feature")
    .strategy(new ConsistentPercentageStrategy(10, "secret-salt"))
    .build();

// Same user always gets same result
boolean result1 = feature.isEnabled(FeatureContext.ofUser("user1"));
boolean result2 = feature.isEnabled(FeatureContext.ofUser("user1"));
assert result1 == result2;

// Anonymous users are NOT allowed
boolean anon = feature.isEnabled(FeatureContext.empty()); // Always false

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-feature V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ConsistentPercentageStrategy

      public ConsistentPercentageStrategy(int percentage, String salt)
      Create consistent percentage strategy 创建一致性百分比策略
      Parameters:
      percentage - the percentage (0-100) | 百分比 (0-100)
      salt - the salt for hashing | 用于哈希的盐
    • ConsistentPercentageStrategy

      public ConsistentPercentageStrategy(int percentage)
      Create with default empty salt 使用默认空盐创建
      Parameters:
      percentage - the percentage | 百分比
  • Method Details

    • isEnabled

      public boolean isEnabled(Feature feature, FeatureContext context)
      Check if enabled using consistent hash 使用一致性哈希检查是否启用
      Specified by:
      isEnabled in interface EnableStrategy
      Parameters:
      feature - the feature | 功能
      context - the context | 上下文
      Returns:
      true if enabled | 如果启用返回true
    • getPercentage

      public int getPercentage()
      Get the percentage 获取百分比
      Returns:
      percentage | 百分比
    • hasSalt

      public boolean hasSalt()
      Check if salt is configured 检查是否配置了盐
      Returns:
      true if salt is set | 如果设置了盐返回true
    • toString

      public String toString()
      Overrides:
      toString in class Object