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

Build services with a derived configuration object #3095

Merged
merged 10 commits into from
Oct 30, 2023

Conversation

david-perez
Copy link
Contributor

@david-perez david-perez commented Oct 25, 2023

This is a pared-down version of #2809.

This is the code-generated version of the
david-perez/smithy-rs-service-config#2 POC.

This introduces a code-generated PokemonServiceConfig object which is
provided to the service builder constructor via
PokemonService::builder(config: PokemonServiceConfig). This will displace the
current builder_without_plugins and builder_with_plugins, deprecating them.
We will eventually remove them.

The motivation is to have a single place where to register plugins and layers.
Smithy traits can leverage this object to inject methods that can apply plugins
and layers. For example, an @authorization trait implementer could vend a
smithy-rs decorator that pre-applied an authorization plugin inside the
${serviceName}Config object, possibly exposing a method to pass in any
runtime arguments needed to configure the middleware.

Checklist

  • I have updated CHANGELOG.next.toml if I made changes to the smithy-rs codegen or runtime crates

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

This is a pared-down version of #2809.

This is the code-generated version of the
david-perez/smithy-rs-service-config#2 POC.

This introduces a code-generated `Config` object which is provided to
the service builder constructor via `PokemonService::builder(config:
Config)`. This will displace the current `builder_without_plugins` and
`builder_with_plugins`, deprecating them. We will eventually remove
them.

The motivation is to have a single place where to register plugins and
layers. Smithy traits can leverage this object to inject methods that
can apply plugins and layers. For example, an `@authorization` trait
implementer could vend a smithy-rs decorator that pre-applied an
authorization plugin inside the `Config` object, possibly exposing a
method to pass in any runtime arguments needed to configure the
middleware.
@david-perez david-perez added the server Rust server SDK label Oct 25, 2023
@david-perez david-perez mentioned this pull request Oct 25, 2023
9 tasks
@david-perez
Copy link
Contributor Author

Upgrade guidance discussion: #3096

@david-perez
Copy link
Contributor Author

Observations:

  • We will make the config builder fallible when we inject methods to configure pre-applied middleware that can fail.
  • Extensions traits on HttpPlugins/ModelPlugins are nice because they expose syntax sugar to push plugins. Said syntax sugar is not available in the config builder, because the config builder is code-generated.

@smithy-lang smithy-lang deleted a comment from github-actions bot Oct 25, 2023
@smithy-lang smithy-lang deleted a comment from github-actions bot Oct 25, 2023
@github-actions

This comment was marked as outdated.

@david-perez david-perez marked this pull request as ready for review October 25, 2023 17:42
@david-perez david-perez requested review from a team as code owners October 25, 2023 17:42
@david-perez
Copy link
Contributor Author

david-perez commented Oct 26, 2023

We will make the config builder fallible when we inject methods to configure pre-applied middleware that can fail.

Actually, I'll make the config builder always fallible to enforce that users register layers -> HTTP plugins -> model plugins in that order. When an injected method to configure pre-applied middleware is used, that method will return a Result.


There was also an idea by @hlbarber on how to enforce plugin dependencies at runtime we should follow-up on: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=675785fd01852a155dced99d182f3f02

@david-perez
Copy link
Contributor Author

Actually, I'll make the config builder always fallible to enforce that users register layers -> HTTP plugins -> model plugins in that order.

Injected methods may apply a combination of layers and plugins, which makes it tricky to say when in the chain they should appear. I guess the best option is to make them appear as early as possible (e.g. if a method applies a HTTP plugin and a model plugin, it should be invoked after the layers and before the model plugins).

@github-actions

This comment was marked as outdated.

@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@@ -73,9 +73,9 @@ open class ServerRootGenerator(
//! ```rust,no_run
//! ## use std::net::SocketAddr;
//! ## async fn dummy() {
//! use $crateName::$serviceName;
//! use $crateName::{${serviceName}Config, $serviceName};
Copy link
Contributor

Choose a reason for hiding this comment

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

nit but shouldn't $serviceName go first?

}

Copy link
Contributor

Choose a reason for hiding this comment

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

Spaces

@david-perez david-perez added the breaking-change This will require a breaking change label Oct 30, 2023
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@david-perez david-perez added this pull request to the merge queue Oct 30, 2023
Merged via the queue into main with commit 53dcff3 Oct 30, 2023
40 of 41 checks passed
@david-perez david-perez deleted the davidpz/service-config-redux branch October 30, 2023 16:52
david-perez added a commit that referenced this pull request Oct 30, 2023
PR #3095 added a code-generated `${serviceName}Config` object on which
users can register layers and plugins. For example:

```rust
let config = PokemonServiceConfig::builder()
    .layer(layers)
    .http_plugin(authn_plugin)
    .model_plugin(authz_plugin)
    .build();
```

