mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-16 18:35:05 +08:00
【fix】 使用线程池处理流量信息及业务
This commit is contained in:
@ -6,6 +6,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.advanced.flow.ChannelFl
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.flow.HandleChannelFlowAdvanced;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* 通道流量适配器
|
||||
@ -15,6 +16,13 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class ChannelFlowAdapter {
|
||||
|
||||
|
||||
ThreadPoolExecutor CHANNEL_FLOW_ADAPTER_EXECUTOR =
|
||||
new ThreadPoolExecutor(20, 200, 3L, TimeUnit.MINUTES,
|
||||
new LinkedBlockingDeque<>(500));
|
||||
|
||||
|
||||
|
||||
protected final List<HandleChannelFlowAdvanced> handleChannelFlowAdvancedList;
|
||||
|
||||
public ChannelFlowAdapter(List<HandleChannelFlowAdvanced> handleChannelFlowAdvancedList) {
|
||||
@ -38,4 +46,14 @@ public class ChannelFlowAdapter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步处理当前数据
|
||||
*
|
||||
* @param channelFlow 通道数据
|
||||
*/
|
||||
public void asyncHandler(Channel channel, ChannelFlow channelFlow) {
|
||||
CHANNEL_FLOW_ADAPTER_EXECUTOR.submit(() -> handler(channel, channelFlow));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.common.pool;
|
||||
|
||||
|
||||
/**
|
||||
* 通道连接池抽象类
|
||||
*/
|
||||
public abstract class AbstractNettyChannelPool implements NettyChannelPool {
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.common.pool;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* 默认netty 连接池
|
||||
*/
|
||||
public class DefaultNettyChannelPool extends AbstractNettyChannelPool implements NettyChannelPool {
|
||||
|
||||
/**
|
||||
* 连接池大小
|
||||
*/
|
||||
private final int poolSize;
|
||||
|
||||
|
||||
// 绑定访客的通道
|
||||
private final ConcurrentHashMap<String, Channel> visitorChannelMap = new ConcurrentHashMap<>();
|
||||
|
||||
// 所有的通道
|
||||
private final List<Channel> allChannelList = new ArrayList<>();
|
||||
|
||||
// 闲置的通道
|
||||
private final List<Channel> idleChannelList = new CopyOnWriteArrayList<Channel>();
|
||||
|
||||
public DefaultNettyChannelPool(int poolSize) {
|
||||
this.poolSize = poolSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据访客ID获取可以使用的通道
|
||||
*
|
||||
* @param visitorId 访客ID
|
||||
* @return Channel 如果无法获取到闲置通道返回null
|
||||
*/
|
||||
@Override
|
||||
public Channel availableChannel(String visitorId) {
|
||||
synchronized (idleChannelList) {
|
||||
if (idleChannelList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
// 获取通道
|
||||
Channel visitorChannel = null;
|
||||
for (Channel idleChannel : idleChannelList) {
|
||||
if (idleChannel.isActive()) {
|
||||
visitorChannel = idleChannel;
|
||||
}
|
||||
idleChannelList.remove(idleChannel);
|
||||
}
|
||||
if (visitorChannel == null) {
|
||||
return null;
|
||||
}
|
||||
// 绑定 通道
|
||||
ChannelAttributeKeyUtils.buildVisitorId(visitorChannel, visitorId);
|
||||
visitorChannelMap.put(visitorId, visitorChannel);
|
||||
return visitorChannel;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.common.pool;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
/**
|
||||
* 通道连接池
|
||||
*/
|
||||
public interface NettyChannelPool {
|
||||
|
||||
/**
|
||||
* 根据访客ID获取可以使用的通道
|
||||
* @param visitorId 访客ID
|
||||
* @return Channel
|
||||
*/
|
||||
Channel availableChannel(String visitorId);
|
||||
|
||||
}
|
Reference in New Issue
Block a user