-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
Problem Statement
The codebase currently has two separate ConnectionMode enums with the same name but in different modules:
config::types::ConnectionMode(insrc/config/types.rs)core::worker::ConnectionMode(insrc/core/worker.rs)
This duplication creates confusion, maintenance burden, and potential for bugs when the wrong type is imported.
Current State
config/types.rs ConnectionMode
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(tag = "type")]
pub enum ConnectionMode {
#[default]
#[serde(rename = "http")]
Http,
#[serde(rename = "grpc")]
Grpc, // ← Simple unit variant
}core/worker.rs ConnectionMode
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConnectionMode {
Http,
Grpc {
port: Option<u16>, // ← Struct variant with port field
},
}Key Differences
| Aspect | config::ConnectionMode | core::ConnectionMode |
|---|---|---|
| Grpc variant | Unit variant (no fields) | Struct variant with port: Option<u16> |
| Derives | Serialize, Deserialize, Default | Eq, Hash |
| Purpose | User-facing configuration | Runtime worker connection |
| Serde | Has JSON parsing annotations | No serde support |
Problems
- Name collision confusion: Same name in same crate causes import ambiguity
- No conversion exists: No
impl Fromto convert between the two types - Duplication of concept: Both represent "how to connect to workers"
- Maintenance burden: Changes must be kept in sync across two definitions
- Easy to import wrong type: Developers may accidentally use the wrong one
Proposed Solution
Consolidate into a single unified ConnectionMode enum that serves both configuration and runtime purposes:
// Location: src/core/worker.rs or src/types.rs
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ConnectionMode {
Http,
Grpc {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
port: Option<u16>,
},
}How This Works
For Configuration (JSON/YAML):
- Users can omit port:
{ "type": "grpc" }→Grpc { port: None }(auto-detect) - Users can specify port:
{ "type": "grpc", "port": 50051 }→Grpc { port: Some(50051) } - The
#[serde(skip_serializing_if = "Option::is_none")]keeps serialized output clean
For Runtime:
- Port information is available when needed
- Can derive
EqandHashfor use in collections (WorkerRegistry) - Maintains all runtime capabilities
Benefits
One source of truth - Single definition eliminates confusion
Config flexibility - Optional port field works for both simple and advanced configs
Runtime capability - Port information available when needed
All traits supported - Can derive Serialize, Deserialize, Eq, Hash simultaneously
Cleaner imports - Only one ConnectionMode to import
Better maintainability - Changes only need to be made in one place
Migration Steps
-
Add serde derives to
core::worker::ConnectionMode:#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "lowercase")] pub enum ConnectionMode { Http, Grpc { #[serde(skip_serializing_if = "Option::is_none")] #[serde(default)] port: Option<u16>, }, }
-
Remove
config::ConnectionModefromsrc/config/types.rs -
Update all imports throughout the codebase:
- Find:
use crate::config::ConnectionModeoruse crate::config::types::ConnectionMode - Replace with:
use crate::core::ConnectionMode
- Find:
-
Test configuration parsing to ensure JSON/YAML deserialization works correctly
-
Update tests if any specifically test config parsing
Impact
- Breaking change: No, the unified type is backward compatible for configs
- Migration effort: Low - mostly import statement updates
- Risk: Low - types are structurally compatible
- Files affected: Approximately 5-10 files with import changes
Additional Context
This issue was discovered while implementing IGW mode multi-router support, where the distinction between the two types caused confusion and led to type mismatch errors. Having a single well-designed type will prevent similar issues in the future.
Related: This consolidation should be done before or in conjunction with the router selection fixes for proper gRPC worker support in IGW mode.