Skip to content

Commit 95b9d6d

Browse files
authored
Revert "okhttp: add max connection idle at OkHttpServer (grpc#9494)" (grpc#9528)
This reverts commit 7291ad4.
1 parent 53a2d50 commit 95b9d6d

File tree

3 files changed

+6
-119
lines changed

3 files changed

+6
-119
lines changed

okhttp/src/main/java/io/grpc/okhttp/OkHttpServerBuilder.java

-28
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.grpc.okhttp;
1818

19-
import static com.google.common.base.Preconditions.checkArgument;
20-
2119
import com.google.common.annotations.VisibleForTesting;
2220
import com.google.common.base.Preconditions;
2321
import com.google.errorprone.annotations.DoNotCall;
@@ -64,10 +62,6 @@
6462
public final class OkHttpServerBuilder extends ForwardingServerBuilder<OkHttpServerBuilder> {
6563
private static final Logger log = Logger.getLogger(OkHttpServerBuilder.class.getName());
6664
private static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535;
67-
68-
static final long MAX_CONNECTION_IDLE_NANOS_DISABLED = Long.MAX_VALUE;
69-
private static final long MIN_MAX_CONNECTION_IDLE_NANO = TimeUnit.SECONDS.toNanos(1L);
70-
7165
private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000L);
7266
private static final ObjectPool<Executor> DEFAULT_TRANSPORT_EXECUTOR_POOL =
7367
OkHttpChannelBuilder.DEFAULT_TRANSPORT_EXECUTOR_POOL;
@@ -116,7 +110,6 @@ public static OkHttpServerBuilder forPort(SocketAddress address, ServerCredentia
116110
int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW;
117111
int maxInboundMetadataSize = GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE;
118112
int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
119-
long maxConnectionIdleInNanos = MAX_CONNECTION_IDLE_NANOS_DISABLED;
120113

121114
@VisibleForTesting
122115
OkHttpServerBuilder(
@@ -185,27 +178,6 @@ public OkHttpServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit)
185178
return this;
186179
}
187180

188-
/**
189-
* Sets a custom max connection idle time, connection being idle for longer than which will be
190-
* gracefully terminated. Idleness duration is defined since the most recent time the number of
191-
* outstanding RPCs became zero or the connection establishment. An unreasonably small value might
192-
* be increased. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
193-
* max connection idle.
194-
*/
195-
@Override
196-
public OkHttpServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
197-
checkArgument(maxConnectionIdle > 0L, "max connection idle must be positive: %s",
198-
maxConnectionIdle);
199-
maxConnectionIdleInNanos = timeUnit.toNanos(maxConnectionIdle);
200-
if (maxConnectionIdleInNanos >= AS_LARGE_AS_INFINITE) {
201-
maxConnectionIdleInNanos = MAX_CONNECTION_IDLE_NANOS_DISABLED;
202-
}
203-
if (maxConnectionIdleInNanos < MIN_MAX_CONNECTION_IDLE_NANO) {
204-
maxConnectionIdleInNanos = MIN_MAX_CONNECTION_IDLE_NANO;
205-
}
206-
return this;
207-
}
208-
209181
/**
210182
* Sets a time waiting for read activity after sending a keepalive ping. If the time expires
211183
* without any read activity on the connection, the connection is considered dead. An unreasonably

okhttp/src/main/java/io/grpc/okhttp/OkHttpServerTransport.java

-23
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.grpc.okhttp;
1818

19-
import static io.grpc.okhttp.OkHttpServerBuilder.MAX_CONNECTION_IDLE_NANOS_DISABLED;
20-
2119
import com.google.common.base.Preconditions;
2220
import com.google.common.util.concurrent.Futures;
2321
import com.google.common.util.concurrent.ListenableFuture;
@@ -30,7 +28,6 @@
3028
import io.grpc.Status;
3129
import io.grpc.internal.GrpcUtil;
3230
import io.grpc.internal.KeepAliveManager;
33-
import io.grpc.internal.MaxConnectionIdleManager;
3431
import io.grpc.internal.ObjectPool;
3532
import io.grpc.internal.SerializingExecutor;
3633
import io.grpc.internal.ServerTransport;
@@ -94,7 +91,6 @@ final class OkHttpServerTransport implements ServerTransport,
9491
private ScheduledExecutorService scheduledExecutorService;
9592
private Attributes attributes;
9693
private KeepAliveManager keepAliveManager;
97-
private MaxConnectionIdleManager maxConnectionIdleManager;
9894

9995
private final Object lock = new Object();
10096
@GuardedBy("lock")
@@ -193,11 +189,6 @@ private void startIo(SerializingExecutor serializingExecutor) {
193189
keepAliveManager.onTransportStarted();
194190
}
195191

196-
if (config.maxConnectionIdleNanos != MAX_CONNECTION_IDLE_NANOS_DISABLED) {
197-
maxConnectionIdleManager = new MaxConnectionIdleManager(config.maxConnectionIdleNanos);
198-
maxConnectionIdleManager.start(this::shutdown, scheduledExecutorService);
199-
}
200-
201192
transportExecutor.execute(
202193
new FrameHandler(variant.newReader(Okio.buffer(Okio.source(socket)), false)));
203194
} catch (Error | IOException | RuntimeException ex) {
@@ -320,9 +311,6 @@ private void terminated() {
320311
if (keepAliveManager != null) {
321312
keepAliveManager.onTransportTermination();
322313
}
323-
if (maxConnectionIdleManager != null) {
324-
maxConnectionIdleManager.onTransportTermination();
325-
}
326314
transportExecutor = config.transportExecutorPool.returnObject(transportExecutor);
327315
scheduledExecutorService =
328316
config.scheduledExecutorServicePool.returnObject(scheduledExecutorService);
@@ -381,9 +369,6 @@ public OutboundFlowController.StreamState[] getActiveStreams() {
381369
void streamClosed(int streamId, boolean flush) {
382370
synchronized (lock) {
383371
streams.remove(streamId);
384-
if (maxConnectionIdleManager != null && streams.isEmpty()) {
385-
maxConnectionIdleManager.onTransportIdle();
386-
}
387372
if (gracefulShutdown && streams.isEmpty()) {
388373
frameWriter.close();
389374
} else {
@@ -448,7 +433,6 @@ static final class Config {
448433
final int flowControlWindow;
449434
final int maxInboundMessageSize;
450435
final int maxInboundMetadataSize;
451-
final long maxConnectionIdleNanos;
452436

453437
public Config(
454438
OkHttpServerBuilder builder,
@@ -468,7 +452,6 @@ public Config(
468452
flowControlWindow = builder.flowControlWindow;
469453
maxInboundMessageSize = builder.maxInboundMessageSize;
470454
maxInboundMetadataSize = builder.maxInboundMetadataSize;
471-
maxConnectionIdleNanos = builder.maxConnectionIdleInNanos;
472455
}
473456
}
474457

@@ -714,9 +697,6 @@ public void headers(boolean outFinished,
714697
authority == null ? null : asciiString(authority),
715698
statsTraceCtx,
716699
tracer);
717-
if (maxConnectionIdleManager != null && streams.isEmpty()) {
718-
maxConnectionIdleManager.onTransportActive();
719-
}
720700
streams.put(streamId, stream);
721701
listener.streamCreated(streamForApp, method, metadata);
722702
stream.onStreamAllocated();
@@ -973,9 +953,6 @@ private void respondWithHttpError(
973953
synchronized (lock) {
974954
Http2ErrorStreamState stream =
975955
new Http2ErrorStreamState(streamId, lock, outboundFlow, config.flowControlWindow);
976-
if (maxConnectionIdleManager != null && streams.isEmpty()) {
977-
maxConnectionIdleManager.onTransportActive();
978-
}
979956
streams.put(streamId, stream);
980957
if (inFinished) {
981958
stream.inboundDataReceived(new Buffer(), 0, true);

okhttp/src/test/java/io/grpc/okhttp/OkHttpServerTransportTest.java

+6-68
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
public class OkHttpServerTransportTest {
9191
private static final int TIME_OUT_MS = 2000;
9292
private static final int INITIAL_WINDOW_SIZE = 65535;
93-
private static final long MAX_CONNECTION_IDLE = TimeUnit.SECONDS.toNanos(1);
9493

9594
private MockServerTransportListener mockTransportListener = new MockServerTransportListener();
9695
private ServerTransportListener transportListener
@@ -106,11 +105,10 @@ public class OkHttpServerTransportTest {
106105
private ExecutorService threadPool = Executors.newCachedThreadPool();
107106
private HandshakerSocketFactory handshakerSocketFactory
108107
= mock(HandshakerSocketFactory.class, delegatesTo(new PlaintextHandshakerSocketFactory()));
109-
private final FakeClock fakeClock = new FakeClock();
110108
private OkHttpServerBuilder serverBuilder
111109
= new OkHttpServerBuilder(new InetSocketAddress(1234), handshakerSocketFactory)
112110
.executor(new FakeClock().getScheduledExecutorService()) // Executor unused
113-
.scheduledExecutorService(fakeClock.getScheduledExecutorService())
111+
.scheduledExecutorService(new FakeClock().getScheduledExecutorService())
114112
.transportExecutor(new Executor() {
115113
@Override public void execute(Runnable runnable) {
116114
if (runnable instanceof OkHttpServerTransport.FrameHandler) {
@@ -121,8 +119,7 @@ public class OkHttpServerTransportTest {
121119
}
122120
}
123121
})
124-
.flowControlWindow(INITIAL_WINDOW_SIZE)
125-
.maxConnectionIdle(MAX_CONNECTION_IDLE, TimeUnit.NANOSECONDS);
122+
.flowControlWindow(INITIAL_WINDOW_SIZE);
126123

127124
@Rule public final Timeout globalTimeout = Timeout.seconds(10);
128125

@@ -149,63 +146,6 @@ public void startThenShutdown() throws Exception {
149146
shutdownAndTerminate(/*lastStreamId=*/ 0);
150147
}
151148

