use std::net::SocketAddr;
#[derive(Debug, Clone)]
pub struct ReceiverConfig {
pub grpc_addr: SocketAddr,
pub http_addr: SocketAddr,
pub grpc_compression: bool,
pub http_compression: bool,
pub max_message_size: usize,
}
impl Default for ReceiverConfig {
fn default() -> Self {
Self {
grpc_addr: "0.0.0.0:4317".parse().expect("valid address"),
http_addr: "0.0.0.0:4318".parse().expect("valid address"),
grpc_compression: true,
http_compression: true,
max_message_size: 10 * 1024 * 1024, }
}
}
impl ReceiverConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_grpc_addr(mut self, addr: SocketAddr) -> Self {
self.grpc_addr = addr;
self
}
pub fn with_http_addr(mut self, addr: SocketAddr) -> Self {
self.http_addr = addr;
self
}
pub fn with_grpc_compression(mut self, enabled: bool) -> Self {
self.grpc_compression = enabled;
self
}
pub fn with_http_compression(mut self, enabled: bool) -> Self {
self.http_compression = enabled;
self
}
pub fn with_max_message_size(mut self, size: usize) -> Self {
self.max_message_size = size;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = ReceiverConfig::default();
assert_eq!(config.grpc_addr.port(), 4317);
assert_eq!(config.http_addr.port(), 4318);
assert!(config.grpc_compression);
assert!(config.http_compression);
assert_eq!(config.max_message_size, 10 * 1024 * 1024);
}
#[test]
fn test_builder_pattern() {
let config = ReceiverConfig::new()
.with_grpc_addr("127.0.0.1:5317".parse().unwrap())
.with_http_addr("127.0.0.1:5318".parse().unwrap())
.with_grpc_compression(false)
.with_max_message_size(5 * 1024 * 1024);
assert_eq!(config.grpc_addr.port(), 5317);
assert_eq!(config.http_addr.port(), 5318);
assert!(!config.grpc_compression);
assert_eq!(config.max_message_size, 5 * 1024 * 1024);
}
}