Preliminary fix for the file descriptor starvation problem
This commit is contained in:
parent
20798ee429
commit
f4a71f92ce
@ -76,6 +76,7 @@ class NioWorker implements Runnable {
|
|||||||
private final Object startStopLock = new Object();
|
private final Object startStopLock = new Object();
|
||||||
private final Queue<Runnable> registerTaskQueue = new LinkedTransferQueue<Runnable>();
|
private final Queue<Runnable> registerTaskQueue = new LinkedTransferQueue<Runnable>();
|
||||||
private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>();
|
private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>();
|
||||||
|
private volatile int cancelledKeys;
|
||||||
|
|
||||||
NioWorker(int bossId, int id, Executor executor) {
|
NioWorker(int bossId, int id, Executor executor) {
|
||||||
this.bossId = bossId;
|
this.bossId = bossId;
|
||||||
@ -189,6 +190,7 @@ class NioWorker implements Runnable {
|
|||||||
selector.wakeup();
|
selector.wakeup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cancelledKeys = 0;
|
||||||
processRegisterTaskQueue();
|
processRegisterTaskQueue();
|
||||||
processWriteTaskQueue();
|
processWriteTaskQueue();
|
||||||
processSelectedKeys(selector.selectedKeys());
|
processSelectedKeys(selector.selectedKeys());
|
||||||
@ -570,6 +572,15 @@ class NioWorker implements Runnable {
|
|||||||
SelectionKey key = channel.socket.keyFor(selector);
|
SelectionKey key = channel.socket.keyFor(selector);
|
||||||
if (key != null) {
|
if (key != null) {
|
||||||
key.cancel();
|
key.cancel();
|
||||||
|
int cancelledKeys = (++ worker.cancelledKeys);
|
||||||
|
if (cancelledKeys >= 128) { // FIXME hardcoded value
|
||||||
|
worker.cancelledKeys = 0;
|
||||||
|
// Reclaim the associated file descriptors immediately.
|
||||||
|
// Otherwise the process will experience sudden spike
|
||||||
|
// in the number of open files, with high chance of getting
|
||||||
|
// the 'too many open files' error.
|
||||||
|
selector.selectNow();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
channel.socket.close();
|
channel.socket.close();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user