lokidundun commented on code in PR #7783:
URL: https://github.com/apache/incubator-seata/pull/7783#discussion_r2532752892
##########
core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.java:
##########
@@ -120,17 +127,18 @@ public abstract class AbstractNettyRemotingClient extends
AbstractNettyRemoting
@Override
public void init() {
- timerExecutor.scheduleAtFixedRate(
- () -> {
- try {
-
clientChannelManager.reconnect(getTransactionServiceGroup());
- } catch (Exception ex) {
- LOGGER.warn("reconnect server failed. {}",
ex.getMessage());
- }
- },
- SCHEDULE_DELAY_MILLS,
- SCHEDULE_INTERVAL_MILLS,
- TimeUnit.MILLISECONDS);
+ if (timerStarted.compareAndSet(false, true)) {
+ mergeLock.lock();
+ try {
+ this.reconnectTimer = new ScheduledThreadPoolExecutor(
+ 1, new NamedThreadFactory("Reconnect-Timer-" +
transactionRole.name(), 1));
+ this.reconnectTimer.scheduleAtFixedRate(
+ this.reconnectTask, SCHEDULE_DELAY_MILLS,
SCHEDULE_INTERVAL_MILLS, TimeUnit.MILLISECONDS);
Review Comment:
Thanks for your detailed feedback! Here are the optimizations I've received
and made in response to your comments:
I've initialized `reconnectTask `in the constructor in advance and adjusted
the logic to reuse the existing `timerExecutor`
```
@Override
public void init() {
mergeLock.lock();
try {
if (timerStarted.compareAndSet(false, true)) {
timerExecutor.scheduleAtFixedRate(
reconnectTask, SCHEDULE_DELAY_MILLS,
SCHEDULE_INTERVAL_MILLS, TimeUnit.MILLISECONDS);
LOGGER.info("Reconnect timer started (role: {})",
transactionRole.name());
}
if (this.isEnableClientBatchSendRequest()) {
mergeSendExecutorService = new ThreadPoolExecutor(
MAX_MERGE_SEND_THREAD,
MAX_MERGE_SEND_THREAD,
KEEP_ALIVE_TIME,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new NamedThreadFactory(getThreadPrefix(),
MAX_MERGE_SEND_THREAD));
mergeSendExecutorService.submit(new MergedSendRunnable());
}
super.init();
clientBootstrap.start();
} finally {
mergeLock.unlock();
}
}
public AbstractNettyRemotingClient(
NettyClientConfig nettyClientConfig,
ThreadPoolExecutor messageExecutor,
NettyPoolKey.TransactionRole transactionRole) {
super(messageExecutor);
this.transactionRole = transactionRole;
clientBootstrap = new NettyClientBootstrap(nettyClientConfig,
transactionRole);
clientBootstrap.setChannelHandlers(new ClientHandler(), new
ChannelEventHandler(this));
clientChannelManager = new NettyClientChannelManager(
new NettyPoolableFactory(this, clientBootstrap),
getPoolKeyFunction(), nettyClientConfig);
this.reconnectTask = () -> {
if (!timerStarted.get()) {
return;
}
try {
String serviceGroup = getTransactionServiceGroup();
if (StringUtils.isNotBlank(serviceGroup)) {
clientChannelManager.reconnect(serviceGroup);
}
} catch (Throwable t) {
LOGGER.error("Reconnect task failed for role: {}",
transactionRole.name(), t);
}
};
}
```
Am I on the right way.I’d really appreciate any feedback and let me know if
you have any further questions!
--
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]