-
Notifications
You must be signed in to change notification settings - Fork 5.5k
network: delayed conn close #4382
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
Changes from 13 commits
4d2426e
94a835a
e2176aa
3a67ecb
30eb02e
9ddf9d3
8e716eb
e71f798
6130f28
890471e
aae3b52
b8eb565
6071d31
36b6087
d056d7a
52c10d9
9d86332
435a518
f2c81ed
6704243
31bcaaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ jobs: | |
| resource_class: xlarge | ||
| working_directory: /source | ||
| environment: | ||
| BAZEL_REMOTE_CACHE: https://storage.googleapis.com/envoy-circleci-bazel-cache/ | ||
|
Member
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. Let's revert these; if we are going to disable the cache, it should be a separate PR.
Contributor
Author
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. Done. |
||
| steps: | ||
| - run: rm -rf /home/circleci/project/.git # CircleCI git caching is likely broken | ||
| - checkout | ||
|
|
@@ -24,7 +23,6 @@ jobs: | |
| resource_class: xlarge | ||
| working_directory: /source | ||
| environment: | ||
| BAZEL_REMOTE_CACHE: https://storage.googleapis.com/envoy-circleci-bazel-cache/ | ||
| steps: | ||
| - run: rm -rf /home/circleci/project/.git # CircleCI git caching is likely broken | ||
| - run: echo $CIRCLE_SHA1 | ||
|
|
@@ -38,7 +36,6 @@ jobs: | |
| resource_class: xlarge | ||
| working_directory: /source | ||
| environment: | ||
| BAZEL_REMOTE_CACHE: https://storage.googleapis.com/envoy-circleci-bazel-cache/ | ||
| steps: | ||
| - run: rm -rf /home/circleci/project/.git # CircleCI git caching is likely broken | ||
| - checkout | ||
|
|
@@ -71,7 +68,6 @@ jobs: | |
| ipv6_tests: | ||
| machine: true | ||
| environment: | ||
| BAZEL_REMOTE_CACHE: https://storage.googleapis.com/envoy-circleci-bazel-cache/ | ||
| steps: | ||
| - run: rm -rf /home/circleci/project/.git # CircleCI git caching is likely broken | ||
| - checkout | ||
|
|
@@ -145,7 +141,6 @@ jobs: | |
| macos: | ||
| xcode: "9.3.0" | ||
| environment: | ||
| BAZEL_REMOTE_CACHE: https://storage.googleapis.com/envoy-circleci-bazel-cache/ | ||
| steps: | ||
| - run: sudo ntpdate -vu time.apple.com | ||
| - run: rm -rf /home/circleci/project/.git # CircleCI git caching is likely broken | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ import "gogoproto/gogo.proto"; | |
| // [#protodoc-title: HTTP connection manager] | ||
| // HTTP connection manager :ref:`configuration overview <config_http_conn_man>`. | ||
|
|
||
| // [#comment:next free field: 25] | ||
| // [#comment:next free field: 26] | ||
| message HttpConnectionManager { | ||
| enum CodecType { | ||
| option (gogoproto.goproto_enum_prefix) = false; | ||
|
|
@@ -175,6 +175,19 @@ message HttpConnectionManager { | |
| // option is not specified. | ||
| google.protobuf.Duration drain_timeout = 12 [(gogoproto.stdduration) = true]; | ||
|
|
||
| // The delayed close timeout is for downstream connections managed by the HTTP connection manager. | ||
|
Member
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. I'd like to see some documentation on why one would change this setting. It's talked about in the linked issue, but from just reading this documentation, I wouldn't know what could go wrong if I change this setting to 0/disabled here.
Contributor
Author
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. Done. |
||
| // It is defined as a grace period after connection close processing has been locally initiated | ||
| // during which Envoy will flush the write buffers for the connection and await the peer to close | ||
| // the connection (i.e., a TCP FIN/RST is received by Envoy from the downstream connection). | ||
| // | ||
| // If the timeout triggers, Envoy will close the connection's socket. | ||
| // | ||
| // The default timeout is 1000 ms if this option is not specified. | ||
| // | ||
| // A value of 0 will completely disable delayed close processing, and the downstream connection's | ||
| // socket will be closed immediately after the write flush is completed. | ||
| google.protobuf.Duration delayed_close_timeout = 25 [(gogoproto.stdduration) = true]; | ||
|
Contributor
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. Is there a default value? What happens if
Contributor
Author
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. There is a default timeout value of 1000 ms. I've amended the comment to note this. |
||
|
|
||
| // Configuration for :ref:`HTTP access logs <arch_overview_access_logs>` | ||
| // emitted by the connection manager. | ||
| repeated envoy.config.filter.accesslog.v2.AccessLog access_log = 13; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,7 +63,9 @@ class ConnectionCallbacks { | |
| */ | ||
| enum class ConnectionCloseType { | ||
| FlushWrite, // Flush pending write data before raising ConnectionEvent::LocalClose | ||
| NoFlush // Do not flush any pending data and immediately raise ConnectionEvent::LocalClose | ||
| NoFlush, // Do not flush any pending data and immediately raise ConnectionEvent::LocalClose | ||
| FlushWriteAndDelay // Flush pending write data and delay raising a ConnectionEvent::LocalClose | ||
| // until the delayed_close_timeout expires | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -86,6 +88,8 @@ class Connection : public Event::DeferredDeletable, public FilterManager { | |
| Stats::Gauge& write_current_; | ||
| // Counter* as this is an optional counter. Bind errors will not be tracked if this is nullptr. | ||
| Stats::Counter* bind_errors_; | ||
| // Optional counter. Delayed close timeouts will not be tracked if this is nullptr. | ||
| Stats::Counter* delayed_close_timeouts_; | ||
|
Member
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. Let's remove the reference to HTTP, since this isn't necessarily HTTP-specific. I think the comment can just mirror the one for bind_errors_.
Contributor
Author
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. Agreed the delayed close timeout is not inherently HTTP specific, but the intent of the comment is to point out that as currently implemented it only affects HTTP connections, since the H{1,2} codecs are the only callers of I'm fine removing this however if you don't think the note/clarification is required.
Member
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. My feeling is that it's just a comment we'll have to delete (or will be incorrect) when someone inevitably enables this for a different purpose.
Contributor
Author
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. Ok. Cleaned up the comment. |
||
| }; | ||
|
|
||
| virtual ~Connection() {} | ||
|
|
@@ -233,6 +237,17 @@ class Connection : public Event::DeferredDeletable, public FilterManager { | |
| * Get the socket options set on this connection. | ||
| */ | ||
| virtual const ConnectionSocket::OptionsSharedPtr& socketOptions() const PURE; | ||
|
|
||
| /** | ||
| * Set the timeout for delayed connection close()s. | ||
| * @param timeout The timeout value in milliseconds | ||
| */ | ||
| virtual void setDelayedCloseTimeout(std::chrono::milliseconds timeout) PURE; | ||
|
|
||
| /** | ||
| * @return std::chrono::milliseconds The delayed close timeout value. | ||
| */ | ||
| virtual std::chrono::milliseconds delayedCloseTimeout() const PURE; | ||
| }; | ||
|
|
||
| typedef std::unique_ptr<Connection> ConnectionPtr; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -450,8 +450,11 @@ void ConnectionImpl::onResetStreamBase(StreamResetReason reason) { | |
|
|
||
| ServerConnectionImpl::ServerConnectionImpl(Network::Connection& connection, | ||
| ServerConnectionCallbacks& callbacks, | ||
| Http1Settings settings) | ||
| : ConnectionImpl(connection, HTTP_REQUEST), callbacks_(callbacks), codec_settings_(settings) {} | ||
| Http1Settings settings, | ||
| std::chrono::milliseconds delayed_close_timeout) | ||
| : ConnectionImpl(connection, HTTP_REQUEST), callbacks_(callbacks), codec_settings_(settings) { | ||
| connection_.setDelayedCloseTimeout(delayed_close_timeout); | ||
|
Member
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. What's the reasoning for doing this set in the codec? It seems like something that is unrelated to the codec itself and should be done by higher layer code? Same question for the HTTP/2 codec below.
Contributor
Author
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. It's not related to the codec directly but this particular delayed_close_timeout is HTTP only since it is managed via the HCM configuration. This seemed to be the most appropriate place to set the timeout for HTTP connections just after they are constructed. Since the timeout value is intended to be network filter specific, I wouldn't expect any layer higher than the corresponding filter to issue the call to setDelayedCloseTimeout().
Member
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. I agree it should be done in the filter. Why not do it in the HCM filter and avoid codec modifications?
Contributor
Author
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. Agreed it's best to contain it within the HCM. I revisited the code and moved setting the timeout to the HCM config. |
||
| } | ||
|
|
||
| void ServerConnectionImpl::onEncodeComplete() { | ||
| ASSERT(active_request_); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,11 @@ ConnectionImpl::ConnectionImpl(Event::Dispatcher& dispatcher, ConnectionSocketPt | |
| ConnectionImpl::~ConnectionImpl() { | ||
| ASSERT(fd() == -1, "ConnectionImpl was unexpectedly torn down without being closed."); | ||
|
|
||
| if (delayed_close_timer_) { | ||
| // It's ok to disable even if the timer has already fired. | ||
| delayed_close_timer_->disableTimer(); | ||
| } | ||
|
|
||
| // In general we assume that owning code has called close() previously to the destructor being | ||
| // run. This generally must be done so that callbacks run in the correct context (vs. deferred | ||
| // deletion). Hence the assert above. However, call close() here just to be completely sure that | ||
|
|
@@ -106,10 +111,42 @@ void ConnectionImpl::close(ConnectionCloseType type) { | |
|
|
||
| closeSocket(ConnectionEvent::LocalClose); | ||
| } else { | ||
| // TODO(mattklein123): We need a flush timer here. We might never get open socket window. | ||
| ASSERT(type == ConnectionCloseType::FlushWrite); | ||
| close_with_flush_ = true; | ||
| ASSERT(type == ConnectionCloseType::FlushWrite || | ||
| type == ConnectionCloseType::FlushWriteAndDelay); | ||
|
|
||
| // No need to continue if a FlushWrite/FlushWriteAndDelay has already been issued and there is a | ||
| // pending delayed close. | ||
| if (delayed_close_) { | ||
|
Member
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. Does this happen in practice? Mostly just wondering about the flow that causes this. Perhaps flesh out the comment with some examples of cases where this happens?
Contributor
Author
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. Yes, I initially tried an ASSERT() which triggered in multiple tests during a //test/... run. I just tracked down a failure during an integration_test and added it to the comment. Based on that example and code inspection, this conditional can evaluate true under fairly standard use cases such as when Envoy initiates the close() sequence during codec processing. |
||
| return; | ||
| } | ||
|
|
||
| delayed_close_ = true; | ||
| bool delayed_close_timeout_set = delayedCloseTimeout().count() > 0; | ||
|
Member
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. nit: const
Contributor
Author
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. Done. |
||
|
|
||
| // NOTE: the delayed close timeout (if set) affects both FlushWrite and FlushWriteAndDelay | ||
| // closes: | ||
| // 1. For FlushWrite, the timeout sets an upper bound on how long to wait for the flush to | ||
| // complete before the connection is locally closed. | ||
| // 2. For FlushWriteAndDelay, the timeout specifies an upper bound on how long to wait for the | ||
| // flush to complete and the peer to close the connection before it is locally closed. | ||
|
|
||
| // All close types that follow do not actually close() the socket immediately so that buffered | ||
| // data can be written. However, we do want to stop reading to apply TCP backpressure. | ||
| read_enabled_ = false; | ||
|
|
||
| // Force a closeSocket() after the write buffer is flushed if the close_type calls for it or if | ||
| // no delayed close timeout is set. | ||
| close_after_flush_ = !delayed_close_timeout_set || type == ConnectionCloseType::FlushWrite; | ||
|
|
||
| // Create and activate a timer which will immediately close the connection if triggered. | ||
| // A config value of 0 disables the timeout. | ||
| if (delayed_close_timeout_set) { | ||
| delayed_close_timer_ = dispatcher_.createTimer([this]() -> void { onDelayedCloseTimeout(); }); | ||
| ENVOY_CONN_LOG(debug, "setting delayed close timer with timeout {} ms", *this, | ||
| delayedCloseTimeout().count()); | ||
| delayed_close_timer_->enableTimer(delayedCloseTimeout()); | ||
| } | ||
|
zuercher marked this conversation as resolved.
|
||
|
|
||
| file_event_->setEnabled(Event::FileReadyType::Write | | ||
| (enable_half_close_ ? 0 : Event::FileReadyType::Closed)); | ||
| } | ||
|
|
@@ -118,7 +155,7 @@ void ConnectionImpl::close(ConnectionCloseType type) { | |
| Connection::State ConnectionImpl::state() const { | ||
| if (fd() == -1) { | ||
| return State::Closed; | ||
| } else if (close_with_flush_) { | ||
| } else if (delayed_close_) { | ||
| return State::Closing; | ||
| } else { | ||
| return State::Open; | ||
|
|
@@ -488,7 +525,7 @@ void ConnectionImpl::onWriteReady() { | |
| // write callback. This can happen if we manage to complete the SSL handshake in the write | ||
| // callback, raise a connected event, and close the connection. | ||
| closeSocket(ConnectionEvent::RemoteClose); | ||
| } else if ((close_with_flush_ && new_buffer_size == 0) || bothSidesHalfClosed()) { | ||
| } else if ((close_after_flush_ && new_buffer_size == 0) || bothSidesHalfClosed()) { | ||
| ENVOY_CONN_LOG(debug, "write flush complete", *this); | ||
| closeSocket(ConnectionEvent::LocalClose); | ||
| } else if (result.action_ == PostIoAction::KeepOpen && result.bytes_processed_ > 0) { | ||
|
|
@@ -535,6 +572,14 @@ bool ConnectionImpl::bothSidesHalfClosed() { | |
| return read_end_stream_ && write_end_stream_ && write_buffer_->length() == 0; | ||
| } | ||
|
|
||
| void ConnectionImpl::onDelayedCloseTimeout() { | ||
| ENVOY_CONN_LOG(debug, "triggered delayed close", *this); | ||
| if (connection_stats_->delayed_close_timeouts_ != nullptr) { | ||
| connection_stats_->delayed_close_timeouts_->inc(); | ||
| } | ||
| closeSocket(ConnectionEvent::LocalClose); | ||
| } | ||
|
|
||
| ClientConnectionImpl::ClientConnectionImpl( | ||
| Event::Dispatcher& dispatcher, const Address::InstanceConstSharedPtr& remote_address, | ||
| const Network::Address::InstanceConstSharedPtr& source_address, | ||
|
|
||
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.
Let's plan to revert these changes before this gets merged. If we do disable this for all of Envoy, I'd like to do it in a separate PR and consult with maintainers. I think you can leave it in for now with this comment in place so senior maintainer gets a heads up.
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.
#4407