Skip to content

Commit

Permalink
fix: use tokio mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Oct 11, 2024
1 parent 036a96d commit a32543a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
4 changes: 1 addition & 3 deletions packages/relayer/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ impl ChainClient {
dkim: ECDSAOwnedDKIMRegistry<SignerM>,
) -> Result<String> {
// Mutex is used to prevent nonce conflicts.
let mut mutex = SHARED_MUTEX
.lock()
.map_err(|e| anyhow::anyhow!("Mutex poisoned: {}", e))?;
let mut mutex = SHARED_MUTEX.lock().await;
*mutex += 1;

// Call the contract method
Expand Down
28 changes: 5 additions & 23 deletions packages/relayer/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::Arc;

use axum::{
body::Body,
extract::State,
http::{request, StatusCode},
response::IntoResponse,
Expand Down Expand Up @@ -87,33 +86,16 @@ pub async fn submit_handler(

pub async fn receive_email_handler(
State(relayer_state): State<Arc<RelayerState>>,
body: axum::body::Body,
body: String,
) -> Result<impl IntoResponse, (StatusCode, Json<Value>)> {
// Convert the body into a string
let bytes = axum::body::to_bytes(body, usize::MAX)
.await
.map_err(|err| {
(
StatusCode::BAD_REQUEST,
axum::Json(json!({"error": format!("Failed to read request body: {}", err)})),
)
})?;

let email = String::from_utf8(bytes.to_vec()).map_err(|err| {
(
StatusCode::BAD_REQUEST,
axum::Json(json!({"error": format!("Invalid UTF-8 sequence: {}", err)})),
)
})?;

// Define the regex pattern for UUID
let uuid_regex = Regex::new(
r"(Your request ID is )([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})",
)
.unwrap();

// Attempt to find a UUID in the body
let captures = uuid_regex.captures(&email);
let captures = uuid_regex.captures(&body);

let request_id = captures
.and_then(|caps| caps.get(2).map(|m| m.as_str()))
Expand Down Expand Up @@ -149,9 +131,9 @@ pub async fn receive_email_handler(
})?;

// Log the received body
info!(LOG, "Received email: {:?}", email);
info!(LOG, "Received email body: {:?}", body);

let parsed_email = ParsedEmail::new_from_raw_email(&email).await.map_err(|e| {
let parsed_email = ParsedEmail::new_from_raw_email(&body).await.map_err(|e| {
// Convert the error to the expected type
(
reqwest::StatusCode::INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -208,7 +190,7 @@ pub async fn receive_email_handler(
})?;

// Process the email
match handle_email(email, request, (*relayer_state).clone()).await {
match handle_email(body, request, (*relayer_state).clone()).await {
Ok(event) => match handle_email_event(event, (*relayer_state).clone()).await {
Ok(_) => {}
Err(e) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/relayer/src/statics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use tokio::sync::Mutex;

use lazy_static::lazy_static;

Expand Down

0 comments on commit a32543a

Please sign in to comment.