summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStuart Stock <stuart@int08h.com>2018-10-22 20:35:36 -0500
committerStuart Stock <stuart@int08h.com>2018-10-22 20:35:36 -0500
commitbed4ed8e8db78434a77bb112a59c360f8f9eb803 (patch)
treea0a1cca35d90a1ee1823491a8c1ffcf9e1a8ae3f /src
parent2225b7c4ecb40cef0c9bf6b4d50b48c0009e6e6a (diff)
downloadroughenough-bed4ed8e8db78434a77bb112a59c360f8f9eb803.zip
groundwork for TCP healthcheck in #8
Diffstat (limited to 'src')
-rw-r--r--src/config/environment.rs32
-rw-r--r--src/config/file.rs10
-rw-r--r--src/config/memory.rs6
-rw-r--r--src/config/mod.rs12
-rw-r--r--src/server.rs2
5 files changed, 50 insertions, 12 deletions
diff --git a/src/config/environment.rs b/src/config/environment.rs
index d051d6d..797f422 100644
--- a/src/config/environment.rs
+++ b/src/config/environment.rs
@@ -26,14 +26,15 @@ use Error;
/// Obtain a Roughenough server configuration ([ServerConfig](trait.ServerConfig.html))
/// from environment variables.
///
-/// Config parameter | Environment Variable
-/// ---------------- | --------------------
-/// port | `ROUGHENOUGH_PORT`
-/// interface | `ROUGHENOUGH_INTERFACE`
-/// seed | `ROUGHENOUGH_SEED`
-/// batch_size | `ROUGHENOUGH_BATCH_SIZE`
-/// status_interval | `ROUGHENOUGH_STATUS_INTERVAL`
-/// key_protection | `ROUGHENOUGH_KEY_PROTECTION`
+/// Config parameter | Environment Variable
+/// ---------------- | --------------------
+/// port | `ROUGHENOUGH_PORT`
+/// interface | `ROUGHENOUGH_INTERFACE`
+/// seed | `ROUGHENOUGH_SEED`
+/// batch_size | `ROUGHENOUGH_BATCH_SIZE`
+/// status_interval | `ROUGHENOUGH_STATUS_INTERVAL`
+/// key_protection | `ROUGHENOUGH_KEY_PROTECTION`
+/// health_check_port | `ROUGHENOUGH_HEALTH_CHECK_PORT`
///
pub struct EnvironmentConfig {
port: u16,
@@ -42,6 +43,7 @@ pub struct EnvironmentConfig {
batch_size: u8,
status_interval: Duration,
key_protection: KeyProtection,
+ health_check_port: Option<u16>,
}
const ROUGHENOUGH_PORT: &str = "ROUGHENOUGH_PORT";
@@ -50,6 +52,7 @@ const ROUGHENOUGH_SEED: &str = "ROUGHENOUGH_SEED";
const ROUGHENOUGH_BATCH_SIZE: &str = "ROUGHENOUGH_BATCH_SIZE";
const ROUGHENOUGH_STATUS_INTERVAL: &str = "ROUGHENOUGH_STATUS_INTERVAL";
const ROUGHENOUGH_KEY_PROTECTION: &str = "ROUGHENOUGH_KEY_PROTECTION";
+const ROUGHENOUGH_HEALTH_CHECK_PORT: &str = "ROUGHENOUGH_HEALTH_CHECK_PORT";
impl EnvironmentConfig {
pub fn new() -> Result<Self, Error> {
@@ -60,6 +63,7 @@ impl EnvironmentConfig {
batch_size: DEFAULT_BATCH_SIZE,
status_interval: DEFAULT_STATUS_INTERVAL,
key_protection: KeyProtection::Plaintext,
+ health_check_port: None,
};
if let Ok(port) = env::var(ROUGHENOUGH_PORT) {
@@ -97,6 +101,14 @@ impl EnvironmentConfig {
.unwrap_or_else(|_| panic!("invalid key_protection value: {}", key_protection));
}
+ if let Ok(health_check_port) = env::var(ROUGHENOUGH_HEALTH_CHECK_PORT) {
+ let val: u16 = health_check_port
+ .parse()
+ .unwrap_or_else(|_| panic!("invalid health_check_port: {}", health_check_port));
+
+ cfg.health_check_port = Some(val);
+ };
+
Ok(cfg)
}
}
@@ -125,4 +137,8 @@ impl ServerConfig for EnvironmentConfig {
fn key_protection(&self) -> &KeyProtection {
&self.key_protection
}
+
+ fn health_check_port(&self) -> Option<u16> {
+ self.health_check_port
+ }
}
diff --git a/src/config/file.rs b/src/config/file.rs
index b1e856b..b70392d 100644
--- a/src/config/file.rs
+++ b/src/config/file.rs
@@ -43,6 +43,7 @@ pub struct FileConfig {
batch_size: u8,
status_interval: Duration,
key_protection: KeyProtection,
+ health_check_port: Option<u16>,
}
impl FileConfig {
@@ -69,6 +70,7 @@ impl FileConfig {
batch_size: DEFAULT_BATCH_SIZE,
status_interval: DEFAULT_STATUS_INTERVAL,
key_protection: KeyProtection::Plaintext,
+ health_check_port: None,
};
for (key, value) in cfg[0].as_hash().unwrap() {
@@ -93,6 +95,10 @@ impl FileConfig {
.unwrap_or_else(|_| panic!("invalid key_protection value: {:?}", value));
config.key_protection = val
}
+ "health_check_port" => {
+ let val = value.as_i64().unwrap() as u16;
+ config.health_check_port = Some(val);
+ }
unknown => {
return Err(Error::InvalidConfiguration(format!(
"unknown config key: {}",
@@ -130,4 +136,8 @@ impl ServerConfig for FileConfig {
fn key_protection(&self) -> &KeyProtection {
&self.key_protection
}
+
+ fn health_check_port(&self) -> Option<u16> {
+ self.health_check_port
+ }
}
diff --git a/src/config/memory.rs b/src/config/memory.rs
index aaf2e08..47480d6 100644
--- a/src/config/memory.rs
+++ b/src/config/memory.rs
@@ -29,6 +29,7 @@ pub struct MemoryConfig {
pub batch_size: u8,
pub status_interval: Duration,
pub key_protection: KeyProtection,
+ pub health_check_port: Option<u16>,
}
impl MemoryConfig {
@@ -41,6 +42,7 @@ impl MemoryConfig {
batch_size: DEFAULT_BATCH_SIZE,
status_interval: DEFAULT_STATUS_INTERVAL,
key_protection: KeyProtection::Plaintext,
+ health_check_port: None
}
}
}
@@ -69,4 +71,8 @@ impl ServerConfig for MemoryConfig {
fn key_protection(&self) -> &KeyProtection {
&self.key_protection
}
+
+ fn health_check_port(&self) -> Option<u16> {
+ self.health_check_port
+ }
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 772e1ee..b0ff9b4 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -60,6 +60,7 @@ pub const DEFAULT_STATUS_INTERVAL: Duration = Duration::from_secs(600);
/// `batch_size` | `ROUGHENOUGH_BATCH_SIZE` | Optional | The maximum number of requests to process in one batch. All nonces in a batch are used to build a Merkle tree, the root of which is signed. Defaults to [DEFAULT_BATCH_SIZE](constant.DEFAULT_BATCH_SIZE.html) requests per batch.
/// `status_interval` | `ROUGHENOUGH_STATUS_INTERVAL` | Optional | Number of _seconds_ between each logged status update. Default value is [DEFAULT_STATUS_INTERVAL](constant.DEFAULT_STATUS_INTERVAL.html).
/// `key_protection` | `ROUGHENOUGH_KEY_PROTECTION` | Optional | Encryption method (if any) applied to the `seed`. Defaults to "`plaintext`" (no encryption, `seed` is in the clear).
+/// `health_check_port` | `ROUGHENOUGH_HEALTH_CHECK_PORT` | Optional | If present, the TCP port to respond to Google-style HTTP "legacy health check".
///
/// Implementations of this trait obtain a valid configuration from different back-end
/// sources. See:
@@ -91,8 +92,13 @@ pub trait ServerConfig {
/// Defaults to "`plaintext`" (no encryption, seed is in the clear).
fn key_protection(&self) -> &KeyProtection;
+ /// [Optional] If present, the TCP port to respond to Google-style HTTP "legacy health check".
+ /// This is a *very* simplistic check, it emits a fixed HTTP response to all TCP connections.
+ /// https://cloud.google.com/load-balancing/docs/health-checks#legacy-health-checks
+ fn health_check_port(&self) -> Option<u16>;
+
/// Convenience function to create a `SocketAddr` from the provided `interface` and `port`
- fn socket_addr(&self) -> Result<SocketAddr, Error> {
+ fn udp_socket_addr(&self) -> Result<SocketAddr, Error> {
let addr = format!("{}:{}", self.interface(), self.port());
match addr.parse() {
Ok(v) => Ok(v),
@@ -152,10 +158,10 @@ pub fn is_valid_config(cfg: &Box<ServerConfig>) -> bool {
}
if is_valid {
- match cfg.socket_addr() {
+ match cfg.udp_socket_addr() {
Err(e) => {
error!(
- "failed to create socket {}:{} {:?}",
+ "failed to create UDP socket {}:{} {:?}",
cfg.interface(),
cfg.port(),
e
diff --git a/src/server.rs b/src/server.rs
index a187508..66e3715 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -112,7 +112,7 @@ impl Server {
let response_counter = AtomicUsize::new(0);
let keep_running = Arc::new(AtomicBool::new(true));
- let sock_addr = config.socket_addr().expect("");
+ let sock_addr = config.udp_socket_addr().expect("");
let socket = UdpSocket::bind(&sock_addr).expect("failed to bind to socket");
let poll_duration = Some(Duration::from_millis(100));