1use std::net::SocketAddr;
2use std::path::PathBuf;
3
4pub struct ServerConfig {
5 pub listen_addr: SocketAddr,
6 pub subscriber_channel_capacity: usize,
10 pub grpc_output_buffer: usize,
15 pub store_path: Option<PathBuf>,
17 pub redelivery_interval_secs: u64,
19 pub max_delivery_attempts: u32,
21 pub retention_secs: u64,
23 pub default_ack_timeout_secs: u32,
25 pub default_max_in_flight: u32,
27 pub gc_interval_secs: u64,
29 pub redelivery_batch_size: u32,
31}
32
33impl Default for ServerConfig {
34 fn default() -> Self {
35 Self {
36 listen_addr: "0.0.0.0:4222".parse().unwrap(),
37 subscriber_channel_capacity: 8192,
38 grpc_output_buffer: 1024,
39 store_path: None,
40 redelivery_interval_secs: 5,
41 max_delivery_attempts: 5,
42 retention_secs: 3600,
43 default_ack_timeout_secs: 30,
44 default_max_in_flight: 32,
45 gc_interval_secs: 60,
46 redelivery_batch_size: 100,
47 }
48 }
49}
50
51impl ServerConfig {
52 pub fn from_env() -> Self {
53 let mut config = Self::default();
54
55 if let Ok(addr) = std::env::var("HERMES_LISTEN_ADDR") {
56 if let Ok(parsed) = addr.parse() {
57 config.listen_addr = parsed;
58 }
59 }
60 if let Ok(cap) = std::env::var("HERMES_CHANNEL_CAPACITY") {
61 if let Ok(parsed) = cap.parse() {
62 config.subscriber_channel_capacity = parsed;
63 }
64 }
65 if let Ok(v) = std::env::var("HERMES_GRPC_OUTPUT_BUFFER") {
66 if let Ok(parsed) = v.parse() {
67 config.grpc_output_buffer = parsed;
68 }
69 }
70 if let Ok(path) = std::env::var("HERMES_STORE_PATH") {
71 config.store_path = Some(PathBuf::from(path));
72 }
73 if let Ok(v) = std::env::var("HERMES_REDELIVERY_INTERVAL") {
74 if let Ok(parsed) = v.parse() {
75 config.redelivery_interval_secs = parsed;
76 }
77 }
78 if let Ok(v) = std::env::var("HERMES_MAX_DELIVERY_ATTEMPTS") {
79 if let Ok(parsed) = v.parse() {
80 config.max_delivery_attempts = parsed;
81 }
82 }
83 if let Ok(v) = std::env::var("HERMES_RETENTION_SECS") {
84 if let Ok(parsed) = v.parse() {
85 config.retention_secs = parsed;
86 }
87 }
88 if let Ok(v) = std::env::var("HERMES_ACK_TIMEOUT") {
89 if let Ok(parsed) = v.parse() {
90 config.default_ack_timeout_secs = parsed;
91 }
92 }
93 if let Ok(v) = std::env::var("HERMES_MAX_IN_FLIGHT") {
94 if let Ok(parsed) = v.parse() {
95 config.default_max_in_flight = parsed;
96 }
97 }
98 if let Ok(v) = std::env::var("HERMES_GC_INTERVAL") {
99 if let Ok(parsed) = v.parse() {
100 config.gc_interval_secs = parsed;
101 }
102 }
103 if let Ok(v) = std::env::var("HERMES_REDELIVERY_BATCH_SIZE") {
104 if let Ok(parsed) = v.parse() {
105 config.redelivery_batch_size = parsed;
106 }
107 }
108
109 config
110 }
111}