ojas
ojas

Reputation: 2210

Netty: Concurrency issues using multiple event loops

I have one client connecting to n different servers. So, i am creating n different channels.
Because my servers are more than 5000. I am using 10 event loops with only one event loop group.Also, there is separate pipeline for each channel.
I already know there would be no concurrency problem if i will use one event loop and i have not seen any concurrency problem on 10 event loops yet.My question is:
Will i will be having concurrency problem on line healthTargets.storeHealthyTarget(InetAddress.getByName(remoteAddress)); in the below piece of code and why?
I suspect there will be a concurrency problem.Because, if multiple event loops are not accessing this, then what is the point being using multiple event loops?

 @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));
                }
            }
        }
    }

Here healthTargets.storeHealthyTarget is just using HashSet to store the ip and healthTargets is a singleton class.

Upvotes: 0

Views: 506

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

If you share the same instance of the Set (which you seems to do) between different EventLoops you will run into issues. This is because the different Channels may run on different EventLoops and so Threads. This has the effect that you will need to make the access of healthTargets thread-safe.

This is not different to any other multi-threaded program.

Upvotes: 1

Related Questions