带你读《2022技术人的百宝黑皮书》——一个搞定责任链的注解(8)https://developer.aliyun.com/article/1339670?groupCode=taobaotech
AcquireThrottlerHandler
/** * 请求令牌处理类 */ public class AcquireThrottlerHandler implements MessageThrottlerHandler { private static final Logger apiThrottlerLog = LoggerFactory.getLogger("api.throttler.log"); @Autowired private ThrottlerProxy throttlerProxy; @Autowired private ThrottlerModeConfiguration throttlerModeConfiguration; private boolean throttle(AcquireToken acquireToken) { // 获取限流模式 ThrottlerMode throttlerMode = throttlerModeConfiguration.getThrottlerMode(acquireToken.getAp- pKey(), acquireToken.getThrottleTag()); // 执行限流 return !throttlerProxy.tryAcquireWithAppKey(throttlerMode, acquireToken.getAppKey(), acquireTo- ken.getPermits()); } @Override public boolean throttle(MessageThrottlerToken messageThrottlerToken, MessageThrottlerHandler- Context context) { boolean throttled = throttle(new AcquireToken(messageThrottlerToken.getThrottleTag(), message- ThrottlerToken.getAppKey(), messageThrottlerToken.getPermits())); // 限流日志埋点 if (SendSwitch.THROTTLER_ONLY_WATCH || throttled) { log(messageThrottlerToken.getAppKey(), messageThrottlerToken.getPermits(), messageThrot- tlerToken.getThrottleTag(), throttled); } return throttled; } @Override public boolean anyThrottle(List<MessageThrottlerToken> messageThrottlerTokens, MessageThrot- tlerHandlerContext context) { return throttle(messageThrottlerTokens, acquireTokens -> acquireTokens.stream().anyMatch (this::throttle) ); } @Override public boolean allThrottle(List<MessageThrottlerToken> messageThrottlerTokens, MessageThrot- tlerHandlerContext messageThrottlerHandlerContext) { return throttle(messageThrottlerTokens, acquireTokens -> acquireTokens.stream().allMatch (this::throttle) ); } private static boolean throttle(List<MessageThrottlerToken> messageThrottlerTokens, Func- tion<List<AcquireToken>, Boolean> function) { if (CollectionUtils.isEmpty(messageThrottlerTokens)) { return false; } List<AcquireToken> acquireTokens = messageThrottlerTokens.stream() .collect(Collectors.groupingBy(messageThrottlerToken -> messageThrottlerToken.getAppKey ())) .entrySet() .stream() .map(messageEntry -> { String appKey = messageEntry.getKey(); int permits = messageEntry.getValue().stream() .map(messageThrottlerToken -> messageThrottlerToken.getPermits()) .reduce(Integer::sum).orElse(1); String throttlerTag = messageEntry.getValue().get(0).getThrottleTag(); return new AcquireToken(throttlerTag, appKey, permits); }).collect(Collectors.toList()); boolean throttled = function.apply(acquireTokens); // 限流日志埋点 if (SendSwitch.THROTTLER_ONLY_WATCH || throttled) { messageThrottlerTokens.forEach(messageThrottlerToken -> { log(messageThrottlerToken.getAppKey(), messageThrottlerToken.getPermits(), messageTh- rottlerToken.getThrottleTag(), throttled); }); } return throttled; } private static void log(String appKey, int permits, String throttlerTag, boolean throtted) { List<String> metrics = new ArrayList<>(); metrics.add(appKey); metrics.add(String.valueOf(permits)); metrics.add(throttlerTag); metrics.add(String.valueOf(throtted)); String logContent = StringUtils.join(metrics, "|"); apiThrottlerLog.info(logContent);
82 |
|
} |
83 |
|
|
84 |
|
@Data |
85 |
|
@AllArgsConstructor |
86 |
|
private static class AcquireToken { |
87 |
|
private final String throttleTag; |
88 |
|
private final String appKey; |
89 |
|
private final int permits; |
90 |
|
} |
91 |
} |
|
带你读《2022技术人的百宝黑皮书》——一个搞定责任链的注解(10)https://developer.aliyun.com/article/1339668?groupCode=taobaotech