Skip to content

Commit

Permalink
🐳 feat(external): add SocketIdleExcludeHandler, If the server is not …
Browse files Browse the repository at this point in the history
…configured with heartbeat processing, exclude heartbeat data
  • Loading branch information
iohao committed Nov 9, 2024
1 parent 7fbebd6 commit 9b047ab
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import lombok.Getter;
import lombok.experimental.FieldDefaults;

import java.util.Locale;

/**
* action 错误码
* <pre>
Expand Down Expand Up @@ -74,4 +76,8 @@ public enum ActionErrorEnum implements MsgExceptionInfo {
this.code = code;
this.msg = msg;
}

public String getMsg() {
return Locale.getDefault() == Locale.CHINA ? msg : name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
* </pre>
* 轻量可控的延时任务 - 特点
* <pre>
* 1. 任务到达指定时间后会执行
* 2. 任务可取消
* 3. 任务可增加、减少延时的时间
* 1. 单一职责原则
* 2. 任务到达指定时间后会执行
* 3. 任务可取消
* 4. 任务可被覆盖
* 5. 可设置任务监听回调
* 5. 任务可增加、减少延时的时间
* 6. 可设置任务监听回调
* 7. 内部使用 Netty HashedWheelTimer,轻松支持百万任务。
* </pre>
* for example
* <pre>{@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,18 @@ final class CmdCacheRegion {
*/
CmdActionCache getCmdCache(int cmdMerge) {

CmdActionCache cmdActionCache = this.cmdCacheMap.get(cmdMerge);

var cmdActionCache = this.cmdCacheMap.get(cmdMerge);
if (Objects.nonNull(cmdActionCache)) {
return cmdActionCache;
}

// 如果开启了范围缓存,即使没有显示的配置,也会生成缓存对象
if (range) {
cmdActionCache = addCmdCache(cmdMerge, this.cmdCacheOption);
}

return cmdActionCache;
return range ? addCmdCache(cmdMerge, this.cmdCacheOption) : null;
}

CmdActionCache addCmdCache(int cmdMerge, CmdCacheOption cmdCacheOption) {

CmdActionCache cmdActionCache = this.cmdCacheMap.get(cmdMerge);
var cmdActionCache = this.cmdCacheMap.get(cmdMerge);
if (Objects.nonNull(cmdActionCache)) {
return cmdActionCache;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.external.core.netty.handler;

import com.iohao.game.action.skeleton.protocol.BarMessage;
import com.iohao.game.external.core.message.ExternalMessageCmdCode;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
* Exclude heartbeat message
*
* @author 渔民小镇
* @date 2024-11-09
* @since 21.20
*/
@ChannelHandler.Sharable
public final class SocketIdleExcludeHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
BarMessage message = (BarMessage) msg;

int cmdCode = message.getHeadMetadata().getCmdCode();
if (cmdCode == ExternalMessageCmdCode.idle) {
return;
}

// 交给下一个业务处理 (handler) , 下一个业务指的是你编排 handler 时的顺序
ctx.fireChannelRead(msg);
}

private SocketIdleExcludeHandler() {
}

public static SocketIdleExcludeHandler me() {
return Holder.ME;
}

/** 通过 JVM 的类加载机制, 保证只加载一次 (singleton) */
private static class Holder {
static final SocketIdleExcludeHandler ME = new SocketIdleExcludeHandler();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ protected void initChannel(SocketChannel ch) {
public void pipelineIdle(PipelineContext context) {
IdleProcessSetting idleProcessSetting = this.setting.getIdleProcessSetting();
if (Objects.isNull(idleProcessSetting)) {
// 如果服务器没有配置心跳相关的内容,则排除心跳数据(不做任何处理)
// If the server is not configured with heartbeat processing, exclude heartbeat data
context.addLast("SocketIdleExcludeHandler", SocketIdleExcludeHandler.me());
return;
}

Expand All @@ -71,9 +74,8 @@ public void pipelineIdle(PipelineContext context) {
idleProcessSetting.getTimeUnit())
);

SocketIdleHandler socketIdleHandler = setting.option(SettingOption.socketIdleHandler);

// 心跳响应、心跳钩子 Handler
SocketIdleHandler socketIdleHandler = setting.option(SettingOption.socketIdleHandler);
context.addLast("idleHandler", socketIdleHandler);
}

Expand Down

0 comments on commit 9b047ab

Please sign in to comment.