R
rajatag
Hi Everyone,
I have a non blocking server which accepts new connections as per the
acceptConnections() method below.
Connections are closed by calling channel.socket().close();
The server works fine, however after sometime we notice that the CPU
usage becomes 100% even though the server continues to function
normally.
I assume that this is due to the channels not being closed after being
registered for an OP_WRITE. However, I am not sure why that would
occur.
If someone can help it would be great!
Thanks!
Rajat
=======================
public void acceptConnections() throws IOException,
InterruptedException {
SelectionKey acceptKey =
this.selectableChannel.register(this.selector,SelectionKey.OP_ACCEPT);
while ((this.keysAdded = acceptKey.selector().select()) > 0) {
Set readyKeys = this.selector.selectedKeys();
Iterator i = readyKeys.iterator();
while (i.hasNext()) {
try {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isAcceptable()) {
ServerSocketChannel nextReady = (ServerSocketChannel) key
.channel();
SocketChannel channel = nextReady.accept();
if (db.checkIPBanning(channel.socket().getInetAddress()
.toString())) {
channel.configureBlocking(false);
SelectionKey readKey = channel.register(
this.selector, SelectionKey.OP_READ);
readKey.attach(new ClientConnection(this, channel,
"" + channel.socket().getPort()
+ channel.socket().getInetAddress()
+ ran.nextInt()));
} else {
channel.socket().close();
System.out.println("Banned IP ... "
+ channel.socket().getInetAddress()
.toString());
}
} else if (key.isReadable()) {
try {
this.readMessage((ClientConnection) key
.attachment());
} catch (Exception e) {
key.channel().close();
}
}
} catch (Exception e) {
}
}
}
}
I have a non blocking server which accepts new connections as per the
acceptConnections() method below.
Connections are closed by calling channel.socket().close();
The server works fine, however after sometime we notice that the CPU
usage becomes 100% even though the server continues to function
normally.
I assume that this is due to the channels not being closed after being
registered for an OP_WRITE. However, I am not sure why that would
occur.
If someone can help it would be great!
Thanks!
Rajat
=======================
public void acceptConnections() throws IOException,
InterruptedException {
SelectionKey acceptKey =
this.selectableChannel.register(this.selector,SelectionKey.OP_ACCEPT);
while ((this.keysAdded = acceptKey.selector().select()) > 0) {
Set readyKeys = this.selector.selectedKeys();
Iterator i = readyKeys.iterator();
while (i.hasNext()) {
try {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isAcceptable()) {
ServerSocketChannel nextReady = (ServerSocketChannel) key
.channel();
SocketChannel channel = nextReady.accept();
if (db.checkIPBanning(channel.socket().getInetAddress()
.toString())) {
channel.configureBlocking(false);
SelectionKey readKey = channel.register(
this.selector, SelectionKey.OP_READ);
readKey.attach(new ClientConnection(this, channel,
"" + channel.socket().getPort()
+ channel.socket().getInetAddress()
+ ran.nextInt()));
} else {
channel.socket().close();
System.out.println("Banned IP ... "
+ channel.socket().getInetAddress()
.toString());
}
} else if (key.isReadable()) {
try {
this.readMessage((ClientConnection) key
.attachment());
} catch (Exception e) {
key.channel().close();
}
}
} catch (Exception e) {
}
}
}
}