Simplify selector close method (#25838)
Currently we have an option to interrupt the selector thread on close. This option is not needed as we do not call this method and we should not be blocking on the network thread. Instead we only need to ever call wakeup() on the raw selector.
This commit is contained in:
parent
315319b763
commit
2d22bad53f
|
@ -30,6 +30,7 @@ import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
@ -49,6 +50,7 @@ public abstract class ESSelector implements Closeable {
|
||||||
|
|
||||||
private final EventHandler eventHandler;
|
private final EventHandler eventHandler;
|
||||||
private final ReentrantLock runLock = new ReentrantLock();
|
private final ReentrantLock runLock = new ReentrantLock();
|
||||||
|
private final CountDownLatch exitedLoop = new CountDownLatch(1);
|
||||||
private final AtomicBoolean isClosed = new AtomicBoolean(false);
|
private final AtomicBoolean isClosed = new AtomicBoolean(false);
|
||||||
private final PlainActionFuture<Boolean> isRunningFuture = PlainActionFuture.newFuture();
|
private final PlainActionFuture<Boolean> isRunningFuture = PlainActionFuture.newFuture();
|
||||||
private final Set<NioChannel> registeredChannels = Collections.newSetFromMap(new ConcurrentHashMap<NioChannel, Boolean>());
|
private final Set<NioChannel> registeredChannels = Collections.newSetFromMap(new ConcurrentHashMap<NioChannel, Boolean>());
|
||||||
|
@ -64,8 +66,7 @@ public abstract class ESSelector implements Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts this selector. The selector will run until {@link #close()} or {@link #close(boolean)} is
|
* Starts this selector. The selector will run until {@link #close()} is called.
|
||||||
* called.
|
|
||||||
*/
|
*/
|
||||||
public void runLoop() {
|
public void runLoop() {
|
||||||
if (runLock.tryLock()) {
|
if (runLock.tryLock()) {
|
||||||
|
@ -85,6 +86,7 @@ public abstract class ESSelector implements Closeable {
|
||||||
eventHandler.closeSelectorException(e);
|
eventHandler.closeSelectorException(e);
|
||||||
} finally {
|
} finally {
|
||||||
runLock.unlock();
|
runLock.unlock();
|
||||||
|
exitedLoop.countDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,17 +158,15 @@ public abstract class ESSelector implements Closeable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
close(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close(boolean shouldInterrupt) throws IOException {
|
|
||||||
if (isClosed.compareAndSet(false, true)) {
|
if (isClosed.compareAndSet(false, true)) {
|
||||||
if (shouldInterrupt && thread != null) {
|
wakeup();
|
||||||
thread.interrupt();
|
if (isRunning()) {
|
||||||
} else {
|
try {
|
||||||
wakeup();
|
exitedLoop.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
eventHandler.uncaughtException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
runLock.lock(); // wait for the shutdown to complete
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue