Skip to content

Commit 656e039

Browse files
insipxmchenani
andauthored
Error handling for rate limits (#1601)
* error rework * cooldown in retry strategy --------- Co-authored-by: Mojtaba Chenani <chenani@outlook.com>
1 parent f5b1089 commit 656e039

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3005
-1436
lines changed

.github/workflows/test-webassembly.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ on:
1717
env:
1818
CARGO_TERM_COLOR: always
1919
CARGO_INCREMENTAL: 0
20-
RUSTFLAGS: --cfg tracing_unstable
20+
RUSTFLAGS: --cfg tracing_unstable -Ctarget-feature=+bulk-memory,+mutable-globals,+atomics
2121
CARGO_PROFILE_TEST_DEBUG: 0
2222
WASM_BINDGEN_TEST_TIMEOUT: 480
2323
WASM_BINDGEN_TEST_ONLY_WEB: 1
@@ -41,7 +41,7 @@ jobs:
4141
run: cargo build --locked --tests --release --target wasm32-unknown-unknown -p xmtp_id -p xmtp_mls -p xmtp_api_http -p xmtp_cryptography -p xmtp_common
4242
- name: test with chrome
4343
run: |
44-
cargo test --locked --release --target wasm32-unknown-unknown -p xmtp_mls -p xmtp_id -p xmtp_api_http -p xmtp_cryptography -- \
44+
cargo test --locked --release --target wasm32-unknown-unknown -p xmtp_mls -p xmtp_id -p xmtp_api_http -p xmtp_cryptography -p xmtp_api -- \
4545
--skip xmtp_mls::storage::encrypted_store::group_message::tests::it_cannot_insert_message_without_group \
4646
--skip xmtp_mls::groups::tests::process_messages_abort_on_retryable_error \
4747
--skip xmtp_mls::storage::encrypted_store::group::tests::test_find_groups \

.vscode/settings.json

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"rust-analyzer.procMacro.attributes.enable": true,
1212
"rust-analyzer.procMacro.ignored": {
1313
"tracing": ["instrument"],
14-
"async-trait": ["async_trait"],
1514
"napi-derive": ["napi"],
1615
"async-recursion": ["async_recursion"],
1716
"ctor": ["ctor"],

Cargo.lock

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

Cargo.toml

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"xmtp_user_preferences",
1010
"xmtp_v2",
1111
"xmtp_mls",
12+
"xmtp_api",
1213
"xmtp_id",
1314
"bindings_wasm",
1415
"bindings_node",
@@ -51,13 +52,15 @@ ethers = { version = "2.0", default-features = false }
5152
fdlimit = "0.3"
5253
futures = { version = "0.3.30", default-features = false }
5354
getrandom = { version = "0.2", default-features = false }
55+
arc-swap = "1.7"
5456
gloo-timers = "0.3"
5557
hex = "0.4.3"
5658
hkdf = "0.12.3"
5759
js-sys = "0.3"
5860
libsqlite3-sys = { version = "0.29", features = [
5961
"bundled-sqlcipher-vendored-openssl",
6062
] }
63+
mockall = "0.13"
6164
once_cell = "1.2"
6265
openmls = { git = "https://github.com/xmtp/openmls", rev = "082cab5f17a54796e87bc1762a64496c86cb9bf8", default-features = false }
6366
openmls_basic_credential = { git = "https://github.com/xmtp/openmls", rev = "082cab5f17a54796e87bc1762a64496c86cb9bf8" }
@@ -81,33 +84,38 @@ sqlite-web = "0.0.1"
8184
thiserror = "2.0"
8285
tls_codec = "0.4.1"
8386
tokio = { version = "1.43.0", default-features = false }
87+
tokio-stream = { version = "0.1", default-features = false }
8488
uuid = "1.12"
8589
vergen-git2 = "1.0.2"
8690
web-time = "1.1"
8791
bytes = "1.9"
8892
pin-project-lite = "0.2"
8993
reqwest = { version = "0.12.12", features = ["json", "stream"] }
94+
itertools = "0.14"
9095
tonic = { version = "0.12", default-features = false }
9196
tracing = { version = "0.1", features = ["log"] }
9297
tracing-logfmt = "0.3"
9398
tracing-subscriber = { version = "0.3", default-features = false }
9499
trait-variant = "0.1.2"
95100
url = "2.5.0"
96-
wasm-bindgen = { version = "=0.2.100", features = ["enable-interning"] }
101+
wasm-bindgen = "=0.2.100"
97102
wasm-bindgen-futures = "0.4.50"
98103
wasm-bindgen-test = "0.3.50"
99104
web-sys = "0.3"
100105
zeroize = "1.8"
101106

102107
# Internal Crate Dependencies
108+
xmtp_api = { path = "xmtp_api" }
103109
xmtp_api_grpc = { path = "xmtp_api_grpc" }
110+
xmtp_api_http = { path = "xmtp_api_http" }
104111
xmtp_common = { path = "common" }
105112
xmtp_content_types = { path = "xmtp_content_types" }
106113
xmtp_cryptography = { path = "xmtp_cryptography" }
107114
xmtp_id = { path = "xmtp_id" }
108115
xmtp_mls = { path = "xmtp_mls" }
109116
xmtp_proto = { path = "xmtp_proto" }
110117

118+
111119
[profile.dev]
112120
# Disabling debug info speeds up builds a bunch,
113121
# and we don't rely on it for debugging that much.

bindings_ffi/Cargo.toml

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ tracing-subscriber = { workspace = true, features = [
2121
"json",
2222
] }
2323
uniffi = { version = "0.28.0", default-features = false, features = ["tokio"] }
24-
xmtp_api_grpc = { path = "../xmtp_api_grpc" }
24+
xmtp_api.workspace = true
25+
xmtp_api_grpc.workspace = true
2526
xmtp_common.workspace = true
26-
xmtp_content_types = { path = "../xmtp_content_types" }
27-
xmtp_cryptography = { path = "../xmtp_cryptography" }
28-
xmtp_id = { path = "../xmtp_id" }
29-
xmtp_mls = { path = "../xmtp_mls" }
27+
xmtp_content_types.workspace = true
28+
xmtp_cryptography.workspace = true
29+
xmtp_id.workspace = true
30+
xmtp_mls.workspace = true
3031
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full"] }
3132
xmtp_user_preferences = { path = "../xmtp_user_preferences" }
3233
xmtp_v2 = { path = "../xmtp_v2" }

bindings_ffi/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub enum GenericError {
2828
ClientBuilder(#[from] xmtp_mls::builder::ClientBuilderError),
2929
#[error("Storage error: {0}")]
3030
Storage(#[from] xmtp_mls::storage::StorageError),
31-
#[error("API error: {0}")]
32-
ApiError(#[from] xmtp_proto::Error),
3331
#[error("Group error: {0}")]
3432
GroupError(#[from] xmtp_mls::groups::GroupError),
3533
#[error("Signature: {0}")]
@@ -62,6 +60,10 @@ pub enum GenericError {
6260
IoError(#[from] tokio::io::Error),
6361
#[error(transparent)]
6462
Subscription(#[from] xmtp_mls::subscriptions::SubscribeError),
63+
#[error(transparent)]
64+
ApiClientBuild(#[from] xmtp_api_grpc::GrpcBuilderError),
65+
#[error(transparent)]
66+
Grpc(#[from] xmtp_api_grpc::GrpcError),
6567
}
6668

6769
#[derive(uniffi::Error, thiserror::Error, Debug)]

bindings_ffi/src/mls.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::{FfiSubscribeError, GenericError};
44
use prost::Message;
55
use std::{collections::HashMap, convert::TryInto, sync::Arc};
66
use tokio::sync::Mutex;
7+
use xmtp_api::{strategies, ApiClientWrapper};
78
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
9+
use xmtp_common::{AbortHandle, GenericStreamHandle, StreamHandle};
810
use xmtp_content_types::reaction::ReactionCodec;
911
use xmtp_content_types::ContentCodec;
1012
use xmtp_id::associations::{verify_signed_with_public_context, DeserializationError};
@@ -28,8 +30,6 @@ use xmtp_mls::storage::group::ConversationType;
2830
use xmtp_mls::storage::group_message::{ContentType, MsgQueryArgs};
2931
use xmtp_mls::storage::group_message::{SortDirection, StoredGroupMessageWithReactions};
3032
use xmtp_mls::{
31-
api::ApiClientWrapper,
32-
builder::ClientBuilder,
3333
client::Client as MlsClient,
3434
groups::{
3535
group_metadata::GroupMetadata,
@@ -51,8 +51,8 @@ use xmtp_mls::{
5151
EncryptedMessageStore, EncryptionKey, StorageOption,
5252
},
5353
subscriptions::SubscribeError,
54-
AbortHandle, GenericStreamHandle, StreamHandle,
5554
};
55+
use xmtp_proto::api_client::ApiBuilder;
5656
use xmtp_proto::xmtp::device_sync::BackupElementSelection;
5757
use xmtp_proto::xmtp::mls::message_contents::content_types::ReactionV2;
5858
use xmtp_proto::xmtp::mls::message_contents::{DeviceSyncKind, EncodedContent};
@@ -74,7 +74,11 @@ pub async fn connect_to_backend(
7474
host,
7575
is_secure
7676
);
77-
let api_client = TonicApiClient::create(host, is_secure).await?;
77+
let mut api_client = TonicApiClient::builder();
78+
api_client.set_host(host);
79+
api_client.set_tls(true);
80+
api_client.set_libxmtp_version(env!("CARGO_PKG_VERSION").into())?;
81+
let api_client = api_client.build().await?;
7882
Ok(Arc::new(XmtpApiClient(api_client)))
7983
}
8084

@@ -140,8 +144,9 @@ pub async fn create_client(
140144
legacy_signed_private_key_proto,
141145
);
142146

143-
let mut builder = ClientBuilder::new(identity_strategy)
147+
let mut builder = xmtp_mls::Client::builder(identity_strategy)
144148
.api_client(Arc::unwrap_or_clone(api).0)
149+
.with_remote_verifier()?
145150
.store(store);
146151

147152
if let Some(url) = &history_sync_url {
@@ -166,7 +171,8 @@ pub async fn get_inbox_id_for_address(
166171
api: Arc<XmtpApiClient>,
167172
account_address: String,
168173
) -> Result<Option<String>, GenericError> {
169-
let api = ApiClientWrapper::new(Arc::new(api.0.clone()), Default::default());
174+
let mut api =
175+
ApiClientWrapper::new(Arc::new(api.0.clone()), strategies::exponential_cooldown());
170176
let results = api
171177
.get_inbox_ids(vec![account_address.clone()])
172178
.await
@@ -2297,7 +2303,7 @@ impl FfiStreamCloser {
22972303

22982304
/// End the stream and asynchronously wait for it to shutdown
22992305
pub async fn end_and_wait(&self) -> Result<(), GenericError> {
2300-
use xmtp_mls::StreamHandleError::*;
2306+
use xmtp_common::StreamHandleError::*;
23012307
use GenericError::Generic;
23022308

23032309
if self.abort_handle.is_finished() {

bindings_ffi/src/v2.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ mod tests {
563563

564564
use futures::stream;
565565
use xmtp_proto::api_client::Envelope;
566-
use xmtp_proto::Error as ApiError;
567566

568567
use crate::{
569568
v2::{
@@ -610,7 +609,7 @@ mod tests {
610609

611610
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
612611
async fn test_subscribe() {
613-
let items: Vec<Result<Envelope, ApiError>> = vec![
612+
let items: Vec<Result<Envelope, xmtp_api_grpc::GrpcError>> = vec![
614613
Ok(Envelope {
615614
content_topic: "test1".to_string(),
616615
timestamp_ns: 0,
@@ -646,7 +645,7 @@ mod tests {
646645

647646
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
648647
async fn test_subscription_close() {
649-
let items: Vec<Result<Envelope, ApiError>> = vec![
648+
let items: Vec<Result<Envelope, xmtp_api_grpc::GrpcError>> = vec![
650649
Ok(Envelope {
651650
content_topic: "test1".to_string(),
652651
timestamp_ns: 0,

0 commit comments

Comments
 (0)