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

Better handle client connectivity state. #510

Merged
merged 2 commits into from
Jul 12, 2019
Merged

Conversation

glbrntt
Copy link
Collaborator

@glbrntt glbrntt commented Jul 10, 2019

Motivation:

Pull #506 highlighted some issues with client reconnectivity, namely
a race between an automatic reconnection and the client closing the
connection. In this case the delegate would report changes of state
after the terminal shutdown state.

Modifications:

  • State changes are now thread safe.
  • Record when a user initiates shutdown, ignore state changes beyond
    this point and close any channels created after this point.

Result:

More correct channel reconnectivity.

Motivation:

Pull grpc#506 highlighted some issues with client reconnectivity, namely
a race between an automatic reconnection and the client closing the
connection. In this case the delegate would report changes of state
after the terminal shutdown state.

Modifications:

- State changes are now thread safe.
- Record when a user initiates shutdown, ignore state changes beyond
  this point and close any channels created after this point.

Result:

More correct channel reconnectivity.
@glbrntt glbrntt closed this Jul 10, 2019
@glbrntt glbrntt reopened this Jul 10, 2019
Sources/GRPC/ClientConnection.swift Outdated Show resolved Hide resolved
/// - `makeBootstrap(configuration:)`, and
/// - `verifyTLS(channel:)`.
/// - Parameter channel: The channel that was set.
private func didSetChannel(to channel: EventLoopFuture<Channel>) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this logic need to be split into will and did set?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

willSet is preparing the channel so that it can be used, didSet is advertising that self.channel may be used (or not in the case of shutdown).

If we had the current didSet logic in willSet then we potentially have race problems when e.g. waiting on .ready (since it would be triggered before actually assigning the new value). If the willSet logic was in didSet then it would be possible to call self.channel and hit an error (closing the channel) before the reconnect callback was registered.

Sources/GRPC/ClientConnection.swift Outdated Show resolved Hide resolved
Sources/GRPC/ClientConnection.swift Outdated Show resolved Hide resolved
@MrMage MrMage merged commit 7c304ed into grpc:nio Jul 12, 2019
@glbrntt glbrntt deleted the connectivity-state branch July 12, 2019 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants