-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refac: centralize http clients in the
proto
crate (#51)
- Loading branch information
Showing
26 changed files
with
296 additions
and
108 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use axum::{extract::State, Json}; | ||
use proto::ctl::deployer::{ | ||
DeployServiceReq, DeployServiceRes, ReportDeployInstanceStatusReq, | ||
ReportDeployInstanceStatusRes, TerminateServiceReq, TerminateServiceRes, | ||
}; | ||
|
||
use crate::http::HttpState; | ||
|
||
pub async fn report_instance_status( | ||
State(state): State<HttpState>, | ||
Json(_payload): Json<ReportDeployInstanceStatusReq>, | ||
) -> Json<ReportDeployInstanceStatusRes> { | ||
_ = state.discovery; | ||
todo!(); | ||
} | ||
|
||
pub async fn deploy_service( | ||
State(_state): State<HttpState>, | ||
Json(_payload): Json<DeployServiceReq>, | ||
) -> Json<DeployServiceRes> { | ||
todo!(); | ||
} | ||
|
||
pub async fn terminate_service( | ||
State(_state): State<HttpState>, | ||
Json(_payload): Json<TerminateServiceReq>, | ||
) -> Json<TerminateServiceRes> { | ||
todo!(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use axum::Json; | ||
use proto::{common::node::Metrics, ctl::worker::PushWorkerMetricsRes}; | ||
use tracing::info; | ||
|
||
pub async fn push_metrics(Json(payload): Json<Metrics>) -> Json<PushWorkerMetricsRes> { | ||
info!("{payload:#?}"); | ||
todo!() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::{net::IpAddr, sync::Arc}; | ||
|
||
use chrono::{DateTime, Utc}; | ||
|
||
use crate::{ | ||
clients::BaseClient, | ||
common::{ | ||
instance::{self, InstanceId}, | ||
node::Metrics, | ||
service::{ServiceId, ServiceSpec}, | ||
}, | ||
ctl::{ | ||
deployer::{ | ||
DeployServiceReq, DeployServiceRes, RedeploymentPolicy, ReportDeployInstanceStatusReq, | ||
ReportDeployInstanceStatusRes, TerminateServiceReq, TerminateServiceRes, | ||
}, | ||
worker::{PushWorkerMetricsReq, PushWorkerMetricsRes}, | ||
}, | ||
well_known::CTL_HTTP_PORT, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct CtlClient { | ||
base_url: Arc<str>, | ||
client: BaseClient, | ||
} | ||
|
||
impl CtlClient { | ||
#[must_use] | ||
pub fn new(ctl_addr: IpAddr) -> Self { | ||
let base_url = format!("http://{ctl_addr}:{CTL_HTTP_PORT}") | ||
.into_boxed_str() | ||
.into(); | ||
let client = BaseClient::new(); | ||
CtlClient { base_url, client } | ||
} | ||
|
||
fn url(&self, path: &str) -> String { | ||
assert!(path.starts_with('/')); | ||
format!("{base}{path}", base = self.base_url) | ||
} | ||
|
||
pub async fn push_metrics( | ||
&self, | ||
metrics: Metrics, | ||
recorded_at: DateTime<Utc>, | ||
) -> eyre::Result<PushWorkerMetricsRes> { | ||
let body = PushWorkerMetricsReq { | ||
metrics, | ||
recorded_at, | ||
}; | ||
self.client | ||
.send(self.url("/worker/push-metrics"), &body) | ||
.await | ||
} | ||
|
||
pub async fn deploy_service( | ||
&self, | ||
service_spec: ServiceSpec, | ||
redeployment_policy: RedeploymentPolicy, | ||
) -> eyre::Result<DeployServiceRes> { | ||
let body = DeployServiceReq { | ||
service_spec, | ||
redeployment_policy, | ||
}; | ||
self.client | ||
.send(self.url("/deployer/deploy-service"), &body) | ||
.await | ||
} | ||
|
||
pub async fn terminate_service( | ||
&self, | ||
service_id: ServiceId, | ||
) -> eyre::Result<TerminateServiceRes> { | ||
let body = TerminateServiceReq { service_id }; | ||
self.client | ||
.send(self.url("/deployer/terminate-service"), &body) | ||
.await | ||
} | ||
|
||
pub async fn report_instance_status( | ||
&self, | ||
instance_id: InstanceId, | ||
status: instance::Status, | ||
) -> eyre::Result<ReportDeployInstanceStatusRes> { | ||
let body = ReportDeployInstanceStatusReq { | ||
instance_id, | ||
status, | ||
}; | ||
self.client.send(self.url("/deployer/status"), &body).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
mod ctl; | ||
pub use ctl::CtlClient; | ||
|
||
mod worker; | ||
use eyre::Context as _; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
pub use worker::WorkerClient; | ||
|
||
#[derive(Clone)] | ||
pub struct BaseClient { | ||
client: reqwest::Client, | ||
} | ||
|
||
impl BaseClient { | ||
#[must_use] | ||
fn new() -> Self { | ||
let client = reqwest::Client::new(); | ||
Self { client } | ||
} | ||
|
||
/// Sends a request to the given path, on the given worker. | ||
/// | ||
/// Paths must start with a `/`. | ||
async fn send<Req, Res>(&self, url: impl AsRef<str>, body: &Req) -> eyre::Result<Res> | ||
where | ||
Req: Serialize, | ||
Res: DeserializeOwned, | ||
{ | ||
let res = self | ||
.client | ||
.post(url.as_ref()) | ||
.json(body) | ||
.send() | ||
.await | ||
.wrap_err("failed to send request to worker")? | ||
.json::<Res>() | ||
.await | ||
.wrap_err("failed to parse response from worker")?; | ||
Ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::net::IpAddr; | ||
|
||
use crate::{ | ||
clients::BaseClient, | ||
common::instance::{InstanceId, InstanceSpec}, | ||
well_known::WORKER_HTTP_PORT, | ||
worker::runner::{ | ||
DeployInstanceReq, DeployInstanceRes, TerminateInstanceReq, TerminateInstanceRes, | ||
}, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct WorkerClient { | ||
client: BaseClient, | ||
} | ||
|
||
impl WorkerClient { | ||
#[must_use] | ||
#[allow(clippy::new_without_default)] | ||
pub fn new() -> Self { | ||
let client = BaseClient::new(); | ||
WorkerClient { client } | ||
} | ||
|
||
#[allow(clippy::unused_self)] | ||
fn url(&self, worker: IpAddr, path: &str) -> String { | ||
assert!(path.starts_with('/')); | ||
format!("http://{worker}:{WORKER_HTTP_PORT}{path}") | ||
} | ||
|
||
pub async fn deploy_instance( | ||
&self, | ||
worker: IpAddr, | ||
instance_spec: InstanceSpec, | ||
) -> eyre::Result<DeployInstanceRes> { | ||
let body = DeployInstanceReq { instance_spec }; | ||
self.client | ||
.send(self.url(worker, "/runner/deploy-instance"), &body) | ||
.await | ||
} | ||
|
||
pub async fn terminate_instance( | ||
&self, | ||
worker: IpAddr, | ||
instance_id: InstanceId, | ||
) -> eyre::Result<TerminateInstanceRes> { | ||
let body = TerminateInstanceReq { instance_id }; | ||
self.client | ||
.send(self.url(worker, "/runner/terminate-instance"), &body) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod deployer; | ||
pub mod worker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.