This PR makes it so that server decorators can inject methods on this
config builder object. These methods can apply arbitrary layers, HTTP
plugins, and/or model plugins. Moreover, the decorator can configure
whether invoking such method is required or not.

For example, a decorator can inject an `aws_auth` method that configures
some plugins using its input arguments. Missing invocation of this method
will result in the config failing to build:

```rust
let _: SimpleServiceConfig<
    // No layers have been applied.
    tower::layer::util::Identity,
    // One HTTP plugin has been applied.
    PluginStack<IdentityPlugin, IdentityPlugin>,
    // One model plugin has been applied.
    PluginStack<IdentityPlugin, IdentityPlugin>,
> = SimpleServiceConfig::builder()
    // This method has been injected in the config builder!
    .aws_auth("a".repeat(69).to_owned(), 69)
    // The method will apply one HTTP plugin and one model plugin,
    // configuring them with the input arguments. Configuration can be
    // declared to be fallible, in which case we get a `Result` we unwrap
    // here.
    .expect("failed to configure aws_auth")
    .build()
    // Since `aws_auth` has been marked as required, if the user misses
    // invoking it, this would panic here.
    .unwrap();
```
github-merge-queue bot pushed a commit that referenced this pull request Oct 31, 2023
PR #3095 added a code-generated `${serviceName}Config` object on which
users can register layers and plugins. For example:

```rust
let config = PokemonServiceConfig::builder()
    .layer(layers)
    .http_plugin(authn_plugin)
    .model_plugin(authz_plugin)
    .build();
```

This PR makes it so that server decorators can inject methods on this
config builder object. These methods can apply arbitrary layers, HTTP
plugins, and/or model plugins. Moreover, the decorator can configure
whether invoking such method is required or not.

For example, a decorator can inject an `aws_auth` method that configures
some plugins using its input arguments. Missing invocation of this
method
will result in the config failing to build:

```rust
let _: SimpleServiceConfig<
    // No layers have been applied.
    tower::layer::util::Identity,
    // One HTTP plugin has been applied.
    PluginStack<IdentityPlugin, IdentityPlugin>,
    // One model plugin has been applied.
    PluginStack<IdentityPlugin, IdentityPlugin>,
> = SimpleServiceConfig::builder()
    // This method has been injected in the config builder!
    .aws_auth("a".repeat(69).to_owned(), 69)
    // The method will apply one HTTP plugin and one model plugin,
    // configuring them with the input arguments. Configuration can be
    // declared to be fallible, in which case we get a `Result` we unwrap
    // here.
    .expect("failed to configure aws_auth")
    .build()
    // Since `aws_auth` has been marked as required, if the user misses
    // invoking it, this would panic here.
    .unwrap();
```

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
rcoh pushed a commit that referenced this pull request Nov 1, 2023
PR #3095 added a code-generated `${serviceName}Config` object on which
users can register layers and plugins. For example:

```rust
let config = PokemonServiceConfig::builder()
    .layer(layers)
    .http_plugin(authn_plugin)
    .model_plugin(authz_plugin)
    .build();
```

This PR makes it so that server decorators can inject methods on this
config builder object. These methods can apply arbitrary layers, HTTP
plugins, and/or model plugins. Moreover, the decorator can configure
whether invoking such method is required or not.

For example, a decorator can inject an `aws_auth` method that configures
some plugins using its input arguments. Missing invocation of this
method
will result in the config failing to build:

```rust
let _: SimpleServiceConfig<
    // No layers have been applied.
    tower::layer::util::Identity,
    // One HTTP plugin has been applied.
    PluginStack<IdentityPlugin, IdentityPlugin>,
    // One model plugin has been applied.
    PluginStack<IdentityPlugin, IdentityPlugin>,
> = SimpleServiceConfig::builder()
    // This method has been injected in the config builder!
    .aws_auth("a".repeat(69).to_owned(), 69)
    // The method will apply one HTTP plugin and one model plugin,
    // configuring them with the input arguments. Configuration can be
    // declared to be fallible, in which case we get a `Result` we unwrap
    // here.
    .expect("failed to configure aws_auth")
    .build()
    // Since `aws_auth` has been marked as required, if the user misses
    // invoking it, this would panic here.
    .unwrap();
```

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
david-perez added a commit that referenced this pull request Nov 14, 2023
This was accidentally removed in #3095.

Fixes #3177.
david-perez added a commit that referenced this pull request Nov 14, 2023
This was accidentally removed in #3095.

Fixes #3177.
david-perez added a commit that referenced this pull request Nov 14, 2023
This was accidentally removed in #3095.

Fixes #3177.
david-perez added a commit that referenced this pull request Nov 14, 2023
This was accidentally removed in #3095.

Fixes #3177.
github-merge-queue bot pushed a commit that referenced this pull request Nov 14, 2023
This was accidentally removed in #3095.

Fixes #3177.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This will require a breaking change server Rust server SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants