Skip to content

Commit e028d86

Browse files
committed
servlet: add extra 5s to AsyncContext timeout
1 parent c643e68 commit e028d86

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

servlet/src/main/java/io/grpc/servlet/ServletAdapter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,9 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
128128
}
129129

130130
Long timeoutNanos = headers.get(TIMEOUT_KEY);
131-
if (timeoutNanos == null) {
132-
timeoutNanos = 0L;
133-
}
134-
asyncCtx.setTimeout(TimeUnit.NANOSECONDS.toMillis(timeoutNanos));
131+
asyncCtx.setTimeout(timeoutNanos != null
132+
? TimeUnit.NANOSECONDS.toMillis(timeoutNanos) + ASYNC_TIMEOUT_SAFETY_MARGIN
133+
: 0);
135134
StatsTraceContext statsTraceCtx =
136135
StatsTraceContext.newServerContext(streamTracerFactories, method, headers);
137136

@@ -158,6 +157,11 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
158157
asyncCtx.addListener(new GrpcAsyncListener(stream, logId));
159158
}
160159

160+
/**
161+
* Deadlines are managed via Context, servlet async timeout is not supposed to happen.
162+
*/
163+
private static final long ASYNC_TIMEOUT_SAFETY_MARGIN = 5_000;
164+
161165
// This method must use Enumeration and its members, since that is the only way to read headers
162166
// from the servlet api.
163167
@SuppressWarnings("JdkObsolete")

0 commit comments

Comments
 (0)