funky-eyes commented on code in PR #6756: URL: https://github.com/apache/incubator-seata/pull/6756#discussion_r1896744584
########## server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java: ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.server.ratelimit; + +import org.apache.seata.common.XID; +import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.util.NumberUtils; +import org.apache.seata.config.CachedConfigurationChangeListener; +import org.apache.seata.config.Configuration; +import org.apache.seata.config.ConfigurationChangeEvent; +import org.apache.seata.common.ConfigurationKeys; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.core.exception.TransactionExceptionCode; +import org.apache.seata.core.protocol.AbstractMessage; +import org.apache.seata.core.protocol.AbstractResultMessage; +import org.apache.seata.core.protocol.ResultCode; +import org.apache.seata.core.protocol.transaction.GlobalBeginRequest; +import org.apache.seata.core.protocol.transaction.GlobalBeginResponse; +import org.apache.seata.core.rpc.RpcContext; +import org.apache.seata.server.metrics.MetricsPublisher; + +/** + * RateLimiterHandler + */ +public class RateLimiterHandler implements CachedConfigurationChangeListener { + /** + * The instance of RateLimiterHandler + */ + private static volatile RateLimiterHandler instance; + + /** + * The instance of RateLimiter + */ + private final RateLimiter rateLimiter; + + /** + * The config of RateLimiterHandler + */ + private final RateLimiterHandlerConfig config; + + public RateLimiterHandler(RateLimiter rateLimiter) { + this.rateLimiter = rateLimiter; + this.config = new RateLimiterHandlerConfig(); + } + + private RateLimiterHandler() { + rateLimiter = EnhancedServiceLoader.load(RateLimiter.class); + config = rateLimiter.obtainConfig(); + + Configuration config = ConfigurationFactory.getInstance(); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this); + } + + public static RateLimiterHandler getInstance() { + if (instance == null) { + synchronized (RateLimiterHandler.class) { + if (instance == null) { + instance = new RateLimiterHandler(); + } + } + } + return instance; + } + + @Override + public void onChangeEvent(ConfigurationChangeEvent event) { + String dataId = event.getDataId(); + String newValue = event.getNewValue(); + if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) { + config.setEnable(Boolean.parseBoolean(newValue)); + } + if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) { + config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond())); + } + if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) { + config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum())); + } + if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) { + config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum())); + } + rateLimiter.reInit(config); + } + + public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) { + if (!rateLimiter.isEnable()) { + return null; + } + + if (request instanceof GlobalBeginRequest) { + if (!rateLimiter.canPass()) { + GlobalBeginResponse response = new GlobalBeginResponse(); + response.setTransactionExceptionCode(TransactionExceptionCode.BeginFailed); + response.setResultCode(ResultCode.Failed); + RateLimitInfo rateLimitInfo = RateLimitInfo.generateRateLimitInfo(rpcContext.getApplicationId(), + RateLimitInfo.GLOBAL_BEGIN_FAILED, rpcContext.getClientId(), XID.getIpAddressAndPort()); + MetricsPublisher.postRateLimitEvent(rateLimitInfo); + response.setMsg(String.format("TransactionException[rate limit exception, rate limit info: %s]", rateLimitInfo)); Review Comment: 限流器应该只是限流作用,为什么要跟业务逻辑耦合在一起? The role of limited viewership should only be limited viewership, why should it be coupled with business logic? ########## server/src/main/resources/file.conf: ########## Review Comment: Delete it ########## server/src/main/java/org/apache/seata/server/ratelimit/RateLimiterHandler.java: ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.server.ratelimit; + +import org.apache.seata.common.XID; +import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.util.NumberUtils; +import org.apache.seata.config.CachedConfigurationChangeListener; +import org.apache.seata.config.Configuration; +import org.apache.seata.config.ConfigurationChangeEvent; +import org.apache.seata.common.ConfigurationKeys; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.core.exception.TransactionExceptionCode; +import org.apache.seata.core.protocol.AbstractMessage; +import org.apache.seata.core.protocol.AbstractResultMessage; +import org.apache.seata.core.protocol.ResultCode; +import org.apache.seata.core.protocol.transaction.GlobalBeginRequest; +import org.apache.seata.core.protocol.transaction.GlobalBeginResponse; +import org.apache.seata.core.rpc.RpcContext; +import org.apache.seata.server.metrics.MetricsPublisher; + +/** + * RateLimiterHandler + */ +public class RateLimiterHandler implements CachedConfigurationChangeListener { + /** + * The instance of RateLimiterHandler + */ + private static volatile RateLimiterHandler instance; + + /** + * The instance of RateLimiter + */ + private final RateLimiter rateLimiter; + + /** + * The config of RateLimiterHandler + */ + private final RateLimiterHandlerConfig config; + + public RateLimiterHandler(RateLimiter rateLimiter) { + this.rateLimiter = rateLimiter; + this.config = new RateLimiterHandlerConfig(); + } + + private RateLimiterHandler() { + rateLimiter = EnhancedServiceLoader.load(RateLimiter.class); + config = rateLimiter.obtainConfig(); + + Configuration config = ConfigurationFactory.getInstance(); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_ENABLE, this); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND, this); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM, this); + config.addConfigListener(ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM, this); + } + + public static RateLimiterHandler getInstance() { + if (instance == null) { + synchronized (RateLimiterHandler.class) { + if (instance == null) { + instance = new RateLimiterHandler(); + } + } + } + return instance; + } + + @Override + public void onChangeEvent(ConfigurationChangeEvent event) { + String dataId = event.getDataId(); + String newValue = event.getNewValue(); + if (ConfigurationKeys.RATE_LIMIT_ENABLE.equals(dataId)) { + config.setEnable(Boolean.parseBoolean(newValue)); + } + if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_NUM_PER_SECOND.equals(dataId)) { + config.setBucketTokenNumPerSecond(NumberUtils.toInt(newValue, config.getBucketTokenNumPerSecond())); + } + if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_MAX_NUM.equals(dataId)) { + config.setBucketTokenMaxNum(NumberUtils.toInt(newValue, config.getBucketTokenMaxNum())); + } + if (ConfigurationKeys.RATE_LIMIT_BUCKET_TOKEN_INITIAL_NUM.equals(dataId)) { + config.setBucketTokenInitialNum(NumberUtils.toInt(newValue, config.getBucketTokenInitialNum())); + } + rateLimiter.reInit(config); + } + + public AbstractResultMessage handle(AbstractMessage request, RpcContext rpcContext) { + if (!rateLimiter.isEnable()) { + return null; + } + + if (request instanceof GlobalBeginRequest) { Review Comment: 为什么不使用MessageType来判断,未来是否限流器可以作用于更多的request?只要对外提供一个配置项,允许用户配置一个需要限流request的集合,这里只要判断MessageType是否包含在其中即可 Why not use MessageType to determine whether the limit viewership of requests can be used in the future? Just provide a configuration item to allow users to configure a collection that needs to limit viewership of requests. Here, just determine whether MessageType is included in it -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
