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

Configure client connections with a builder #733

Merged
merged 2 commits into from
Mar 6, 2020

Conversation

glbrntt
Copy link
Collaborator

@glbrntt glbrntt commented Mar 3, 2020

Motivation:

Configuring a client without TLS is easy to do, since no tls is the
default in the client connection configuration. This is bad. The API
should make it clearer that the default is insecure.

The current API for configuring clients falls a bit short in a number of
areas. In no particular order:

  1. It can't be easily extended without SemVer major changes (since we'd
    have to provide a new init whenever new properties are added),
  2. Nested configuration (i.e. TLS) is unnecessarily verbose,
  3. The initializer requires properties to be passed in the correct
    order, and
  4. TLS is disabled by default (!) and the API doesn't make this clear that
    this is insecure

Modifications:

  • create a ClientConnectionBuilder whose public API is limited to
    configuring the private properties; this is similar to NIOs bootstraps
  • provide static methods on ClientConnection to provide a secure or
    insecure builder
  • update tests in a couple of places to demonstrate usage

Result:

  • Configuring a connection is less verbose
  • Not using TLS is more obviously 'insecure'

@glbrntt glbrntt added kind/enhancement Improvements to existing feature. nio 🆕 semver/minor Adds new public API. labels Mar 3, 2020
@glbrntt glbrntt requested a review from MrMage March 3, 2020 17:09
Motivation:

Configuring a client without TLS is easy to do, since no tls is the
default in the client connection configuration. This is bad. The API
should make it clearer that the default is insecure.

The current API for configuring clients falls a bit short in a number of
areas. In no particular order:

1. It can't be easily extended without SemVer major changes (since we'd
   have to provide a new init whenever new properties are added),
2. Nested configuration (i.e. TLS) is unnecessarily verbose,
3. The initializer requires properties to be passed in the correct
   order, and
4. TLS is disabled by default (!) and the API doesn't make this clear that
   this is insecure

Modifications:

- create a `ClientConnectionBuilder` whose public API is limited to
  configuring the private properties; this is similar to NIOs bootstraps
- provide static methods on `ClientConnection` to provide a `secure` or
  `insecure` builder
- update tests in a couple of places to demonstrate usage

Result:

- Configuring a connection is less verbose
- Not using TLS is more obviously 'insecure'
@glbrntt glbrntt force-pushed the gb-channel-builders branch from f5c3b3d to 0c29466 Compare March 4, 2020 10:34
Copy link
Contributor

@Davidde94 Davidde94 left a comment

Choose a reason for hiding this comment

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

Not sure if I should approve as I might not be authorised yet, but LGTM

@@ -101,6 +101,9 @@ public class ClientConnection {
}

/// Creates a new connection from the given configuration.
///
/// - Important: Users should prefer using `ClientConnection.secure(group:)` to build a connection
/// with TLS, or `ClientConnection.insecure(group:)` to build a connection without TLS.
public init(configuration: Configuration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still alpha right? You could make an API-breaking change to hide these initialisers and force people to use the new secure and insecure?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's right but it's impossible to provide fix-its for this and it's quite an annoying change to have to make. I'm leaning towards keeping it for now. The longer term plan would be to not add anything to it to gently force people to the builder.

Copy link

Choose a reason for hiding this comment

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

Please do not hide this initialiser, as this would disallow precise configuration by hands, which is often needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@be4rd what's your use case here? Everything should be representable by the builder as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

@glbrntt if you'd like to keep it another option to dissuade use would be to deprecate or provide a warning for direct use

Copy link

Choose a reason for hiding this comment

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

@glbrntt sorry, did not saw TLS configuration methods in builder when I left a comment 😅

/// multiplier to 1.0.
@discardableResult
public func withConnectionBackoff(fixed amount: TimeAmount) -> Self {
let seconds = Double(amount.nanoseconds) / Double(1_000_000_000)
Copy link
Contributor

Choose a reason for hiding this comment

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

Constant 1_000_000_000 is used a couple of times. Perhaps make attached it to Double to reduce potential error? (E.g. Double.Nanosecond, or just make it a constant somewhere)

@@ -101,6 +101,9 @@ public class ClientConnection {
}

/// Creates a new connection from the given configuration.
///
/// - Important: Users should prefer using `ClientConnection.secure(group:)` to build a connection
/// with TLS, or `ClientConnection.insecure(group:)` to build a connection without TLS.
public init(configuration: Configuration) {
Copy link

Choose a reason for hiding this comment

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

@glbrntt sorry, did not saw TLS configuration methods in builder when I left a comment 😅

@glbrntt glbrntt merged commit 86ea799 into grpc:nio Mar 6, 2020
@glbrntt glbrntt deleted the gb-channel-builders branch March 6, 2020 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements to existing feature. 🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants