Skip to content

Commit 80ad00b

Browse files
goffrieConvex, Inc.
authored and
Convex, Inc.
committed
Make s3 client a singleton (#36383)
GitOrigin-RevId: 07aeecc43e46044eefecb328b31000ed1daeb12b
1 parent c7b52d9 commit 80ad00b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

crates/aws_s3/src/storage.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,7 @@ impl<RT: Runtime> S3Storage<RT> {
132132
key_prefix: String,
133133
runtime: RT,
134134
) -> anyhow::Result<Self> {
135-
let config = must_s3_config_from_env()
136-
.context("AWS env variables are required when using S3 storage")?
137-
.retry_config(RetryConfig::standard())
138-
.load()
139-
.await;
140-
let client = Client::new(&config);
135+
let client = s3_client().await?;
141136
let storage = Self {
142137
client,
143138
bucket,
@@ -157,6 +152,22 @@ impl<RT: Runtime> S3Storage<RT> {
157152
}
158153
}
159154

155+
async fn s3_client() -> Result<Client, anyhow::Error> {
156+
static S3_CLIENT: tokio::sync::OnceCell<Client> = tokio::sync::OnceCell::const_new();
157+
let client = S3_CLIENT
158+
.get_or_try_init(|| async {
159+
let config = must_s3_config_from_env()
160+
.context("AWS env variables are required when using S3 storage")?
161+
.retry_config(RetryConfig::standard())
162+
.load()
163+
.await;
164+
anyhow::Ok(Client::new(&config))
165+
})
166+
.await?
167+
.clone();
168+
Ok(client)
169+
}
170+
160171
struct ClientDrivenUpload {
161172
object_key: ObjectKey,
162173
upload_id: UploadId,

0 commit comments

Comments
 (0)