-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathupdate_workers.rs
More file actions
152 lines (135 loc) · 5.51 KB
/
update_workers.rs
File metadata and controls
152 lines (135 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use core::fmt;
use std::sync::Arc;
use miden_node_utils::ErrorReport;
use miden_remote_prover::COMPONENT;
use pingora::apps::{HttpServerApp, HttpServerOptions, ReusedHttpStream};
use pingora::http::ResponseHeader;
use pingora::protocols::http::ServerSession;
use pingora::server::ShutdownWatch;
use tonic::async_trait;
use tracing::{error, info};
use super::LoadBalancerState;
use crate::commands::update_workers::UpdateWorkers;
use crate::utils::create_response_with_error_message;
/// The Load Balancer Updater Service.
///
/// This service is responsible for updating the list of workers in the load balancer.
pub(crate) struct LoadBalancerUpdateService {
lb_state: Arc<LoadBalancerState>,
server_opts: HttpServerOptions,
}
/// Manually implement Debug for `LoadBalancerUpdateService`. [`HttpServerOptions`] does not
/// implement Debug, so we cannot derive Debug for [`LoadBalancerUpdateService`], which is needed
/// for the tracing instrumentation.
impl fmt::Debug for LoadBalancerUpdateService {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LBUpdaterService")
.field("lb_state", &self.lb_state)
.finish_non_exhaustive()
}
}
impl LoadBalancerUpdateService {
pub(crate) fn new(lb_state: Arc<LoadBalancerState>) -> Self {
let mut server_opts = HttpServerOptions::default();
server_opts.h2c = true;
Self { lb_state, server_opts }
}
}
#[async_trait]
impl HttpServerApp for LoadBalancerUpdateService {
/// Handles the update workers request.
///
/// # Behavior
/// - Reads the HTTP request from the session.
/// - If query parameters are present, attempts to parse them as an `UpdateWorkers` object.
/// - If the parsing fails, returns an error response.
/// - If successful, updates the list of workers by calling `update_workers`.
/// - If the update is successful, returns the count of available workers.
///
/// # Errors
/// - If the HTTP request cannot be read.
/// - If the query parameters cannot be parsed.
/// - If the workers cannot be updated.
/// - If the response cannot be created.
#[tracing::instrument(target = COMPONENT, name = "lb_updater_service.process_new_http", skip(http))]
async fn process_new_http(
self: &Arc<Self>,
mut http: ServerSession,
_shutdown: &ShutdownWatch,
) -> Option<ReusedHttpStream> {
match http.read_request().await {
Ok(res) => {
if !res {
error!("Failed to read request header");
create_response_with_error_message(
&mut http,
"Failed to read request header".to_string(),
)
.await
.ok();
return None;
}
},
Err(e) => {
error!("HTTP server fails to read from downstream: {e}");
create_response_with_error_message(
&mut http,
format!("HTTP server fails to read from downstream: {e}"),
)
.await
.ok();
return None;
},
}
info!("Successfully get a new request to update workers");
// Extract and parse query parameters, if there are not any, return early.
let Some(query_params) = http.req_header().as_ref().uri.query() else {
let error_message = "No query parameters provided".to_string();
error!("{}", error_message);
create_response_with_error_message(&mut http, error_message).await.ok();
return None;
};
let update_workers: Result<UpdateWorkers, _> = serde_qs::from_str(query_params);
let update_workers = match update_workers {
Ok(workers) => workers,
Err(err) => {
let error_message = err.as_report_context("failed to parse query parameters");
error!("{}", error_message);
create_response_with_error_message(&mut http, error_message).await.ok();
return None;
},
};
// Update workers and handle potential errors.
if let Err(err) = self.lb_state.update_workers(update_workers).await {
let error_message = err.as_report_context("failed to update workers");
error!("{}", error_message);
create_response_with_error_message(&mut http, error_message).await.ok();
return None;
}
create_workers_updated_response(&mut http, self.lb_state.num_workers().await)
.await
.ok();
info!("Successfully updated workers");
None
}
/// Provide HTTP server options used to override default behavior. This function will be called
/// every time a new connection is processed.
fn server_options(&self) -> Option<&HttpServerOptions> {
Some(&self.server_opts)
}
}
// HELPERS
// ================================================================================================
/// Create a 200 response for updated workers
///
/// It will set the X-Worker-Count header to the number of workers.
async fn create_workers_updated_response(
session: &mut ServerSession,
workers: usize,
) -> pingora_core::Result<bool> {
let mut header = ResponseHeader::build(200, None)?;
header.insert_header("X-Worker-Count", workers.to_string())?;
session.set_keepalive(None);
session.write_response_header(Box::new(header)).await?;
Ok(true)
}