Skip to content

Commit

Permalink
[UNDERTOW-2035] Add Http2StreamSinkChannel.awaitWritable(long, TimeUn…
Browse files Browse the repository at this point in the history
…it method).
  • Loading branch information
fl4via committed Jun 10, 2022
1 parent bf39a3a commit 5261e01
Showing 1 changed file with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

import io.undertow.UndertowMessages;
import io.undertow.connector.PooledByteBuffer;
Expand Down Expand Up @@ -184,20 +185,44 @@ protected PooledByteBuffer[] allocateAll(PooledByteBuffer[] allHeaderBuffers, Po
* Invokes super awaitWritable, with an extra check for flowControlWindow. The purpose of this is to
* warn clearly that peer is not updating the flow control window.
*
* This method will block for the maximum amount of time specified by {@link #getAwaitWritableTimeout()}
*
* @throws IOException if an IO error occurs
*/
public void awaitWritable() throws IOException {
awaitWritable(getAwaitWritableTimeout() * 1_000_000);
}

/**
* Invokes super awaitWritable, with an extra check for flowControlWindow. The purpose of this is to
* warn clearly that peer is not updating the flow control window.
*
* @param time the time to wait
* @param timeUnit the time unit
* @throws IOException if an IO error occurs
*/
public void awaitWritable(long time, TimeUnit timeUnit) throws IOException {
awaitWritable(timeUnit.toNanos(time));
}

/**
* Invokes super awaitWritable, with an extra check for flowControlWindow. The purpose of this is to
* warn clearly that peer is not updating the flow control window.
*
* @param timeoutInNanos the time to wait in nanoseconds
* @throws IOException if an IO error occurs
*/
protected void awaitWritable(final long timeoutInNanos) throws IOException {
final int flowControlWindow;
synchronized (flowControlLock) {
flowControlWindow = this.flowControlWindow;
}
long initialTime = System.nanoTime();
super.awaitWritable();
super.awaitWritable(timeoutInNanos);
synchronized (flowControlLock) {
if (isReadyForFlush() && flowControlWindow <= 0 && flowControlWindow == this.flowControlWindow) {
final long timeout = getAwaitWritableTimeout();
long remainingTimeout;
while ((remainingTimeout = timeout * 1_000_000 - (System.nanoTime() - initialTime)) > 0) {
while ((remainingTimeout = timeoutInNanos - (System.nanoTime() - initialTime)) > 0) {
try {
flowControlLock.wait(remainingTimeout/ 1_000_000, (int) (remainingTimeout % 1_000_000));
} catch (InterruptedException e) {
Expand All @@ -207,7 +232,7 @@ public void awaitWritable() throws IOException {
if (flowControlWindow != this.flowControlWindow)
return;
}
throw UndertowMessages.MESSAGES.noWindowUpdate(getAwaitWritableTimeout());
throw UndertowMessages.MESSAGES.noWindowUpdate(timeoutInNanos / 1_000_000);
}
}
}
Expand Down

0 comments on commit 5261e01

Please # to comment.