ryerraguntla commented on code in PR #3519: URL: https://github.com/apache/iggy/pull/3519#discussion_r3812912234
########## gateways/kafka/src/main.rs: ########## @@ -0,0 +1,172 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fmt::Display; +use std::str::FromStr; +use std::time::Duration; + +use tokio::net::TcpListener; +use tokio::signal; +use tokio::sync::{Semaphore, broadcast}; + +use iggy_gateway_kafka::server::init_tracing; +use iggy_gateway_kafka::{GatewayConfig, KafkaGateway}; + +#[tokio::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + init_tracing(); + + let config = load_config()?; + + let listener = TcpListener::bind(&config.bind_addr) + .await + .map_err(|e| format!("failed to bind {}: {e}", config.bind_addr))?; + let server = KafkaGateway::new(config); + + let (tx, rx) = broadcast::channel(1); + let mut server_task = tokio::spawn(async move { server.run(listener, rx).await }); + + tokio::select! { + result = &mut server_task => { + return Ok(result??); + } + () = shutdown_signal() => { + let _ = tx.send(()); + } + } + + server_task.await??; + Ok(()) +} + +/// Build [`GatewayConfig`] from `IGGY_KAFKA_*` env vars, rejecting values that would silently +/// break the listener (a zero connection cap serves nothing, a zero timeout drops every +/// connection, a connection cap above `Semaphore::MAX_PERMITS` panics at startup). +fn load_config() -> Result<GatewayConfig, String> { + let mut config = GatewayConfig::default(); + + if let Some(bind_addr) = env_var("IGGY_KAFKA_BIND_ADDR") { + config.bind_addr = bind_addr; + } + if let Some(advertised_host) = env_var("IGGY_KAFKA_ADVERTISED_HOST") { + config.advertised_host = Some(advertised_host); + } + if let Some(raw) = env_var("IGGY_KAFKA_ADVERTISED_PORT") { + config.advertised_port = Some(parse_positive("IGGY_KAFKA_ADVERTISED_PORT", &raw)?); + } + if let Some(raw) = env_var("IGGY_KAFKA_MAX_CONNECTIONS") { + let max_connections: usize = parse_positive("IGGY_KAFKA_MAX_CONNECTIONS", &raw)?; + if max_connections > Semaphore::MAX_PERMITS { + return Err(format!( + "IGGY_KAFKA_MAX_CONNECTIONS {max_connections} exceeds maximum {}", + Semaphore::MAX_PERMITS + )); + } + config.max_connections = max_connections; + } + if let Some(raw) = env_var("IGGY_KAFKA_MAX_FRAME_SIZE") { + config.max_frame_size = parse_positive("IGGY_KAFKA_MAX_FRAME_SIZE", &raw)?; + } + if let Some(raw) = env_var("IGGY_KAFKA_IDLE_TIMEOUT_SECS") { + config.idle_timeout = + Duration::from_secs(parse_positive("IGGY_KAFKA_IDLE_TIMEOUT_SECS", &raw)?); + } + if let Some(raw) = env_var("IGGY_KAFKA_READ_TIMEOUT_SECS") { + config.read_timeout = + Duration::from_secs(parse_positive("IGGY_KAFKA_READ_TIMEOUT_SECS", &raw)?); + } + if let Some(raw) = env_var("IGGY_KAFKA_WRITE_TIMEOUT_SECS") { + config.write_timeout = + Duration::from_secs(parse_positive("IGGY_KAFKA_WRITE_TIMEOUT_SECS", &raw)?); + } + // Drain of 0 is valid: abandon in-flight connections immediately on shutdown. + if let Some(raw) = env_var("IGGY_KAFKA_SHUTDOWN_DRAIN_TIMEOUT_SECS") { + let secs: u64 = raw + .parse() + .map_err(|e| format!("invalid IGGY_KAFKA_SHUTDOWN_DRAIN_TIMEOUT_SECS `{raw}`: {e}"))?; + config.shutdown_drain_timeout = Duration::from_secs(secs); + } Review Comment: IGGY_KAFKA_ → DELEGATED_ENV_VAR_PREFIXES was already in place (fixes the debug_assert! panic on iggy-server/integration-test startup). Closed the consequence you flagged: added reject_unknown_kafka_env_vars() in main.rs, checked first in load_config() — any IGGY_KAFKA_* var not in the known 9-name set now fails startup with a clear "unknown env var, check for a typo" error instead of silently no-opping. Went with the known-key-check option rather than #[derive(ConfigEnv)], since the latter would mean restructuring the existing hand-parsed block to match core/configs' derive convention for the same practical outcome. IGGY_MCP_*'s identical gap is out of scope here (not introduced by this PR, not touched). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
