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

fix: implement relay websocket connection state #128

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.

- - -
## v0.15.1 - 2023-10-17
Copy link
Member

@chris13524 chris13524 Oct 17, 2023

Choose a reason for hiding this comment

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

Once my PR is merged can you reset this CHANGELOG, revert the version in Cargo.toml, and delete any created GitHub releases before merging?

#### Bug Fixes
- relay websocket connection state implementation - (a0bf2f2) - Max Kalashnikoff

- - -

## v0.14.2 - 2023-10-17
#### Bug Fixes
- Terraform workspace in CI - (2d43c97) - Xavier Basty
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "notify-server"
version = "0.14.1"
version = "0.15.1"
edition = "2021"
authors = ["Maciej Rak <[email protected]>"]
build = "build.rs"
Expand Down
29 changes: 17 additions & 12 deletions src/websocket_service/handlers/notify_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
websocket_service::{
decode_key,
handlers::{decrypt_message, notify_watch_subscriptions::update_subscription_watchers},
NotifyDelete, NotifyRequest, NotifyResponse,
publish_message, NotifyDelete, NotifyRequest, NotifyResponse, WebSocketClientState,
},
Result,
},
Expand All @@ -22,7 +22,7 @@ use {
mongodb::bson::doc,
relay_rpc::domain::DecodedClientId,
serde_json::{json, Value},
std::sync::Arc,
std::sync::{Arc, Mutex},
tracing::{info, warn},
};

