netty: Add ProtocolNegotiator.close

This notifies the negotiator when it will no longer be used, allowing it
to clean up any resources.
This commit is contained in:
Eric Anderson 2018-09-25 08:23:00 -07:00
parent 09737fea7a
commit 99a2cac07a
6 changed files with 49 additions and 0 deletions

View File

@ -61,6 +61,11 @@ public abstract class AltsProtocolNegotiator implements ProtocolNegotiator {
handshakerFactory.newHandshaker(grpcHandler.getAuthority()))), handshakerFactory.newHandshaker(grpcHandler.getAuthority()))),
new TsiFrameHandler()); new TsiFrameHandler());
} }
@Override
public void close() {
// TODO(jiangtaoli2016): release resources
}
}; };
} }
@ -75,6 +80,11 @@ public abstract class AltsProtocolNegotiator implements ProtocolNegotiator {
new TsiHandshakeHandler(new NettyTsiHandshaker(handshakerFactory.newHandshaker(null))), new TsiHandshakeHandler(new NettyTsiHandshaker(handshakerFactory.newHandshaker(null))),
new TsiFrameHandler()); new TsiFrameHandler());
} }
@Override
public void close() {
// TODO(jiangtaoli2016): release resources
}
}; };
} }

View File

@ -49,4 +49,10 @@ public final class GoogleDefaultProtocolNegotiator implements ProtocolNegotiator
return tlsProtocolNegotiator.newHandler(grpcHandler); return tlsProtocolNegotiator.newHandler(grpcHandler);
} }
} }
@Override
public void close() {
altsProtocolNegotiator.close();
tlsProtocolNegotiator.close();
}
} }

View File

@ -526,6 +526,7 @@ public final class NettyChannelBuilder
} }
closed = true; closed = true;
protocolNegotiator.close();
if (usingSharedGroup) { if (usingSharedGroup) {
SharedResourceHolder.release(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP, group); SharedResourceHolder.release(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP, group);
} }

View File

@ -43,4 +43,11 @@ public interface ProtocolNegotiator {
* completed successfully. * completed successfully.
*/ */
Handler newHandler(GrpcHttp2ConnectionHandler grpcHandler); Handler newHandler(GrpcHttp2ConnectionHandler grpcHandler);
/**
* Releases resources held by this negotiator. Called when the Channel transitions to terminated.
* Is currently only supported on client-side; server-side protocol negotiators will not see this
* method called.
*/
void close();
} }

View File

@ -104,6 +104,9 @@ public final class ProtocolNegotiators {
return new PlaintextHandler(); return new PlaintextHandler();
} }
@Override
public void close() {}
}; };
} }
@ -117,6 +120,9 @@ public final class ProtocolNegotiators {
public Handler newHandler(GrpcHttp2ConnectionHandler handler) { public Handler newHandler(GrpcHttp2ConnectionHandler handler) {
return new ServerTlsHandler(sslContext, handler); return new ServerTlsHandler(sslContext, handler);
} }
@Override
public void close() {}
}; };
} }
@ -207,6 +213,13 @@ public final class ProtocolNegotiators {
return new BufferUntilProxyTunnelledHandler( return new BufferUntilProxyTunnelledHandler(
proxyHandler, negotiator.newHandler(http2Handler)); proxyHandler, negotiator.newHandler(http2Handler));
} }
// This method is not normally called, because we use httpProxy on a per-connection basis in
// NettyChannelBuilder. Instead, we expect `negotiator' to be closed by NettyTransportFactory.
@Override
public void close() {
negotiator.close();
}
} }
return new ProxyNegotiator(); return new ProxyNegotiator();
@ -310,6 +323,9 @@ public final class ProtocolNegotiators {
}; };
return new BufferUntilTlsNegotiatedHandler(sslBootstrap, handler); return new BufferUntilTlsNegotiatedHandler(sslBootstrap, handler);
} }
@Override
public void close() {}
} }
/** A tuple of (host, port). */ /** A tuple of (host, port). */
@ -341,6 +357,9 @@ public final class ProtocolNegotiators {
new HttpClientUpgradeHandler(httpClientCodec, upgradeCodec, 1000); new HttpClientUpgradeHandler(httpClientCodec, upgradeCodec, 1000);
return new BufferingHttp2UpgradeHandler(upgrader, handler); return new BufferingHttp2UpgradeHandler(upgrader, handler);
} }
@Override
public void close() {}
} }
/** /**
@ -357,6 +376,9 @@ public final class ProtocolNegotiators {
public Handler newHandler(GrpcHttp2ConnectionHandler handler) { public Handler newHandler(GrpcHttp2ConnectionHandler handler) {
return new BufferUntilChannelActiveHandler(handler); return new BufferUntilChannelActiveHandler(handler);
} }
@Override
public void close() {}
} }
private static RuntimeException unavailableException(String msg) { private static RuntimeException unavailableException(String msg) {

View File

@ -821,5 +821,8 @@ public class NettyClientTransportTest {
this.grpcHandler = grpcHandler; this.grpcHandler = grpcHandler;
return handler = new NoopHandler(grpcHandler); return handler = new NoopHandler(grpcHandler);
} }
@Override
public void close() {}
} }
} }