Skip to content
Open
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
45 changes: 26 additions & 19 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ pub async fn handle_request(
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());

// Check if any mock needs body matching
let needs_body_matching = state
.mocks
.values()
.flatten()
.any(|mock| mock.body.is_some());
// Check if any mock needs body matching (acquire read lock)
let needs_body_matching = {
let mocks = state.mocks.read().await;
mocks.values().flatten().any(|mock| mock.body.is_some())
};

// Consume body if needed for matching or if consume_body is set
let body_bytes: Option<Bytes> =
Expand Down Expand Up @@ -93,14 +92,18 @@ pub async fn handle_request(
content_type,
};

// Find matching mock using the new matcher
match find_matching_mock(&context, &state.mocks) {
// Find matching mock using the matcher (acquire read lock)
let mocks = state.mocks.read().await;
match find_matching_mock(&context, &mocks) {
Some(mock) => {
info!("Mock matched: {} {} -> {}", method_str, path, mock.status);

let status = StatusCode::from_u16(mock.status).unwrap_or(StatusCode::OK);
let matched_key = format!("{}:{}", method_str, path);

// Release read lock before recording to avoid holding it during async I/O
drop(mocks);

// Record the request
record_request(&state, context, Some(matched_key), mock.status).await;

Expand All @@ -110,6 +113,9 @@ pub async fn handle_request(
None => {
info!("No mock found for: {} {}", method_str, path);

// Release read lock before recording to avoid holding it during async I/O
drop(mocks);

// Record the request (clone query_params for use in error response)
let query_params_clone = context.query_params.clone();
record_request(&state, context, None, 404).await;
Expand All @@ -131,9 +137,10 @@ pub async fn handle_request(
}

pub async fn health_check(State(state): State<AppState>) -> Json<serde_json::Value> {
let mocks = state.mocks.read().await;
Json(json!({
"status": "healthy",
"mocks_loaded": state.mocks.len(),
"mocks_loaded": mocks.len(),
"service": "mimic"
}))
}
Expand Down Expand Up @@ -288,15 +295,15 @@ mod tests {
);

AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
}
}

fn create_empty_state() -> AppState {
AppState {
mocks: Arc::new(HashMap::new()),
mocks: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
}
Expand Down Expand Up @@ -459,7 +466,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -503,7 +510,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -557,7 +564,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -609,7 +616,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -662,7 +669,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -706,7 +713,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -767,7 +774,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down Expand Up @@ -835,7 +842,7 @@ mod tests {
);

let state = AppState {
mocks: Arc::new(mocks),
mocks: Arc::new(tokio::sync::RwLock::new(mocks)),
request_log: Arc::new(tokio::sync::RwLock::new(Vec::new())),
request_counter: Arc::new(AtomicU64::new(0)),
};
Expand Down
Loading