Papaya
Papaya

Reputation: 158

How to set maximum concurrent connections with netty

I'm beginning with Netty Library and want to set to 4 the number of clients which can connect on my server, how can I do ?

Thanks

Upvotes: 2

Views: 12740

Answers (2)

Ingrim4
Ingrim4

Reputation: 331

The simplest way would be to write your own handler which counts the connected clients in a static integer.

Something like this:

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

@Sharable
public class ConnectionCounter extends ChannelInboundHandlerAdapter {

    private static int connections = 0;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        if(connections < 4) {
            connections++;
            super.channelActive(ctx);
        } else
            ctx.close();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        connections--;
    }
}

EDIT

You should keep in mind that you have to use one thread otherwise it could cause some problems (race condition). If have to use multiple thread change the int to an AtomicInteger or use the synchronized keyword on the static int.

Upvotes: 1

Dmitriy Dumanskiy
Dmitriy Dumanskiy

Reputation: 12877

You can't configure netty to limit the number of incoming connections. But you can close connections that are above your limit right after they are opened. There are few ways to achieve that.

First one would be as in the example above. You need to add at the beginning of your pipeline the ConnectionCounter handler. However, you need to use AtomicInteger instead of int connections and increment the counter before check (to avoid race condition issues):

@Sharable
public class ConnectionCounter extends ChannelInboundHandlerAdapter {

private final AtomicInteger connections = new AtomicInteger();

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    int val = connections.incrementAndGet();
    if (val <= 4) {
        super.channelActive(ctx);
    } else {
        ctx.close();
    }
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    super.channelInactive(ctx);
    connections.decrementAndGet();
}
}

P. S. Have in mind this handler is Sharable and you need to create only 1 instance of it. Otherwise, you need to make connections field static.

Another option would be to use single-threaded EventLoop. As you expect only 4 connections - they are could be easily handled with 1 EventLoop:

new ServerBootstrap().group(bossGroup, new EpollEventLoopGroup(1));

Thus you have only 1 worker thread you may use above ConnectionCounter handler code but without AtomicInteger.

And the last option would be - DefaultChannelGroup. However, inside it uses ConcurrentMap<ChannelId, Channel>. So you can implement it in the same way as ConnectionCounter handler.

Upvotes: 8

Related Questions