krishvishal commented on code in PR #3519: URL: https://github.com/apache/iggy/pull/3519#discussion_r3803214722
########## 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_*` is an unregistered `IGGY_` sub-namespace; `iggy-server` `debug_assert!(false)`s on each one. `main.rs:62-102` vs `typed_env_provider.rs:317`. None of the nine names is in `IGNORED_ENV_VARS` (`:48-63`) or `DELEGATED_ENV_VAR_PREFIXES` (`:67`). Follow `MANUAL_TESTING.md:133`, then run `cargo run --bin iggy-server` or `cargo test -p integration` in the same shell: it panics at config load with debug assertions on, and the integration harness forwards `IGGY_*` to spawned servers. `IGGY_MCP_*` has the same gap, so not a regression this PR invented. Fix: add `"IGGY_KAFKA_"` to `DELEGATED_ENV_VAR_PREFIXES`. Breaks: typo'd keys then go unwarned by both layers, so pair with a known-key check, or `#[derive(ConfigEnv)]` -- 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]
