Netty:使用多个事件循环的并发问题
Netty: Concurrency issues using multiple event loops
我有一个客户端连接到 n 个不同的服务器。所以,我正在创建 n 个不同的频道。
因为我的服务器超过5000台。我使用了10个事件循环,只有一个事件循环group.Also,每个通道都有单独的管道。
我已经知道如果我使用一个事件循环就不会有并发问题而且我在 10 个事件循环上没有看到任何并发问题 yet.My 问题是:
在下面的代码中,我会在行 healthTargets.storeHealthyTarget(InetAddress.getByName(remoteAddress));
上遇到并发问题吗?为什么?
我怀疑会有并发性 problem.Because,如果多个事件循环不访问它,那么使用多个事件循环有什么意义?
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws UnknownHostException {
if(channelHandlerContext.channel().remoteAddress() != null)
{
String remoteAddress = remoteAddress(channelHandlerContext.channel().remoteAddress().toString());
if (httpObject instanceof HttpResponse) {
HttpResponseStatus responseStatus = ((HttpResponse)httpObject).getStatus();
if (responseStatus.code() == HttpResponseStatus.OK.code())
{
healthTargets.storeHealthyTarget(InetAddress.getByName(remoteAddress));
}
}
}
}
这里healthTargets.storeHealthyTarget
只是用HashSet存储ip,healthTargets是单例class.
如果您在不同的 EventLoop 之间共享相同的 Set
实例(您似乎这样做),您将 运行 陷入问题。这是因为不同的 Channels 可能 运行 在不同的 EventLoops 和 Threads 上。这会导致您需要使 healthTargets
的访问成为线程安全的。
这与任何其他多线程程序没有什么不同。
我有一个客户端连接到 n 个不同的服务器。所以,我正在创建 n 个不同的频道。
因为我的服务器超过5000台。我使用了10个事件循环,只有一个事件循环group.Also,每个通道都有单独的管道。
我已经知道如果我使用一个事件循环就不会有并发问题而且我在 10 个事件循环上没有看到任何并发问题 yet.My 问题是:
在下面的代码中,我会在行 healthTargets.storeHealthyTarget(InetAddress.getByName(remoteAddress));
上遇到并发问题吗?为什么?
我怀疑会有并发性 problem.Because,如果多个事件循环不访问它,那么使用多个事件循环有什么意义?
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws UnknownHostException {
if(channelHandlerContext.channel().remoteAddress() != null)
{
String remoteAddress = remoteAddress(channelHandlerContext.channel().remoteAddress().toString());
if (httpObject instanceof HttpResponse) {
HttpResponseStatus responseStatus = ((HttpResponse)httpObject).getStatus();
if (responseStatus.code() == HttpResponseStatus.OK.code())
{
healthTargets.storeHealthyTarget(InetAddress.getByName(remoteAddress));
}
}
}
}
这里healthTargets.storeHealthyTarget
只是用HashSet存储ip,healthTargets是单例class.
如果您在不同的 EventLoop 之间共享相同的 Set
实例(您似乎这样做),您将 运行 陷入问题。这是因为不同的 Channels 可能 运行 在不同的 EventLoops 和 Threads 上。这会导致您需要使 healthTargets
的访问成为线程安全的。
这与任何其他多线程程序没有什么不同。