|
22 | 22 | import java.io.InterruptedIOException;
|
23 | 23 | import java.nio.ByteBuffer;
|
24 | 24 | import java.nio.channels.FileChannel;
|
| 25 | +import java.security.AccessController; |
| 26 | +import java.security.PrivilegedAction; |
25 | 27 | import java.util.concurrent.TimeUnit;
|
26 | 28 | import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
27 | 29 |
|
28 | 30 | import io.undertow.UndertowLogger;
|
29 | 31 | import io.undertow.UndertowMessages;
|
30 |
| -import io.undertow.UndertowOptions; |
31 | 32 | import io.undertow.connector.PooledByteBuffer;
|
32 | 33 | import io.undertow.util.ImmediatePooledByteBuffer;
|
33 | 34 | import org.xnio.Buffers;
|
34 | 35 | import org.xnio.ChannelListener;
|
35 | 36 | import org.xnio.ChannelListeners;
|
36 | 37 | import org.xnio.IoUtils;
|
37 | 38 | import org.xnio.Option;
|
38 |
| -import org.xnio.Options; |
39 | 39 | import org.xnio.XnioExecutor;
|
40 | 40 | import org.xnio.XnioIoThread;
|
41 | 41 | import org.xnio.XnioWorker;
|
|
59 | 59 | */
|
60 | 60 | public abstract class AbstractFramedStreamSinkChannel<C extends AbstractFramedChannel<C, R, S>, R extends AbstractFramedStreamSourceChannel<C, R, S>, S extends AbstractFramedStreamSinkChannel<C, R, S>> implements StreamSinkChannel {
|
61 | 61 |
|
| 62 | + |
| 63 | + /** |
| 64 | + * The maximum timeout to wait on awaitWritable in milliseconds when not specified. |
| 65 | + */ |
| 66 | + private static final int AWAIT_WRITABLE_TIMEOUT; |
| 67 | + |
| 68 | + static { |
| 69 | + final int defaultAwaitWritableTimeout = 600000; |
| 70 | + int await_writable_timeout = AccessController.doPrivileged((PrivilegedAction<Integer>) () -> Integer.getInteger("io.undertow.await_writable_timeout", defaultAwaitWritableTimeout)); |
| 71 | + AWAIT_WRITABLE_TIMEOUT = await_writable_timeout > 0? await_writable_timeout : defaultAwaitWritableTimeout; |
| 72 | + } |
62 | 73 | private static final PooledByteBuffer EMPTY_BYTE_BUFFER = new ImmediatePooledByteBuffer(ByteBuffer.allocateDirect(0));
|
63 | 74 |
|
64 | 75 | private final C channel;
|
@@ -112,20 +123,11 @@ public abstract class AbstractFramedStreamSinkChannel<C extends AbstractFramedCh
|
112 | 123 | private volatile int inListenerLoop;
|
113 | 124 | /* keep track of successful writes to properly prevent a loop UNDERTOW-1624 */
|
114 | 125 | private volatile boolean writeSucceeded;
|
115 |
| - /** Timeout used by awaitWritable (ms) */ |
116 |
| - private final long awaitWritableTimeout; |
117 | 126 |
|
118 | 127 | private static final AtomicIntegerFieldUpdater<AbstractFramedStreamSinkChannel> inListenerLoopUpdater = AtomicIntegerFieldUpdater.newUpdater(AbstractFramedStreamSinkChannel.class, "inListenerLoop");
|
119 | 128 |
|
120 | 129 | protected AbstractFramedStreamSinkChannel(C channel) {
|
121 | 130 | this.channel = channel;
|
122 |
| - Integer writeTimeout = null; |
123 |
| - try { |
124 |
| - writeTimeout = channel.getOption(Options.WRITE_TIMEOUT); |
125 |
| - } catch (IOException e) { |
126 |
| - UndertowLogger.ROOT_LOGGER.ioException(e); |
127 |
| - } |
128 |
| - awaitWritableTimeout = writeTimeout != null && writeTimeout > 0? writeTimeout : UndertowOptions.DEFAULT_WRITE_TIMEOUT; |
129 | 131 | }
|
130 | 132 |
|
131 | 133 | public long transferFrom(final FileChannel src, final long position, final long count) throws IOException {
|
@@ -296,7 +298,7 @@ public void awaitWritable() throws IOException {
|
296 | 298 | waiterCount++;
|
297 | 299 | //we need to re-check after incrementing the waiters count
|
298 | 300 | if(readyForFlush && !anyAreSet(state, STATE_CLOSED) && !broken) {
|
299 |
| - lock.wait(awaitWritableTimeout); |
| 301 | + lock.wait(AWAIT_WRITABLE_TIMEOUT); |
300 | 302 | }
|
301 | 303 | } catch (InterruptedException e) {
|
302 | 304 | Thread.currentThread().interrupt();
|
@@ -481,7 +483,7 @@ protected boolean safeToSend() throws IOException {
|
481 | 483 | * @return the awaitWritable timeout, in milliseconds
|
482 | 484 | */
|
483 | 485 | protected long getAwaitWritableTimeout() {
|
484 |
| - return this.awaitWritableTimeout; |
| 486 | + return AWAIT_WRITABLE_TIMEOUT; |
485 | 487 | }
|
486 | 488 |
|
487 | 489 | @Override
|
|
0 commit comments