Expand All @@ -31,6 +31,7 @@ pub async fn handle(
msg: relay_client::websocket::PublishedMessage,
state: &Arc<AppState>,
client: &Arc<relay_client::websocket::Client>,
client_state: &Arc<Mutex<WebSocketClientState>>,
) -> Result<()> {
let request_id = uuid::Uuid::new_v4();
let topic = msg.topic;
Expand Down Expand Up @@ -160,21 +161,25 @@ pub async fn handle(

let response_topic = sha256::digest(&sym_key);

client
.publish(
response_topic.into(),
base64_notification,
NOTIFY_DELETE_RESPONSE_TAG,
NOTIFY_DELETE_RESPONSE_TTL,
false,
)
.await?;
publish_message(
client.clone(),
client_state.clone(),
state.clone(),
response_topic.into(),
&base64_notification,
NOTIFY_DELETE_RESPONSE_TAG,
NOTIFY_DELETE_RESPONSE_TTL,
false,
)
.await?;

update_subscription_watchers(
&account,
&project_data.app_domain,
&state.database,
client.as_ref(),
client,
client_state,
state,
&state.notify_keys.authentication_secret,
&state.notify_keys.authentication_public,
)
Expand Down
52 changes: 31 additions & 21 deletions src/websocket_service/handlers/notify_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
websocket_service::{
decode_key, derive_key,
handlers::{decrypt_message, notify_watch_subscriptions::update_subscription_watchers},
NotifyRequest, NotifyResponse, NotifySubscribe,
publish_message, NotifyRequest, NotifyResponse, NotifySubscribe, WebSocketClientState,
},
Result,
},
Expand All @@ -21,7 +21,10 @@ use {
mongodb::bson::doc,
relay_rpc::domain::DecodedClientId,
serde_json::{json, Value},
std::{sync::Arc, time::Duration},
std::{
sync::{Arc, Mutex},
time::Duration,
},
tracing::{info, instrument},
x25519_dalek::StaticSecret,
};
Expand All @@ -32,6 +35,7 @@ pub async fn handle(
msg: relay_client::websocket::PublishedMessage,
state: &Arc<AppState>,
client: &Arc<relay_client::websocket::Client>,
client_state: &Arc<Mutex<WebSocketClientState>>,
) -> Result<()> {
let topic = msg.topic.to_string();

Expand Down Expand Up @@ -152,32 +156,38 @@ pub async fn handle(

// Send noop to extend ttl of relay's mapping
info!("publishing noop to notify_topic: {notify_topic}");
client
.publish(
notify_topic.clone().into(),
"",
4050,
Duration::from_secs(300),
false,
)
.await?;
publish_message(
client.clone(),
client_state.clone(),
state.clone(),
notify_topic.clone().into(),
"",
4050,
Duration::from_secs(300),
false,
)
.await?;

info!("publishing subscribe response to topic: {response_topic}");
client
.publish(
response_topic.into(),
base64_notification,
NOTIFY_SUBSCRIBE_RESPONSE_TAG,
NOTIFY_SUBSCRIBE_RESPONSE_TTL,
false,
)
.await?;
publish_message(
client.clone(),
client_state.clone(),
state.clone(),
response_topic.into(),
&base64_notification,
NOTIFY_SUBSCRIBE_RESPONSE_TAG,
NOTIFY_SUBSCRIBE_RESPONSE_TTL,
false,
)
.await?;

update_subscription_watchers(
&account,
&project_data.app_domain,
&state.database,
client.as_ref(),
client,
client_state,
state,
&state.notify_keys.authentication_secret,
&state.notify_keys.authentication_public,
)
Expand Down
30 changes: 18 additions & 12 deletions src/websocket_service/handlers/notify_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use {
state::AppState,
types::{ClientData, Envelope, EnvelopeType0, LookupEntry},
websocket_service::{
decode_key, handlers::decrypt_message, NotifyRequest, NotifyResponse, NotifyUpdate,
decode_key, handlers::decrypt_message, publish_message, NotifyRequest, NotifyResponse,
NotifyUpdate, WebSocketClientState,
},
Result,
},
Expand All @@ -20,14 +21,15 @@ use {
mongodb::bson::doc,
relay_rpc::domain::DecodedClientId,
serde_json::{json, Value},
std::sync::Arc,
std::sync::{Arc, Mutex},
};

// TODO test idempotency
pub async fn handle(
msg: relay_client::websocket::PublishedMessage,
state: &Arc<AppState>,
client: &Arc<relay_client::websocket::Client>,
client_state: &Arc<Mutex<WebSocketClientState>>,
) -> Result<()> {
let _request_id = uuid::Uuid::new_v4();
let topic = msg.topic.to_string();
Expand Down Expand Up @@ -142,21 +144,25 @@ pub async fn handle(

let response_topic = sha256::digest(&sym_key);

client
.publish(
response_topic.into(),
base64_notification,
NOTIFY_UPDATE_RESPONSE_TAG,
NOTIFY_UPDATE_RESPONSE_TTL,
false,
)
.await?;
publish_message(
client.clone(),
client_state.clone(),
state.clone(),
response_topic.into(),
&base64_notification,
NOTIFY_UPDATE_RESPONSE_TAG,
NOTIFY_UPDATE_RESPONSE_TTL,
false,
)
.await?;

update_subscription_watchers(
&account,
&project_data.app_domain,
&state.database,
client.as_ref(),
client,
client_state,
state,
&state.notify_keys.authentication_secret,
&state.notify_keys.authentication_public,
)
Expand Down
59 changes: 36 additions & 23 deletions src/websocket_service/handlers/notify_watch_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
WatchSubscriptionsEntry,
},
websocket_service::{
decode_key, derive_key, handlers::decrypt_message, NotifyRequest, NotifyResponse,
NotifyWatchSubscriptions,
decode_key, derive_key, handlers::decrypt_message, publish_message, NotifyRequest,
NotifyResponse, NotifyWatchSubscriptions, WebSocketClientState,
},
Result,
},
Expand All @@ -29,7 +29,7 @@
mongodb::{bson::doc, Database},
relay_rpc::domain::{DecodedClientId, Topic},
serde_json::{json, Value},
std::sync::Arc,
std::sync::{Arc, Mutex},
tracing::{info, instrument},
};

Expand All @@ -38,6 +38,7 @@
msg: relay_client::websocket::PublishedMessage,
state: &Arc<AppState>,
client: &Arc<relay_client::websocket::Client>,
client_state: &Arc<Mutex<WebSocketClientState>>,
) -> Result<()> {
if msg.topic != state.notify_keys.key_agreement_topic {
return Err(Error::WrongNotifyWatchSubscriptionsTopic(msg.topic));
Expand Down Expand Up @@ -153,15 +154,17 @@
base64::engine::general_purpose::STANDARD.encode(envelope.to_bytes());

info!("Publishing response on topic {response_topic}");
client
.publish(
response_topic.into(),
base64_notification,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TTL,
false,
)
.await?;
publish_message(
client.clone(),
client_state.clone(),
state.clone(),
response_topic.into(),
&base64_notification,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TTL,
false,
)
.await?;
}

Ok(())
Expand Down Expand Up @@ -226,12 +229,14 @@
}

// TODO do async outside of websocket request handler
#[instrument(skip_all, fields(account = %account, app_domain = %app_domain))]

Check failure on line 232 in src/websocket_service/handlers/notify_watch_subscriptions.rs

View workflow job for this annotation

GitHub Actions / CI / App CI / Clippy

this function has too many arguments (8/7)
pub async fn update_subscription_watchers(
account: &str,
app_domain: &str,
database: &Database,
client: &relay_client::websocket::Client,
client: &Arc<relay_client::websocket::Client>,
client_state: &Arc<Mutex<WebSocketClientState>>,
app_state: &Arc<AppState>,
authentication_secret: &ed25519_dalek::SigningKey,
authentication_public: &ed25519_dalek::VerifyingKey,
) -> Result<()> {
Expand All @@ -242,14 +247,16 @@

let did_pkh = format!("did:pkh:{account}");

#[instrument(skip_all, fields(did_pkh = %did_pkh, aud = %aud, subscriptions_count = %subscriptions.len()))]

Check failure on line 250 in src/websocket_service/handlers/notify_watch_subscriptions.rs

View workflow job for this annotation

GitHub Actions / CI / App CI / Clippy

this function has too many arguments (9/7)
async fn send(
subscriptions: Vec<NotifyServerSubscription>,
aud: String,
sym_key: &str,
notify_did_key: String,
did_pkh: String,
client: &relay_client::websocket::Client,
client: &Arc<relay_client::websocket::Client>,
client_state: &Arc<Mutex<WebSocketClientState>>,
app_state: &Arc<AppState>,
authentication_secret: &ed25519_dalek::SigningKey,
) -> Result<()> {
let now = Utc::now();
Expand Down Expand Up @@ -277,15 +284,17 @@

let topic = Topic::from(sha256::digest(&sym_key));
info!("topic: {topic}");
client
.publish(
topic,
base64_notification,
NOTIFY_SUBSCRIPTIONS_CHANGED_TAG,
NOTIFY_SUBSCRIPTIONS_CHANGED_TTL,
false,
)
.await?;
publish_message(
client.clone(),
client_state.clone(),
app_state.clone(),
topic,
&base64_notification,
NOTIFY_SUBSCRIPTIONS_CHANGED_TAG,
NOTIFY_SUBSCRIPTIONS_CHANGED_TTL,
false,
)
.await?;

Ok(())
}
Expand All @@ -308,6 +317,8 @@
notify_did_key.clone(),
did_pkh.clone(),
client,
client_state,
app_state,
authentication_secret,
)
.await?;
Expand All @@ -331,6 +342,8 @@
notify_did_key.clone(),
did_pkh.clone(),
client,
client_state,
app_state,
Comment on lines 344 to +346
Copy link
Member

Choose a reason for hiding this comment

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

This is a lot to have to pass around. Can we wrap all 3 of these in a struct (maybe called RobustRelayClient) and put publish_message(&self) in its implementation?

authentication_secret,
)
.await?;
Expand Down
Loading
Loading