Skip to content

Commit

Permalink
make response::Event a struct, create EventKind
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Apr 28, 2023
1 parent 7214aa2 commit 7fb08f3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
1 change: 0 additions & 1 deletion command/src/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ enum MetricsConfiguration {
CLEAR = 2;
}


enum ResponseStatus {
OK = 0;
PROCESSING = 1;
Expand Down
24 changes: 14 additions & 10 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,21 @@ impl fmt::Display for RunState {
}
}

// TODO: remove the SocketAddr type
/// a backend event that happened on a proxy
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Event {
BackendDown(String, SocketAddr),
BackendUp(String, SocketAddr),
NoAvailableBackends(String),
/// indicates a backend that was removed from configuration has no lingering connections
/// so it can be safely stopped
RemovedBackendHasNoConnections(String, SocketAddr),
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Event {
pub kind: EventKind,
pub cluster_id: Option<String>,
pub backend_id: Option<String>,
pub address: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EventKind {
BackendDown,
BackendUp,
NoAvailableBackends,
RemovedBackendHasNoConnections,
}

#[derive(Serialize)]
Expand Down
9 changes: 7 additions & 2 deletions lib/src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mio::net::TcpStream;

use sozu_command::{
proto::command::{LoadBalancingAlgorithms, LoadMetric},
response::Event,
response::{Event, EventKind},
state::ClusterId,
};

Expand Down Expand Up @@ -105,7 +105,12 @@ impl BackendMap {
if self.available {
self.available = false;

push_event(Event::NoAvailableBackends(cluster_id.to_string()));
push_event(Event {
kind: EventKind::NoAvailableBackends,
cluster_id: Some(cluster_id.to_owned()),
backend_id: None,
address: None,
});
}
bail!("No more backend available for cluster {}", cluster_id);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ use sozu_command::{
proto::command::{Cluster, LoadBalancingParams},
ready::Ready,
request::WorkerRequest,
response::{Event, WorkerResponse},
response::{Event, EventKind, WorkerResponse},
state::ClusterId,
};
use time::{Duration, Instant};
Expand Down Expand Up @@ -716,10 +716,12 @@ impl Backend {
// can be safely stopped
impl std::ops::Drop for Backend {
fn drop(&mut self) {
server::push_event(Event::RemovedBackendHasNoConnections(
self.backend_id.clone(),
self.address,
));
server::push_event(Event {
kind: EventKind::RemovedBackendHasNoConnections,
backend_id: Some(self.backend_id.clone()),
address: Some(self.address.to_string()),
cluster_id: None,
});
}
}

Expand Down
22 changes: 13 additions & 9 deletions lib/src/protocol/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
use anyhow::{bail, Context};
use mio::{net::TcpStream, *};
use rusty_ulid::Ulid;
use sozu_command::response::Event;
use sozu_command::response::{Event, EventKind};
use time::{Duration, Instant};

use crate::{
Expand Down Expand Up @@ -2220,10 +2220,12 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
backend.backend_id, backend.address
);

push_event(Event::BackendUp(
backend.backend_id.clone(),
backend.address,
));
push_event(Event {
kind: EventKind::BackendUp,
backend_id: Some(backend.backend_id.to_owned()),
address: Some(backend.address.to_string()),
cluster_id: None,
});
}

if let BackendConnectionStatus::Connecting(start) = last {
Expand Down Expand Up @@ -2261,10 +2263,12 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
metrics.backend_id.as_deref()
);

push_event(Event::BackendDown(
backend.backend_id.clone(),
backend.address,
));
push_event(Event {
kind: EventKind::BackendDown,
backend_id: Some(backend.backend_id.to_owned()),
address: Some(backend.address.to_string()),
cluster_id: None,
});
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use mio::{
};
use rusty_ulid::Ulid;
use slab::Slab;
use sozu_command::proto::command::request::RequestType;
use time::{Duration, Instant};

use sozu_command::{proto::command::request::RequestType, response::EventKind};

use crate::{
backends::BackendMap,
pool::{Checkout, Pool},
Expand Down Expand Up @@ -504,10 +505,12 @@ impl TcpSession {
"backend server {} at {} is up",
backend.backend_id, backend.address
);
push_event(Event::BackendUp(
backend.backend_id.clone(),
backend.address,
));
push_event(Event {
kind: EventKind::BackendUp,
backend_id: Some(backend.backend_id.to_owned()),
address: Some(backend.address.to_string()),
cluster_id: None,
});
}

if let BackendConnectionStatus::Connecting(start) = last {
Expand Down Expand Up @@ -556,10 +559,12 @@ impl TcpSession {
self.metrics.backend_id.as_deref()
);

push_event(Event::BackendDown(
backend.backend_id.clone(),
backend.address,
));
push_event(Event {
kind: EventKind::BackendDown,
backend_id: Some(backend.backend_id.to_owned()),
address: Some(backend.address.to_string()),
cluster_id: None,
});
}
}
}
Expand Down

0 comments on commit 7fb08f3

Please sign in to comment.