Skip to content

Commit

Permalink
End solve loop slightly before the deadline (#8)
Browse files Browse the repository at this point in the history
Required for cowprotocol/services#2222
(otherwise DEX solvers would time out in production)
  • Loading branch information
fleupold authored Dec 30, 2023
1 parent 61441db commit a9a9696
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/domain/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ impl Deadline {
.to_std()
.ok()
}

/// Returns a new deadline with the specified duration subtracted.
pub fn reduce(self, duration: chrono::Duration) -> Self {
Self(self.0 - duration)
}
}
14 changes: 11 additions & 3 deletions src/domain/solver/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use {
crate::{
boundary::rate_limiter::{RateLimiter, RateLimiterError},
domain::{
self,
auction,
self, auction,
dex::{self, slippage},
order::{self, Order},
solution,
Expand Down Expand Up @@ -45,6 +44,10 @@ pub struct Dex {
rate_limiter: RateLimiter,
}

/// The amount of time we aim the solver to finish before the final deadline is
/// reached.
const DEADLINE_SLACK: chrono::Duration = chrono::Duration::milliseconds(500);

impl Dex {
pub fn new(dex: infra::dex::Dex, config: infra::config::dex::Config) -> Self {
let rate_limiter = RateLimiter::new(config.rate_limiting_strategy, "dex_api".to_string());
Expand Down Expand Up @@ -72,7 +75,12 @@ impl Dex {
}
};

let deadline = auction.deadline.remaining().unwrap_or_default();
let deadline = auction
.deadline
.clone()
.reduce(DEADLINE_SLACK)
.remaining()
.unwrap_or_default();
if tokio::time::timeout(deadline, solve_orders).await.is_err() {
tracing::debug!("reached deadline; stopping to solve");
}
Expand Down

0 comments on commit a9a9696

Please sign in to comment.