Skip to content

Commit

Permalink
fix(budget): reduce windup
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Nov 29, 2023
1 parent bf9cb4f commit 8fe69a9
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions graph-gateway/src/budgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ struct Controller {

impl Controller {
fn new(target_query_fees: USD) -> Self {
let mut error_history = FastDecayBuffer::default();
*error_history.current_mut() = target_query_fees.0.into();
Self {
target_query_fees,
recent_fees: USD(UDecimal18::from(0)),
recent_query_count: 0,
error_history,
error_history: FastDecayBuffer::default(),
}
}

Expand All @@ -123,12 +121,12 @@ impl Controller {
self.recent_query_count = 0;
self.error_history.decay();
let error = f64::from(self.target_query_fees.0) - process_variable;
*self.error_history.current_mut() = error;
*self.error_history.current_mut() += error;

let i: f64 = self.error_history.frames().iter().sum();
let k_i = 1.2;
let correction = UDecimal18::try_from(i * k_i).unwrap_or_default();
self.target_query_fees.0 + correction
let k_i = 30e3;
self.target_query_fees.0
* (UDecimal18::from(1) + UDecimal18::try_from(i * k_i).unwrap_or_default())
}
}

Expand All @@ -147,7 +145,7 @@ mod tests {
) {
let setpoint: f64 = controller.target_query_fees.0.into();
let mut process_variable = 0.0;
for i in 0..20 {
for i in 0..60 {
let control_variable: f64 = controller.control_variable().into();
process_variable = control_variable * process_variable_multiplier;
println!(
Expand All @@ -159,19 +157,20 @@ mod tests {
assert_within(process_variable, setpoint, tolerance);
}

let tolerance = 1.0e-6;
for setpoint in [10e-6, 20e-6, 50e-6] {
let setpoint = USD(UDecimal18::try_from(setpoint).unwrap());
let mut controller = Controller::new(setpoint);
test_controller(&mut controller, 0.2, 1e-6);
test_controller(&mut controller, 0.2, tolerance);
let mut controller = Controller::new(setpoint);
test_controller(&mut controller, 0.6, 1e-6);
test_controller(&mut controller, 0.6, tolerance);
let mut controller = Controller::new(setpoint);
test_controller(&mut controller, 0.8, 1e-6);
test_controller(&mut controller, 0.8, tolerance);

let mut controller = Controller::new(setpoint);
test_controller(&mut controller, 0.2, 1e-6);
test_controller(&mut controller, 0.6, 1e-6);
test_controller(&mut controller, 0.7, 1e-6);
test_controller(&mut controller, 0.2, tolerance);
test_controller(&mut controller, 0.6, tolerance);
test_controller(&mut controller, 0.7, tolerance);
}
}
}

0 comments on commit 8fe69a9

Please sign in to comment.