Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nio2Session improvements #494

Merged
merged 3 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -65,7 +65,7 @@ public class Nio2Session extends AbstractCloseable implements IoSession {
private final SocketAddress remoteAddress;
private final SocketAddress acceptanceAddress;
private final PropertyResolver propertyResolver;
private final Queue<Nio2DefaultIoWriteFuture> writes = new LinkedTransferQueue<>();
private final Queue<Nio2DefaultIoWriteFuture> writes = new ConcurrentLinkedQueue<>();
private final AtomicReference<Nio2DefaultIoWriteFuture> currentWrite = new AtomicReference<>();
private final AtomicLong readCyclesCounter = new AtomicLong();
private final AtomicLong lastReadCycleStart = new AtomicLong();
Expand Down Expand Up @@ -516,7 +516,8 @@ protected void startWriting() {
doWriteCycle(buffer, handler);
}
} catch (Throwable e) {
future.setWritten();
future.setException(e);
finishWrite(future);

if (e instanceof RuntimeException) {
throw (RuntimeException) e;
Expand Down Expand Up @@ -555,17 +556,22 @@ protected void handleCompletedWriteCycle(
Nio2CompletionHandler<Integer, Object> completionHandler, Integer result, Object attachment) {
if (buffer.hasRemaining()) {
try {
socket.write(buffer, null, completionHandler);
if (log.isDebugEnabled()) {
log.debug("handleCompletedWriteCycle({}) incomplete write of writeLen={}. Written result={}, resume writing buffer.remaining()={} at cycle={} after {} nanos",
this, writeLen, result, buffer.remaining(), writeCyclesCounter.get(), System.nanoTime() - lastWriteCycleStart.get());
}

doWriteCycle(buffer, completionHandler);
} catch (Throwable t) {
debug("handleCompletedWriteCycle({}) {} while writing to socket len={}: {}",
this, t.getClass().getSimpleName(), writeLen, t.getMessage(), t);
future.setWritten();
warn("handleCompletedWriteCycle({}) {} while writing to socket len={}, result={}: {}",
this, t.getClass().getSimpleName(), writeLen, result, t.getMessage(), t);
future.setException(t);
finishWrite(future);
}
} else {
if (log.isTraceEnabled()) {
log.trace("handleCompletedWriteCycle({}) finished writing len={} at cycle={} after {} nanos",
this, writeLen, writeCyclesCounter, System.nanoTime() - lastWriteCycleStart.get());
this, writeLen, writeCyclesCounter.get(), System.nanoTime() - lastWriteCycleStart.get());
}

// This should be called before future.setWritten() to avoid WriteAbortedException
Expand Down