Skip to content

Commit 2c99017

Browse files
Use connection pooling when querying CosmosDB
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
1 parent e721610 commit 2c99017

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/key-value-azure/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ futures = { workspace = true }
1717
serde = { workspace = true }
1818
spin-core = { path = "../core" }
1919
spin-factor-key-value = { path = "../factor-key-value" }
20+
reqwest = { version = "0.12", default-features = false }
2021

2122
[lints]
2223
workspace = true
24+
25+
[features]
26+
# Enables reusing connections to the Azure Cosmos DB service.
27+
connection-pooling = []
28+
default = ["connection-pooling"]

crates/key-value-azure/src/store.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use anyhow::Result;
2-
use azure_data_cosmos::prelude::Operation;
1+
use anyhow::{Context, Result};
32
use azure_data_cosmos::{
4-
prelude::{AuthorizationToken, CollectionClient, CosmosClient, Query},
3+
prelude::{
4+
AuthorizationToken, CollectionClient, CosmosClient, CosmosClientBuilder, Operation, Query,
5+
},
56
CosmosEntity,
67
};
78
use futures::StreamExt;
@@ -88,14 +89,28 @@ impl KeyValueAzureCosmos {
8889
)
8990
}
9091
};
91-
let cosmos_client = CosmosClient::new(account, token);
92+
let cosmos_client = cosmos_client(account, token)?;
9293
let database_client = cosmos_client.database_client(database);
9394
let client = database_client.collection_client(container);
9495

9596
Ok(Self { client, app_id })
9697
}
9798
}
9899

100+
fn cosmos_client(account: impl Into<String>, token: AuthorizationToken) -> Result<CosmosClient> {
101+
if cfg!(feature = "connection-pooling") {
102+
let client = reqwest::ClientBuilder::new()
103+
.build()
104+
.context("failed to build reqwest client")?;
105+
let transport_options = azure_core::TransportOptions::new(std::sync::Arc::new(client));
106+
Ok(CosmosClientBuilder::new(account, token)
107+
.transport(transport_options)
108+
.build())
109+
} else {
110+
Ok(CosmosClient::new(account, token))
111+
}
112+
}
113+
99114
#[async_trait]
100115
impl StoreManager for KeyValueAzureCosmos {
101116
async fn get(&self, name: &str) -> Result<Arc<dyn Store>, Error> {

0 commit comments

Comments
 (0)