Sentinel Parameter Flow Control provides functionality of flow control by frequent (hot spot) parameters.
npx @tessl/cli install tessl/maven-sentinel-parameter-flow-control@1.8.0Sentinel Parameter Flow Control provides functionality of flow control by frequent ("hot spot") parameters. It enables QPS-based and thread-count-based flow control for specific parameter values, allowing fine-grained traffic shaping based on parameter frequency patterns.
pom.xml:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parameter-flow-control</artifactId>
<version>1.8.8</version>
</dependency>import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowChecker;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
import com.alibaba.csp.sentinel.SphU;import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.param.*;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import java.util.Collections;
// Create parameter flow rule
ParamFlowRule rule = new ParamFlowRule("resourceName")
.setParamIdx(0) // Monitor first parameter
.setCount(5) // Allow 5 QPS for each parameter value
.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Add exception for specific parameter value
ParamFlowItem item = new ParamFlowItem()
.setObject("VIP_USER")
.setClassType(String.class.getName())
.setCount(10); // VIP users get 10 QPS
rule.setParamFlowItemList(Collections.singletonList(item));
// Load rules
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
// Use in application code
public void processRequest(String userId) {
Entry entry = null;
try {
// Pass parameters to Sentinel
entry = SphU.entry("resourceName", EntryType.IN, 1, userId);
// Your business logic here
doBusinessLogic(userId);
} catch (ParamFlowException ex) {
// Handle parameter flow control triggered
System.out.println("Parameter flow control triggered for user: " + ex.getLimitParam());
} catch (BlockException ex) {
// Handle other flow control
} finally {
if (entry != null) {
entry.exit();
}
}
}Sentinel Parameter Flow Control is built around several key components:
ParamFlowRuleManager provides centralized rule configuration and dynamic updatesParamFlowChecker implements the core flow control logic with support for QPS and thread count modesParamFlowSlot integrates with Sentinel's slot chain for transparent parameter monitoringParameterMetric and ParameterMetricStorage track parameter usage statisticsCore functionality for defining, loading, and managing parameter flow control rules. This is the primary entry point for configuring parameter-based flow control.
public static void loadRules(List<ParamFlowRule> rules);
public static List<ParamFlowRule> getRulesOfResource(String resourceName);
public static List<ParamFlowRule> getRules();
public static boolean hasRules(String resourceName);Configuration objects that define how parameter flow control should behave. Rules specify which parameters to monitor, thresholds, and exception handling.
public class ParamFlowRule extends AbstractRule {
public ParamFlowRule();
public ParamFlowRule(String resourceName);
public ParamFlowRule setParamIdx(Integer paramIdx);
public ParamFlowRule setCount(double count);
public ParamFlowRule setGrade(int grade);
public ParamFlowRule setControlBehavior(int controlBehavior);
}Special parameter values that have different thresholds than the default rule. Useful for implementing VIP user handling or special case parameters.
public class ParamFlowItem {
public ParamFlowItem();
public ParamFlowItem(String object, Integer count, String classType);
public static <T> ParamFlowItem newItem(T object, Integer count);
public ParamFlowItem setObject(String object);
public ParamFlowItem setCount(Integer count);
public ParamFlowItem setClassType(String classType);
}Exception handling for when parameter flow control is triggered. Provides detailed information about which parameter caused the flow control.
public class ParamFlowException extends BlockException {
public ParamFlowException(String resourceName, String param);
public ParamFlowException(String resourceName, String param, ParamFlowRule rule);
public String getResourceName();
public String getLimitParam();
public ParamFlowRule getRule();
}Advanced functionality for distributed parameter flow control across multiple instances. Enables coordinated parameter limits in cluster environments.
public class ParamFlowClusterConfig {
public ParamFlowClusterConfig setFlowId(Long flowId);
public ParamFlowClusterConfig setThresholdType(int thresholdType);
public ParamFlowClusterConfig setFallbackToLocalWhenFail(boolean fallbackToLocalWhenFail);
}Utility functions for rule validation and management. These functions help with rule processing and validation logic.
public final class ParamFlowRuleUtil {
public static boolean isValidRule(ParamFlowRule rule);
public static Map<String, List<ParamFlowRule>> buildParamRuleMap(List<ParamFlowRule> list);
public static void fillExceptionFlowItems(ParamFlowRule rule);
}Core metrics tracking and storage functionality for parameter flow control statistics and performance monitoring.
public class ParameterMetric {
public void clear();
public CacheMap<Object, AtomicLong> getRuleTimeCounter(ParamFlowRule rule);
public long getThreadCount(int index, Object value);
public void addThreadCount(Object... args);
public void decreaseThreadCount(Object... args);
}
public final class ParameterMetricStorage {
public static ParameterMetric getParamMetric(ResourceWrapper resourceWrapper);
public static ParameterMetric getParamMetricForResource(String resourceName);
public static void clearParamMetricForResource(String resourceName);
}Command handlers for managing parameter flow rules through Sentinel's command interface, supporting dynamic rule retrieval and modification.
public class GetParamFlowRulesCommandHandler implements CommandHandler<String> {
public CommandResponse<String> handle(CommandRequest request);
}
public class ModifyParamFlowRulesCommandHandler implements CommandHandler<String> {
public CommandResponse<String> handle(CommandRequest request);
public static WritableDataSource<List<ParamFlowRule>> getWritableDataSource();
public static void setWritableDataSource(WritableDataSource<List<ParamFlowRule>> dataSource);
}public interface ParamFlowArgument {
Object paramFlowKey();
}
public enum RollingParamEvent {
REQUEST_PASSED,
REQUEST_BLOCKED
}
public final class ParamFlowChecker {
public static boolean passCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int count, Object... args);
}
public interface CacheMap<K, V> {
boolean containsKey(K key);
V get(K key);
V put(K key, V value);
V putIfAbsent(K key, V value);
void clear();
long size();
}
public class ParamMapBucket {
public static final int DEFAULT_MAX_CAPACITY = 200;
public ParamMapBucket();
public ParamMapBucket(int capacity);
public void reset();
public int get(RollingParamEvent event, Object value);
}