Skip to content
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

Fix connection timeout when retries are set to none #1650

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/GRPC/ConnectionBackoff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public struct ConnectionBackoff: Sequence, Sendable {

/// An iterator for ``ConnectionBackoff``.
public class ConnectionBackoffIterator: IteratorProtocol {
public typealias Element = (timeout: TimeInterval, backoff: TimeInterval)
public typealias Element = (timeout: TimeInterval, backoff: TimeInterval?)

/// Creates a new connection backoff iterator with the given configuration.
public init(connectionBackoff: ConnectionBackoff) {
Expand Down Expand Up @@ -135,7 +135,12 @@ public class ConnectionBackoffIterator: IteratorProtocol {
case let .limited(limit) where limit > 0:
self.connectionBackoff.retries.limit = .limited(limit - 1)

// limit must be <= 0, no new element.
// limit is reached, return an element with only a timeout.
case let .limited(limit) where limit == 0:
self.connectionBackoff.retries.limit = .limited(limit - 1)
return self.makeElement(backoff: nil)
Comment on lines +138 to +141
Copy link
Author

@afeick afeick Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glbrntt What do you think about using a nil Element.backoff as the sentinel for the iterator?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It changes the Element type which is an API breaking change so we can't do it unfortunately. Otherwise it'd be a good solution.


// limit must be < 0, no new element.
case .limited:
return nil
}
Expand All @@ -159,8 +164,8 @@ public class ConnectionBackoffIterator: IteratorProtocol {

/// Make a timeout-backoff pair from the given backoff. The timeout is the `max` of the backoff
/// and `connectionBackoff.minimumConnectionTimeout`.
private func makeElement(backoff: TimeInterval) -> Element {
let timeout = max(backoff, self.connectionBackoff.minimumConnectionTimeout)
private func makeElement(backoff: TimeInterval?) -> Element {
let timeout = max(backoff ?? self.unjitteredBackoff, self.connectionBackoff.minimumConnectionTimeout)
return (timeout, backoff)
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/GRPC/ConnectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,10 @@ extension ConnectionManager {
}

// Should we reconnect if the candidate channel fails?
let reconnect: Reconnect = timeoutAndBackoff.map { .after($0.backoff) } ?? .none
var reconnect = Reconnect.none
if let backoff = timeoutAndBackoff?.backoff {
reconnect = Reconnect.after(backoff)
}
let connecting = ConnectingState(
backoffIterator: backoffIterator,
reconnect: reconnect,
Expand Down