-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ProxyConnectConnectionFactoryFilter
leaks connection in case of errors
#1002
Merged
idelpivnitskiy
merged 9 commits into
apple:master
from
idelpivnitskiy:proxy-close-connection
Apr 9, 2020
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a9cf37
`ProxyConnectConnectionFactoryFilter` leaks connection in case of errors
idelpivnitskiy a0cd7b8
Address comments
idelpivnitskiy 685900e
Merge remote-tracking branch 'upstream/master' into proxy-close-conne…
idelpivnitskiy d2bf22f
Use whenFinally to prevent handling cancel event after termination
idelpivnitskiy 083347d
Do not drain payload body in case of error
idelpivnitskiy cfd0117
Change whenFinally to afterFinally
idelpivnitskiy 57e470b
Remove close-on-cancel
idelpivnitskiy 0144018
Remove unnecessary try-catch
idelpivnitskiy 12cbd4c
Move handleConnectResponse inside ProxyFilter
idelpivnitskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright © 2019 Apple Inc. and the ServiceTalk project authors | ||
* Copyright © 2019-2020 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -19,12 +19,11 @@ | |
import io.servicetalk.client.api.ConnectionFactoryFilter; | ||
import io.servicetalk.client.api.DelegatingConnectionFactory; | ||
import io.servicetalk.concurrent.SingleSource; | ||
import io.servicetalk.concurrent.api.ListenableAsyncCloseable; | ||
import io.servicetalk.concurrent.api.Single; | ||
import io.servicetalk.http.api.FilterableStreamingHttpConnection; | ||
import io.servicetalk.http.api.HttpExecutionStrategy; | ||
import io.servicetalk.http.api.HttpExecutionStrategyInfluencer; | ||
import io.servicetalk.http.api.StreamingHttpRequestResponseFactory; | ||
import io.servicetalk.http.api.StreamingHttpResponse; | ||
import io.servicetalk.transport.netty.internal.DeferSslHandler; | ||
import io.servicetalk.transport.netty.internal.NettyConnectionContext; | ||
|
||
|
@@ -47,17 +46,12 @@ | |
* @param <ResolvedAddress> The type of a resolved address that can be used for connecting. | ||
* @param <C> The type of connections created by this factory. | ||
*/ | ||
final class ProxyConnectConnectionFactoryFilter<ResolvedAddress, C | ||
extends ListenableAsyncCloseable & FilterableStreamingHttpConnection> | ||
implements ConnectionFactoryFilter<ResolvedAddress, C>, | ||
HttpExecutionStrategyInfluencer { | ||
final class ProxyConnectConnectionFactoryFilter<ResolvedAddress, C extends FilterableStreamingHttpConnection> | ||
implements ConnectionFactoryFilter<ResolvedAddress, C>, HttpExecutionStrategyInfluencer { | ||
|
||
private final StreamingHttpRequestResponseFactory reqRespFactory; | ||
private final String connectAddress; | ||
|
||
ProxyConnectConnectionFactoryFilter(final CharSequence connectAddress, | ||
final StreamingHttpRequestResponseFactory reqRespFactory) { | ||
this.reqRespFactory = reqRespFactory; | ||
ProxyConnectConnectionFactoryFilter(final CharSequence connectAddress) { | ||
this.connectAddress = connectAddress.toString(); | ||
} | ||
|
||
|
@@ -74,51 +68,59 @@ private ProxyFilter(final ConnectionFactory<ResolvedAddress, C> delegate) { | |
|
||
@Override | ||
public Single<C> newConnection(final ResolvedAddress resolvedAddress) { | ||
return delegate().newConnection(resolvedAddress).flatMap(c -> | ||
// We currently only have access to a StreamingHttpRequester, which means we are forced to provide an | ||
// HttpExecutionStrategy. Because we can't be sure if there is any blocking code in the connection | ||
// filters we use the default strategy which should offload everything to be safe. | ||
c.request(defaultStrategy(), | ||
reqRespFactory.connect(connectAddress).addHeader(CONTENT_LENGTH, ZERO)) | ||
.flatMap(response -> { | ||
if (SUCCESSFUL_2XX.contains(response.status())) { | ||
final Channel channel = ((NettyConnectionContext) c.connectionContext()).nettyChannel(); | ||
final SingleSource.Processor<C, C> processor = newSingleProcessor(); | ||
return delegate().newConnection(resolvedAddress).flatMap(c -> { | ||
try { | ||
// We currently only have access to a StreamingHttpRequester, which means we are forced to provide | ||
// an HttpExecutionStrategy. Because we can't be sure if there is any blocking code in the | ||
// connection filters we use the default strategy which should offload everything to be safe. | ||
return c.request(defaultStrategy(), c.connect(connectAddress).addHeader(CONTENT_LENGTH, ZERO)) | ||
.flatMap(response -> handleConnectResponse(c, response)) | ||
// Close recently created connection in case of any error while it connects to the proxy: | ||
.recoverWith(t -> c.closeAsync().concat(failed(t))); | ||
} catch (Throwable t) { | ||
return c.closeAsync().concat(failed(t)); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
channel.pipeline().addLast(new ChannelInboundHandlerAdapter() { | ||
@Override | ||
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) { | ||
if (evt instanceof SslHandshakeCompletionEvent) { | ||
SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt; | ||
if (event.isSuccess()) { | ||
processor.onSuccess(c); | ||
} else { | ||
processor.onError(event.cause()); | ||
} | ||
} | ||
ctx.fireUserEventTriggered(evt); | ||
} | ||
}); | ||
private Single<C> handleConnectResponse(final C connection, final StreamingHttpResponse response) { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this logic to the different method because nested |
||
if (response.status().statusClass() != SUCCESSFUL_2XX) { | ||
return failed(new ProxyResponseException("Non-successful response from proxy CONNECT " + | ||
connectAddress, response.status())); | ||
} | ||
|
||
DeferSslHandler deferSslHandler = channel.pipeline().get(DeferSslHandler.class); | ||
if (deferSslHandler == null) { | ||
return response.payloadBodyAndTrailers().ignoreElements().concat(failed( | ||
new IllegalStateException("Failed to find a handler of type " + | ||
DeferSslHandler.class + " in channel pipeline."))); | ||
final Channel channel = ((NettyConnectionContext) connection.connectionContext()).nettyChannel(); | ||
final SingleSource.Processor<C, C> processor = newSingleProcessor(); | ||
channel.pipeline().addLast(new ChannelInboundHandlerAdapter() { | ||
@Override | ||
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) { | ||
if (evt instanceof SslHandshakeCompletionEvent) { | ||
SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt; | ||
if (event.isSuccess()) { | ||
processor.onSuccess(connection); | ||
} else { | ||
processor.onError(event.cause()); | ||
} | ||
} | ||
ctx.fireUserEventTriggered(evt); | ||
} | ||
}); | ||
|
||
deferSslHandler.ready(); | ||
final DeferSslHandler deferSslHandler = channel.pipeline().get(DeferSslHandler.class); | ||
if (deferSslHandler == null) { | ||
return failed(new IllegalStateException("Failed to find a handler of type " + | ||
DeferSslHandler.class + " in channel pipeline.")); | ||
} | ||
deferSslHandler.ready(); | ||
|
||
// There is no need to apply offloading explicitly (despite completing `processor` on the | ||
// EventLoop) because `payloadBody()` will be offloaded according to the strategy for the | ||
// request. | ||
return response.payloadBodyAndTrailers().ignoreElements().concat(fromSource(processor)); | ||
} else { | ||
return response.payloadBodyAndTrailers().ignoreElements().concat( | ||
failed(new ProxyResponseException("Bad response from proxy CONNECT " + connectAddress, | ||
response.status()))); | ||
} | ||
})); | ||
// There is no need to apply offloading explicitly (despite completing `processor` on the | ||
// EventLoop) because `payloadBody()` will be offloaded according to the strategy for the | ||
// request. | ||
return response.payloadBodyAndTrailers().ignoreElements().concat(fromSource(processor)); | ||
} catch (Throwable t) { | ||
idelpivnitskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return failed(t); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we are being overly paranoid here about the calls to
c.request()
orc.connect()
throwing. Any method returning an asynchronous source is not expected to throw. Having said that it is not a big deal so its ok as it is, I will leave it to you to take a call on this.