-
Notifications
You must be signed in to change notification settings - Fork 972
/
BlockingWriteTimeoutHandler.java
270 lines (226 loc) · 9.02 KB
/
BlockingWriteTimeoutHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* JBoss, Home of Professional Open Source.
* Copyright 2020 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.server.handlers;
import static org.wildfly.common.Assert.checkNotNullParam;
import static org.wildfly.common.Assert.checkNotNullParamWithNullPointerException;
import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.server.ConduitWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.ServerConnection;
import io.undertow.util.ConduitFactory;
import org.xnio.IoUtils;
import org.xnio.XnioIoThread;
import org.xnio.XnioWorker;
import org.xnio.channels.StreamSourceChannel;
import org.xnio.channels.WriteTimeoutException;
import org.xnio.conduits.StreamSinkConduit;
import org.xnio.conduits.WriteReadyHandler;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* BlockingTimeoutHandler allows configurable blocking I/O timeouts for write
* operations within an exchange.
* <p>
* Unlike Options.WRITE_TIMEOUT this only applies to blocking operations which
* can be helpful to prevent the worker pool from becoming saturated when
* clients stop responding.
* <p>
* When a timeout occurs, a {@link WriteTimeoutException} is thrown, and the
* {@link ServerConnection} is closed.
*
* @author Carter Kozak
*/
public final class BlockingWriteTimeoutHandler implements HttpHandler {
private final HttpHandler next;
private final ConduitWrapper<StreamSinkConduit> streamSinkConduitWrapper;
private BlockingWriteTimeoutHandler(HttpHandler next, Duration writeTimeout) {
this.next = next;
this.streamSinkConduitWrapper = new TimeoutStreamSinkConduitWrapper(writeTimeout);
}
private static final class TimeoutStreamSinkConduitWrapper implements ConduitWrapper<StreamSinkConduit> {
private final long timeoutNanoseconds;
TimeoutStreamSinkConduitWrapper(Duration writeTimeout) {
this.timeoutNanoseconds = writeTimeout.toNanos();
}
@Override
public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
return new TimeoutStreamSinkConduit(factory.create(), exchange.getConnection(), timeoutNanoseconds);
}
}
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.addResponseWrapper(streamSinkConduitWrapper);
next.handleRequest(exchange);
}
private static final class TimeoutStreamSinkConduit implements StreamSinkConduit {
private final StreamSinkConduit delegate;
private final ServerConnection serverConnection;
private final long timeoutNanos;
private long remaining;
TimeoutStreamSinkConduit(
StreamSinkConduit delegate,
ServerConnection serverConnection,
long timeoutNanos) {
this.delegate = delegate;
this.serverConnection = serverConnection;
this.timeoutNanos = timeoutNanos;
this.remaining = timeoutNanos;
}
@Override
public long transferFrom(FileChannel fileChannel, long position, long count) throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.transferFrom(fileChannel, position, count));
}
@Override
public long transferFrom(
StreamSourceChannel streamSourceChannel,
long count,
ByteBuffer byteBuffer) throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.transferFrom(streamSourceChannel, count, byteBuffer));
}
@Override
public int write(ByteBuffer byteBuffer) throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.write(byteBuffer));
}
@Override
public long write(ByteBuffer[] byteBuffers, int offset, int length) throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.write(byteBuffers, offset, length));
}
@Override
public int writeFinal(ByteBuffer byteBuffer) throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.writeFinal(byteBuffer));
}
@Override
public long writeFinal(ByteBuffer[] byteBuffers, int offset, int length) throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.writeFinal(byteBuffers, offset, length));
}
@Override
public void terminateWrites() throws IOException {
delegate.terminateWrites();
}
@Override
public boolean isWriteShutdown() {
return delegate.isWriteShutdown();
}
@Override
public void resumeWrites() {
delegate.resumeWrites();
}
@Override
public void suspendWrites() {
delegate.suspendWrites();
}
@Override
public void wakeupWrites() {
delegate.wakeupWrites();
}
@Override
public boolean isWriteResumed() {
return delegate.isWriteResumed();
}
@Override
public void awaitWritable() throws IOException {
awaitWritable(remaining, TimeUnit.NANOSECONDS);
}
@Override
public void awaitWritable(long duration, TimeUnit unit) throws IOException {
long startTime = System.nanoTime();
long requestedNanos = unit.toNanos(duration);
try {
delegate.awaitWritable(Math.min(requestedNanos, remaining), TimeUnit.NANOSECONDS);
} finally {
remaining -= System.nanoTime() - startTime;
}
if (remaining < 0) {
WriteTimeoutException wte = UndertowMessages.MESSAGES.blockingWriteTimedOut(timeoutNanos);
UndertowLogger.REQUEST_IO_LOGGER.blockingWriteTimedOut(wte);
IoUtils.safeClose(serverConnection);
throw wte;
}
}
@Override
public XnioIoThread getWriteThread() {
return delegate.getWriteThread();
}
@Override
public void setWriteReadyHandler(WriteReadyHandler writeReadyHandler) {
delegate.setWriteReadyHandler(writeReadyHandler);
}
@Override
public void truncateWrites() throws IOException {
delegate.truncateWrites();
}
@Override
public boolean flush() throws IOException {
return resetTimeoutIfWriteSucceeded(delegate.flush());
}
@Override
public XnioWorker getWorker() {
return delegate.getWorker();
}
private long resetTimeoutIfWriteSucceeded(long value) {
if (value != 0) {
// Reset the timeout
remaining = timeoutNanos;
}
return value;
}
private int resetTimeoutIfWriteSucceeded(int value) {
if (value != 0) {
// Reset the timeout
remaining = timeoutNanos;
}
return value;
}
private boolean resetTimeoutIfWriteSucceeded(boolean value) {
if (value) {
// Reset the timeout
remaining = timeoutNanos;
}
return value;
}
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private HttpHandler nextHandler;
private Duration writeTimeout;
private Builder() {}
public Builder writeTimeout(Duration writeTimeout) {
this.writeTimeout = checkNotNullParamWithNullPointerException("writeTimeout", writeTimeout);
return this;
}
public Builder nextHandler(HttpHandler nextHandler) {
this.nextHandler = checkNotNullParamWithNullPointerException("nextHandler", nextHandler);
return this;
}
public HttpHandler build() {
HttpHandler next = checkNotNullParamWithNullPointerException("nextHandler", nextHandler);
checkNotNullParam("writeTimeout", writeTimeout);
if (writeTimeout.isZero() || writeTimeout.isNegative()) {
throw new IllegalArgumentException("Write timeout must be positive: " + writeTimeout);
}
return new BlockingWriteTimeoutHandler(next, writeTimeout);
}
}
}