Skip to content

Commit

Permalink
write Response in protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Apr 28, 2023
1 parent 73c496d commit d51590c
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
5 changes: 2 additions & 3 deletions bin/src/acme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use sozu_command_lib::{
config::Config,
proto::command::{
request::RequestType, AddBackend, AddCertificate, CertificateAndKey, PathRule,
RemoveBackend, ReplaceCertificate, Request, RequestHttpFrontend, ResponseStatus,
RemoveBackend, ReplaceCertificate, Request, RequestHttpFrontend, Response, ResponseStatus,
TlsVersion,
},
response::Response,
};

use crate::util;
Expand Down Expand Up @@ -409,7 +408,7 @@ fn order_request(channel: &mut Channel<Request, Response>, request: Request) ->
.read_message()
.with_context(|| "Could not read response on channel")?;

match response.status {
match response.status() {
ResponseStatus::Processing => {
// do nothing here
// for other messages, we would loop over read_message
Expand Down
4 changes: 2 additions & 2 deletions bin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ use sozu_command_lib::{
config::Config,
proto::command::{
request::RequestType, response_content::ContentType, MetricsConfiguration, Request,
ResponseContent, ResponseStatus, RunState, Status,
Response, ResponseContent, ResponseStatus, RunState, Status,
},
request::WorkerRequest,
response::{Response, WorkerResponse},
response::WorkerResponse,
scm_socket::{Listeners, ScmSocket},
state::ConfigState,
};
Expand Down
5 changes: 2 additions & 3 deletions bin/src/command/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ use sozu_command_lib::{
proto::command::{
request::RequestType, response_content::ContentType, AggregatedMetrics, AvailableMetrics,
ClusterHashes, ClusterInformations, FrontendFilters, ListedFrontends, ListenersList,
MetricsConfiguration, Request, ResponseContent, ResponseStatus, ReturnListenSockets,
RunState, SoftStop, Status, WorkerInfo, WorkerInfos, WorkerResponses,
MetricsConfiguration, Request, Response, ResponseContent, ResponseStatus,
ReturnListenSockets, RunState, SoftStop, Status, WorkerInfo, WorkerInfos, WorkerResponses,
},
request::WorkerRequest,
response::Response,
scm_socket::Listeners,
state::get_cluster_ids_by_domain,
};
Expand Down
25 changes: 11 additions & 14 deletions bin/src/ctl/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ use anyhow::{self, bail, Context};
use prettytable::Table;
use serde::Serialize;

use sozu_command_lib::{
proto::command::{
request::RequestType, response_content::ContentType, ListWorkers, QueryAllCertificates,
QueryClusterByDomain, QueryClustersHashes, QueryMetricsOptions, Request, ResponseContent,
ResponseStatus, RunState, UpgradeMain, WorkerInfo,
},
response::Response,
use sozu_command_lib::proto::command::{
request::RequestType, response_content::ContentType, ListWorkers, QueryAllCertificates,
QueryClusterByDomain, QueryClustersHashes, QueryMetricsOptions, Request, Response,
ResponseContent, ResponseStatus, RunState, UpgradeMain, WorkerInfo,
};

use crate::ctl::{
Expand Down Expand Up @@ -58,7 +55,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => println!("Proxy is processing: {}", response.message),
ResponseStatus::Failure => bail!("Request failed: {}", response.message),
ResponseStatus::Ok => {
Expand Down Expand Up @@ -105,7 +102,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => {
println!("Processing: {}", response.message);
}
Expand Down Expand Up @@ -140,7 +137,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => {
println!("Main process is upgrading");
}
Expand Down Expand Up @@ -207,7 +204,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => info!("Proxy is processing: {}", response.message),
ResponseStatus::Failure => bail!(
"could not stop the worker {}: {}",
Expand Down Expand Up @@ -255,7 +252,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => {
println!("Proxy is processing: {}", response.message);
}
Expand Down Expand Up @@ -343,7 +340,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => {
println!("Proxy is processing: {}", response.message);
}
Expand Down Expand Up @@ -399,7 +396,7 @@ impl CommandManager {
loop {
let response = self.read_channel_message_with_timeout()?;

match response.status {
match response.status() {
ResponseStatus::Processing => {
println!("Proxy is processing: {}", response.message);
}
Expand Down
4 changes: 3 additions & 1 deletion bin/src/ctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use std::time::Duration;
use anyhow::Context;

use sozu_command_lib::{
channel::Channel, config::Config, proto::command::Request, response::Response,
channel::Channel,
config::Config,
proto::command::{Request, Response},
};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion command/assets/answer_metrics.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"status": "OK",
"status": 0,
"message": "",
"content": {
"content_type": {
Expand Down
2 changes: 1 addition & 1 deletion command/assets/answer_workers_status.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"status": "OK",
"status": 0,
"message": "",
"content": {
"content_type": {
Expand Down
11 changes: 11 additions & 0 deletions command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,17 @@ enum MetricsConfiguration {
CLEAR = 2;
}

// Response to a request
message Response {
// wether the request was a success, a failure, or is processing
required ResponseStatus status = 1 [default = FAILURE];
// a success or error message
required string message = 2;
// response data, if any
optional ResponseContent content = 3;
}


// content of a response
message ResponseContent {
oneof content_type {
Expand Down
18 changes: 5 additions & 13 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@ use std::{cmp::Ordering, collections::BTreeMap, default::Default, fmt, net::Sock
use crate::{
proto::command::{
AddBackend, FilteredTimeSerie, LoadBalancingParams, PathRule, PathRuleKind,
RequestHttpFrontend, RequestTcpFrontend, ResponseContent, ResponseStatus, RulePosition,
RunState,
RequestHttpFrontend, RequestTcpFrontend, Response, ResponseContent, ResponseStatus,
RulePosition, RunState,
},
state::ClusterId,
};

/// Responses of the main process to the CLI (or other client)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Response {
pub status: ResponseStatus,
pub message: String,
pub content: Option<ResponseContent>,
}

impl Response {
pub fn new(
status: ResponseStatus,
message: String,
content: Option<ResponseContent>,
) -> Response {
Response {
status,
status: status as i32,
message,
content,
}
Expand Down Expand Up @@ -362,7 +354,7 @@ mod tests {
answer_workers_status,
"../assets/answer_workers_status.json",
Response {
status: ResponseStatus::Ok,
status: ResponseStatus::Ok as i32,
message: String::from(""),
content: Some(ResponseContent {
content_type: Some(ContentType::Workers(WorkerInfos {
Expand All @@ -387,7 +379,7 @@ mod tests {
answer_metrics,
"../assets/answer_metrics.json",
Response {
status: ResponseStatus::Ok,
status: ResponseStatus::Ok as i32,
message: String::from(""),
content: Some(ResponseContent {
content_type: Some(ContentType::Metrics(AggregatedMetrics {
Expand Down

0 comments on commit d51590c

Please sign in to comment.