Skip to content

Commit

Permalink
feat(webhooks): track only as much responder requests as allowed by t…
Browse files Browse the repository at this point in the history
…he user subscription
  • Loading branch information
azasypkin committed Mar 25, 2024
1 parent 72cfd03 commit c32b141
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
22 changes: 15 additions & 7 deletions src/server/handlers/webhooks_responders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,18 @@ pub async fn webhooks_responders(
}
};

// Record request
if responder.settings.requests_to_track > 0 {
let subscription_config = user
.subscription
.get_features(&state.config)
.config
.webhooks;

// Record request, but don't track more requests than allowed by the subscription.
let requests_to_track = std::cmp::min(
responder.settings.requests_to_track,
subscription_config.responder_requests,
);
if requests_to_track > 0 {
let headers = request
.headers()
.iter()
Expand Down Expand Up @@ -173,6 +183,7 @@ pub async fn webhooks_responders(
} else {
Some(Cow::Borrowed(&payload))
},
requests_to_track,
},
)
.await?;
Expand Down Expand Up @@ -203,12 +214,9 @@ pub async fn webhooks_responders(
};

// Configure JavaScript runtime based on user's subscription level/overrides.
let features = user.subscription.get_features(&state.config);
let js_runtime_config = JsRuntimeConfig {
max_heap_size: features.config.webhooks.js_runtime_heap_size,
max_user_script_execution_time: features
.config
.webhooks
max_heap_size: subscription_config.js_runtime_heap_size,
max_user_script_execution_time: subscription_config
.js_runtime_script_execution_time,
};

Expand Down
4 changes: 4 additions & 0 deletions src/utils/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ pub mod tests {
headers: None,
url: Cow::Borrowed("/?query=value"),
body: None,
requests_to_track: 3,
},
)
.await?
Expand All @@ -423,6 +424,7 @@ pub mod tests {
headers: None,
url: Cow::Borrowed("/?query=other-value"),
body: None,
requests_to_track: 3,
},
)
.await?
Expand Down Expand Up @@ -497,6 +499,7 @@ pub mod tests {
headers: None,
url: Cow::Borrowed("/?query=value"),
body: None,
requests_to_track: 3,
},
)
.await?;
Expand All @@ -510,6 +513,7 @@ pub mod tests {
headers: None,
url: Cow::Borrowed("/?query=other-value"),
body: None,
requests_to_track: 3,
},
)
.await?;
Expand Down
13 changes: 7 additions & 6 deletions src/utils/webhooks/api_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ impl<'a, DR: DnsResolver, ET: EmailTransport> WebhooksApiExt<'a, DR, ET> {
responder_id: Uuid,
params: RespondersRequestCreateParams<'r>,
) -> anyhow::Result<Option<ResponderRequest<'r>>> {
if params.requests_to_track == 0 {
return Ok(None);
}

let Some(responder) = self.get_responder(user_id, responder_id).await? else {
bail!(SecutilsError::client(format!(
"Responder ('{responder_id}') is not found."
)));
};

if responder.settings.requests_to_track == 0 {
return Ok(None);
}

let webhooks = self.api.db.webhooks();
let requests = webhooks
.get_responder_requests(user_id, responder.id)
Expand All @@ -179,8 +179,8 @@ impl<'a, DR: DnsResolver, ET: EmailTransport> WebhooksApiExt<'a, DR, ET> {
webhooks.insert_responder_request(user_id, &request).await?;

// Enforce requests limit and displace old ones.
if requests.len() >= responder.settings.requests_to_track {
let requests_to_remove = requests.len() - responder.settings.requests_to_track + 1;
if requests.len() >= params.requests_to_track {
let requests_to_remove = requests.len() - params.requests_to_track + 1;
for request_to_remove in requests.iter().take(requests_to_remove) {
webhooks
.remove_responder_request(user_id, responder.id, request_to_remove.id)
Expand Down Expand Up @@ -325,6 +325,7 @@ mod tests {
headers: None,
url: Cow::Borrowed(url),
body: None,
requests_to_track: 3,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ pub struct RespondersRequestCreateParams<'a> {
pub url: Cow<'a, str>,
/// HTTP body of the request.
pub body: Option<Cow<'a, [u8]>>,
/// Number of requests to track.
pub requests_to_track: usize,
}

0 comments on commit c32b141

Please sign in to comment.