Skip to content

Commit ec45767

Browse files
authored
Avoid exposing Arced ResolveEndpoint in public API (#2758)
## Motivation and Context Hides `Arc<dyn ResolveEndpoint>` from public API. ## Description This PR replaces the occurrences of `Arc<dyn ResolveEndpoint>` with `SharedEndpointResolver` to not expose bare `Arc`s in the public API. ## Testing - [x] Passed tests in CI ## Checklist <!--- If a checkbox below is not applicable, then please DELETE it rather than leaving it unchecked --> - [x] 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._ --------- Co-authored-by: Yuki Saito <awsaito@amazon.com>
1 parent 5eb0927 commit ec45767

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

CHANGELOG.next.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,9 @@ filter_by_operation_id(plugin, |id| id.absolute() != "namespace#name");
162162
author = "82marbag"
163163
references = ["smithy-rs#2678"]
164164
meta = { "breaking" = true, "tada" = false, "bug" = false }
165+
166+
[[smithy-rs]]
167+
message = "The occurrences of `Arc<dyn ResolveEndpoint>` have now been replaced with `SharedEndpointResolver` in public APIs."
168+
references = ["smithy-rs#2758"]
169+
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client" }
170+
author = "ysaito1001"

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/customize/timestream/TimestreamDecorator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TimestreamDecorator : ClientCodegenDecorator {
4545
},
4646
)
4747
}
48+
4849
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
4950
val endpointDiscovery = InlineAwsDependency.forRustFile(
5051
"endpoint_discovery",
@@ -87,7 +88,7 @@ class TimestreamDecorator : ClientCodegenDecorator {
8788
time
8889
)
8990
.await?;
90-
new_conf.endpoint_resolver = ::std::sync::Arc::new(resolver);
91+
new_conf.endpoint_resolver = #{SharedEndpointResolver}::new(resolver);
9192
Ok((Self::from_conf(new_conf), reloader))
9293
}
9394
}
@@ -96,6 +97,8 @@ class TimestreamDecorator : ClientCodegenDecorator {
9697
"endpoint_discovery" to endpointDiscovery.toType(),
9798
"SystemTime" to RuntimeType.std.resolve("time::SystemTime"),
9899
"Duration" to RuntimeType.std.resolve("time::Duration"),
100+
"SharedEndpointResolver" to RuntimeType.smithyHttp(codegenContext.runtimeConfig)
101+
.resolve("endpoint::SharedEndpointResolver"),
99102
"SystemTimeSource" to RuntimeType.smithyAsync(codegenContext.runtimeConfig)
100103
.resolve("time::SystemTimeSource"),
101104
*Types(codegenContext.runtimeConfig).toArray(),

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/EndpointConfigCustomization.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.Writable
1313
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1414
import software.amazon.smithy.rust.codegen.core.rustlang.writable
1515
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
16+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.preludeScope
1617

1718
/**
1819
* Customization which injects an Endpoints 2.0 Endpoint Resolver into the service config struct
@@ -28,22 +29,25 @@ internal class EndpointConfigCustomization(
2829

2930
override fun section(section: ServiceConfig): Writable {
3031
return writable {
32+
val sharedEndpointResolver = "#{SharedEndpointResolver}<#{Params}>"
3133
val resolverTrait = "#{SmithyResolver}<#{Params}>"
3234
val codegenScope = arrayOf(
35+
*preludeScope,
36+
"SharedEndpointResolver" to types.sharedEndpointResolver,
3337
"SmithyResolver" to types.resolveEndpoint,
3438
"Params" to typesGenerator.paramsStruct(),
3539
)
3640
when (section) {
3741
is ServiceConfig.ConfigStruct -> rustTemplate(
38-
"pub (crate) endpoint_resolver: std::sync::Arc<dyn $resolverTrait>,",
42+
"pub (crate) endpoint_resolver: $sharedEndpointResolver,",
3943
*codegenScope,
4044
)
4145

4246
is ServiceConfig.ConfigImpl ->
4347
rustTemplate(
4448
"""
4549
/// Returns the endpoint resolver.
46-
pub fn endpoint_resolver(&self) -> std::sync::Arc<dyn $resolverTrait> {
50+
pub fn endpoint_resolver(&self) -> $sharedEndpointResolver {
4751
self.endpoint_resolver.clone()
4852
}
4953
""",
@@ -52,7 +56,7 @@ internal class EndpointConfigCustomization(
5256

5357
is ServiceConfig.BuilderStruct ->
5458
rustTemplate(
55-
"endpoint_resolver: Option<std::sync::Arc<dyn $resolverTrait>>,",
59+
"endpoint_resolver: #{Option}<$sharedEndpointResolver>,",
5660
*codegenScope,
5761
)
5862

@@ -99,15 +103,15 @@ internal class EndpointConfigCustomization(
99103
/// Sets the endpoint resolver to use when making requests.
100104
$defaultResolverDocs
101105
pub fn endpoint_resolver(mut self, endpoint_resolver: impl $resolverTrait + 'static) -> Self {
102-
self.endpoint_resolver = Some(std::sync::Arc::new(endpoint_resolver) as _);
106+
self.endpoint_resolver = #{Some}(#{SharedEndpointResolver}::new(endpoint_resolver));
103107
self
104108
}
105109
106110
/// Sets the endpoint resolver to use when making requests.
107111
///
108112
/// When unset, the client will used a generated endpoint resolver based on the endpoint resolution
109113
/// rules for `$moduleUseName`.
110-
pub fn set_endpoint_resolver(&mut self, endpoint_resolver: Option<std::sync::Arc<dyn $resolverTrait>>) -> &mut Self {
114+
pub fn set_endpoint_resolver(&mut self, endpoint_resolver: #{Option}<$sharedEndpointResolver>) -> &mut Self {
111115
self.endpoint_resolver = endpoint_resolver;
112116
self
113117
}
@@ -122,7 +126,7 @@ internal class EndpointConfigCustomization(
122126
rustTemplate(
123127
"""
124128
endpoint_resolver: self.endpoint_resolver.unwrap_or_else(||
125-
std::sync::Arc::new(#{DefaultResolver}::new())
129+
#{SharedEndpointResolver}::new(#{DefaultResolver}::new())
126130
),
127131
""",
128132
*codegenScope,
@@ -150,8 +154,9 @@ internal class EndpointConfigCustomization(
150154
// always fail. In the future, this will be changed to an `expect()`
151155
rustTemplate(
152156
"""
153-
endpoint_resolver: self.endpoint_resolver.unwrap_or_else(||std::sync::Arc::new(#{FailingResolver})),
157+
endpoint_resolver: self.endpoint_resolver.unwrap_or_else(||#{SharedEndpointResolver}::new(#{FailingResolver})),
154158
""",
159+
*codegenScope,
155160
"FailingResolver" to alwaysFailsResolver,
156161
)
157162
}

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/EndpointsDecorator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,14 @@ class EndpointsDecorator : ClientCodegenDecorator {
169169
val codegenScope = arrayOf(
170170
*RuntimeType.preludeScope,
171171
"Params" to typesGenerator.paramsStruct(),
172+
"ResolveEndpoint" to types.resolveEndpoint,
172173
"ResolveEndpointError" to types.resolveEndpointError,
173174
)
174175
return when (section) {
175176
is OperationSection.MutateInput -> writable {
176177
rustTemplate(
177178
"""
179+
use #{ResolveEndpoint};
178180
let params_result = #{Params}::builder()#{builderFields:W}.build()
179181
.map_err(|err| #{ResolveEndpointError}::from_source("could not construct endpoint parameters", err));
180182
let (endpoint_result, params) = match params_result {

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/endpoint/Util.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Types(runtimeConfig: RuntimeConfig) {
4848
private val smithyTypesEndpointModule = RuntimeType.smithyTypes(runtimeConfig).resolve("endpoint")
4949
val smithyHttpEndpointModule = RuntimeType.smithyHttp(runtimeConfig).resolve("endpoint")
5050
val resolveEndpoint = smithyHttpEndpointModule.resolve("ResolveEndpoint")
51+
val sharedEndpointResolver = smithyHttpEndpointModule.resolve("SharedEndpointResolver")
5152
val smithyEndpoint = smithyTypesEndpointModule.resolve("Endpoint")
5253
val resolveEndpointError = smithyHttpEndpointModule.resolve("ResolveEndpointError")
5354

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceRuntimePluginGenerator.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class ServiceRuntimePluginGenerator(
9797
"Params" to endpointTypesGenerator.paramsStruct(),
9898
"ResolveEndpoint" to http.resolve("endpoint::ResolveEndpoint"),
9999
"RuntimePlugin" to runtimeApi.resolve("client::runtime_plugin::RuntimePlugin"),
100-
"SharedEndpointResolver" to http.resolve("endpoint::SharedEndpointResolver"),
101100
"StaticAuthOptionResolver" to runtimeApi.resolve("client::auth::option_resolver::StaticAuthOptionResolver"),
102101
"default_connector" to client.resolve("conns::default_connector"),
103102
"require_connector" to client.resolve("conns::require_connector"),
@@ -134,7 +133,7 @@ class ServiceRuntimePluginGenerator(
134133
cfg.set_auth_option_resolver(#{StaticAuthOptionResolver}::new(#{Vec}::new()));
135134
136135
let endpoint_resolver = #{DefaultEndpointResolver}::<#{Params}>::new(
137-
#{SharedEndpointResolver}::from(self.handle.conf.endpoint_resolver()));
136+
self.handle.conf.endpoint_resolver());
138137
cfg.set_endpoint_resolver(endpoint_resolver);
139138
140139
// TODO(enableNewSmithyRuntime): Use the `store_append` method of ConfigBag to insert classifiers

0 commit comments

Comments
 (0)