152-
@Test
153-
public void maxConnectionIdleTimer() throws Exception {
154-
initTransport();
155-
handshake();
156-
clientFrameWriter.headers(1, Arrays.asList(
157-
HTTP_SCHEME_HEADER,
158-
METHOD_HEADER,
159-
new Header(Header.TARGET_AUTHORITY, "example.com:80"),
160-
new Header(Header.TARGET_PATH, "/com.example/SimpleService.doit"),
161-
CONTENT_TYPE_HEADER,
162-
TE_HEADER));
163-
clientFrameWriter.synStream(true, false, 1, -1, Arrays.asList(
164-
new Header("some-client-sent-trailer", "trailer-value")));
165-
pingPong();
166-
167-
MockStreamListener streamListener = mockTransportListener.newStreams.pop();
168-
assertThat(streamListener.messages.peek()).isNull();
169-
assertThat(streamListener.halfClosedCalled).isTrue();
170-
171-
streamListener.stream.close(Status.OK, new Metadata());
172-
173-
List<Header> responseTrailers = Arrays.asList(
174-
new Header(":status", "200"),
175-
CONTENT_TYPE_HEADER,
176-
new Header("grpc-status", "0"));
177-
assertThat(clientFrameReader.nextFrame(clientFramesRead)).isTrue();
178-
verify(clientFramesRead)
179-
.headers(false, true, 1, -1, responseTrailers, HeadersMode.HTTP_20_HEADERS);
180-
181-
fakeClock.forwardNanos(MAX_CONNECTION_IDLE);
182-
fakeClock.forwardNanos(MAX_CONNECTION_IDLE);
183-
verifyGracefulShutdown(1);
184-
}
185-
186-
@Test
187-
public void maxConnectionIdleTimer_respondWithError() throws Exception {
188-
initTransport();
189-
handshake();
190-
191-
clientFrameWriter.headers(1, Arrays.asList(
192-
HTTP_SCHEME_HEADER,
193-
METHOD_HEADER,
194-
new Header(Header.TARGET_PATH, "/com.example/SimpleService.doit"),
195-
CONTENT_TYPE_HEADER,
196-
TE_HEADER,
197-
new Header("host", "example.com:80"),
198-
new Header("host", "example.com:80")));
199-
clientFrameWriter.flush();
200-
201-
verifyHttpError(
202-
1, 400, Status.Code.INTERNAL, "Multiple host headers disallowed. RFC7230 section 5.4");
203-
204-
fakeClock.forwardNanos(MAX_CONNECTION_IDLE);
205-
fakeClock.forwardNanos(MAX_CONNECTION_IDLE);
206-
verifyGracefulShutdown(1);
207-
}
208-
209149
@Test
210150
public void startThenShutdownTwice() throws Exception {
211151
initTransport();
@@ -376,8 +316,7 @@ public void activeRpc_delaysShutdownTermination() throws Exception {
376316
clientFrameWriter.data(true, 1, requestMessageFrame, (int) requestMessageFrame.size());
377317
pingPong();
378318

379-
serverTransport.shutdown();
380-
verifyGracefulShutdown(1);
319+
shutdownAndVerifyGraceful(1);
381320
verify(transportListener, never()).transportTerminated();
382321

383322
MockStreamListener streamListener = mockTransportListener.newStreams.pop();
@@ -1099,8 +1038,8 @@ private Metadata metadata(String... keysAndValues) {
10991038
return metadata;
11001039
}
11011040

1102-
private void verifyGracefulShutdown(int lastStreamId)
1103-
throws IOException {
1041+
private void shutdownAndVerifyGraceful(int lastStreamId) throws IOException {
1042+
serverTransport.shutdown();
11041043
assertThat(clientFrameReader.nextFrame(clientFramesRead)).isTrue();
11051044
verify(clientFramesRead).goAway(2147483647, ErrorCode.NO_ERROR, ByteString.EMPTY);
11061045
assertThat(clientFrameReader.nextFrame(clientFramesRead)).isTrue();
@@ -1113,8 +1052,7 @@ private void verifyGracefulShutdown(int lastStreamId)
11131052

11141053
private void shutdownAndTerminate(int lastStreamId) throws IOException {
11151054
assertThat(serverTransport.getActiveStreams().length).isEqualTo(0);
1116-
serverTransport.shutdown();
1117-
verifyGracefulShutdown(lastStreamId);
1055+
shutdownAndVerifyGraceful(lastStreamId);
11181056
assertThat(clientFrameReader.nextFrame(clientFramesRead)).isFalse();
11191057
verify(transportListener, timeout(TIME_OUT_MS)).transportTerminated();
11201058
}

0 commit comments

Comments
 (0)