带你读《2022技术人的百宝黑皮书》——一个搞定责任链的注解(7)https://developer.aliyun.com/article/1339671?groupCode=taobaotech
DuplicateThrottlerHandler
@Slf4j public class DuplicateThrottlerHandler implements MessageThrottlerHandler { 3 @Override public boolean throttle(MessageThrottlerToken messageThrottlerToken, MessageThrottlerHandler- Context context) { if (messageThrottlerToken.isThrottled()) { return false; } boolean throttleResult = context.throttle(messageThrottlerToken); messageThrottlerToken.markThrottled(); return throttleResult; } @Override public boolean anyThrottle(List<MessageThrottlerToken> messageThrottlerTokens, MessageThrot- tlerHandlerContext context) { if (CollectionUtils.isEmpty(messageThrottlerTokens)) { return false; } // 过滤掉已经被限流的消息 List<MessageThrottlerToken> needMessageThrottlerTokens = messageThrottlerTokens.stream() .filter(messageThrottlerToken -> !messageThrottlerToken.isThrottled()).collect(Collectors.toList ()); if (CollectionUtils.isEmpty(needMessageThrottlerTokens)) { return false; } boolean throttleResult = context.anyThrottle(needMessageThrottlerTokens); needMessageThrottlerTokens.forEach(messageThrottlerToken -> messageThrottlerToken.mark- Throttled()); return throttleResult; } @Override public boolean allThrottle(List<MessageThrottlerToken> messageThrottlerTokens, MessageThrot- tlerHandlerContext context) { if (CollectionUtils.isEmpty(messageThrottlerTokens)) {
36 |
|
return false; |
37 |
|
} |
38 |
|
|
|
|
// 过滤掉已经被限流的消息 |
39 |
|
List<MessageThrottlerToken> needMessageThrottlerTokens = messageThrottlerTokens.stream() |
40 |
|
.filter(messageThrottlerToken -> !messageThrottlerToken.isThrottled()).collect(Collectors.toList |
41 |
()); |
|
if (CollectionUtils.isEmpty(needMessageThrottlerTokens)) { return false; } boolean throttleResult = context.allThrottle(needMessageThrottlerTokens); needMessageThrottlerTokens.forEach(messageThrottlerToken -> messageThrottlerToken.mark- Throttled()); return throttleResult; } }
带你读《2022技术人的百宝黑皮书》——一个搞定责任链的注解(9)https://developer.aliyun.com/article/1339669?groupCode=taobaotech