refactor CHORUS

This commit is contained in:
anthonyrawlins
2025-09-06 14:47:41 +10:00
parent 9bdcbe0447
commit b6634e4c1b
16 changed files with 225 additions and 225 deletions

View File

@@ -241,13 +241,13 @@ func TestConfig_GetBootstrapPeers(t *testing.T) {
func TestConfigWithEnvironmentOverrides(t *testing.T) {
// Set environment variables
os.Setenv("BZZZ_AGENT_ID", "env-test-agent")
os.Setenv("BZZZ_P2P_PORT", "9999")
os.Setenv("BZZZ_ENCRYPTION_ENABLED", "false")
os.Setenv("CHORUS_AGENT_ID", "env-test-agent")
os.Setenv("CHORUS_P2P_PORT", "9999")
os.Setenv("CHORUS_ENCRYPTION_ENABLED", "false")
defer func() {
os.Unsetenv("BZZZ_AGENT_ID")
os.Unsetenv("BZZZ_P2P_PORT")
os.Unsetenv("BZZZ_ENCRYPTION_ENABLED")
os.Unsetenv("CHORUS_AGENT_ID")
os.Unsetenv("CHORUS_P2P_PORT")
os.Unsetenv("CHORUS_ENCRYPTION_ENABLED")
}()
cfg := DefaultConfig()

View File

@@ -24,34 +24,34 @@ type HybridConfig struct {
}
type HybridDHTConfig struct {
Backend string `env:"BZZZ_DHT_BACKEND" default:"mock" json:"backend" yaml:"backend"`
BootstrapNodes []string `env:"BZZZ_DHT_BOOTSTRAP_NODES" json:"bootstrap_nodes" yaml:"bootstrap_nodes"`
FallbackOnError bool `env:"BZZZ_FALLBACK_ON_ERROR" default:"true" json:"fallback_on_error" yaml:"fallback_on_error"`
HealthCheckInterval time.Duration `env:"BZZZ_HEALTH_CHECK_INTERVAL" default:"30s" json:"health_check_interval" yaml:"health_check_interval"`
MaxRetries int `env:"BZZZ_DHT_MAX_RETRIES" default:"3" json:"max_retries" yaml:"max_retries"`
RetryBackoff time.Duration `env:"BZZZ_DHT_RETRY_BACKOFF" default:"1s" json:"retry_backoff" yaml:"retry_backoff"`
OperationTimeout time.Duration `env:"BZZZ_DHT_OPERATION_TIMEOUT" default:"10s" json:"operation_timeout" yaml:"operation_timeout"`
Backend string `env:"CHORUS_DHT_BACKEND" default:"mock" json:"backend" yaml:"backend"`
BootstrapNodes []string `env:"CHORUS_DHT_BOOTSTRAP_NODES" json:"bootstrap_nodes" yaml:"bootstrap_nodes"`
FallbackOnError bool `env:"CHORUS_FALLBACK_ON_ERROR" default:"true" json:"fallback_on_error" yaml:"fallback_on_error"`
HealthCheckInterval time.Duration `env:"CHORUS_HEALTH_CHECK_INTERVAL" default:"30s" json:"health_check_interval" yaml:"health_check_interval"`
MaxRetries int `env:"CHORUS_DHT_MAX_RETRIES" default:"3" json:"max_retries" yaml:"max_retries"`
RetryBackoff time.Duration `env:"CHORUS_DHT_RETRY_BACKOFF" default:"1s" json:"retry_backoff" yaml:"retry_backoff"`
OperationTimeout time.Duration `env:"CHORUS_DHT_OPERATION_TIMEOUT" default:"10s" json:"operation_timeout" yaml:"operation_timeout"`
}
type HybridUCXLConfig struct {
CacheEnabled bool `env:"BZZZ_UCXL_CACHE_ENABLED" default:"true" json:"cache_enabled" yaml:"cache_enabled"`
CacheTTL time.Duration `env:"BZZZ_UCXL_CACHE_TTL" default:"5m" json:"cache_ttl" yaml:"cache_ttl"`
UseDistributed bool `env:"BZZZ_UCXL_USE_DISTRIBUTED" default:"false" json:"use_distributed" yaml:"use_distributed"`
MaxCacheSize int `env:"BZZZ_UCXL_MAX_CACHE_SIZE" default:"10000" json:"max_cache_size" yaml:"max_cache_size"`
CacheEnabled bool `env:"CHORUS_UCXL_CACHE_ENABLED" default:"true" json:"cache_enabled" yaml:"cache_enabled"`
CacheTTL time.Duration `env:"CHORUS_UCXL_CACHE_TTL" default:"5m" json:"cache_ttl" yaml:"cache_ttl"`
UseDistributed bool `env:"CHORUS_UCXL_USE_DISTRIBUTED" default:"false" json:"use_distributed" yaml:"use_distributed"`
MaxCacheSize int `env:"CHORUS_UCXL_MAX_CACHE_SIZE" default:"10000" json:"max_cache_size" yaml:"max_cache_size"`
}
type DiscoveryConfig struct {
MDNSEnabled bool `env:"BZZZ_MDNS_ENABLED" default:"true" json:"mdns_enabled" yaml:"mdns_enabled"`
DHTDiscovery bool `env:"BZZZ_DHT_DISCOVERY" default:"false" json:"dht_discovery" yaml:"dht_discovery"`
AnnounceInterval time.Duration `env:"BZZZ_ANNOUNCE_INTERVAL" default:"30s" json:"announce_interval" yaml:"announce_interval"`
ServiceName string `env:"BZZZ_SERVICE_NAME" default:"CHORUS" json:"service_name" yaml:"service_name"`
MDNSEnabled bool `env:"CHORUS_MDNS_ENABLED" default:"true" json:"mdns_enabled" yaml:"mdns_enabled"`
DHTDiscovery bool `env:"CHORUS_DHT_DISCOVERY" default:"false" json:"dht_discovery" yaml:"dht_discovery"`
AnnounceInterval time.Duration `env:"CHORUS_ANNOUNCE_INTERVAL" default:"30s" json:"announce_interval" yaml:"announce_interval"`
ServiceName string `env:"CHORUS_SERVICE_NAME" default:"CHORUS" json:"service_name" yaml:"service_name"`
}
type MonitoringConfig struct {
Enabled bool `env:"BZZZ_MONITORING_ENABLED" default:"true" json:"enabled" yaml:"enabled"`
MetricsInterval time.Duration `env:"BZZZ_METRICS_INTERVAL" default:"15s" json:"metrics_interval" yaml:"metrics_interval"`
HealthEndpoint string `env:"BZZZ_HEALTH_ENDPOINT" default:"/health" json:"health_endpoint" yaml:"health_endpoint"`
MetricsEndpoint string `env:"BZZZ_METRICS_ENDPOINT" default:"/metrics" json:"metrics_endpoint" yaml:"metrics_endpoint"`
Enabled bool `env:"CHORUS_MONITORING_ENABLED" default:"true" json:"enabled" yaml:"enabled"`
MetricsInterval time.Duration `env:"CHORUS_METRICS_INTERVAL" default:"15s" json:"metrics_interval" yaml:"metrics_interval"`
HealthEndpoint string `env:"CHORUS_HEALTH_ENDPOINT" default:"/health" json:"health_endpoint" yaml:"health_endpoint"`
MetricsEndpoint string `env:"CHORUS_METRICS_ENDPOINT" default:"/metrics" json:"metrics_endpoint" yaml:"metrics_endpoint"`
}
// LoadHybridConfig loads configuration from environment variables with defaults
@@ -60,37 +60,37 @@ func LoadHybridConfig() (*HybridConfig, error) {
// Load DHT configuration
config.DHT = HybridDHTConfig{
Backend: getEnvString("BZZZ_DHT_BACKEND", "mock"),
BootstrapNodes: getEnvStringSlice("BZZZ_DHT_BOOTSTRAP_NODES", []string{}),
FallbackOnError: getEnvBool("BZZZ_FALLBACK_ON_ERROR", true),
HealthCheckInterval: getEnvDuration("BZZZ_HEALTH_CHECK_INTERVAL", 30*time.Second),
MaxRetries: getEnvInt("BZZZ_DHT_MAX_RETRIES", 3),
RetryBackoff: getEnvDuration("BZZZ_DHT_RETRY_BACKOFF", 1*time.Second),
OperationTimeout: getEnvDuration("BZZZ_DHT_OPERATION_TIMEOUT", 10*time.Second),
Backend: getEnvString("CHORUS_DHT_BACKEND", "mock"),
BootstrapNodes: getEnvStringSlice("CHORUS_DHT_BOOTSTRAP_NODES", []string{}),
FallbackOnError: getEnvBool("CHORUS_FALLBACK_ON_ERROR", true),
HealthCheckInterval: getEnvDuration("CHORUS_HEALTH_CHECK_INTERVAL", 30*time.Second),
MaxRetries: getEnvInt("CHORUS_DHT_MAX_RETRIES", 3),
RetryBackoff: getEnvDuration("CHORUS_DHT_RETRY_BACKOFF", 1*time.Second),
OperationTimeout: getEnvDuration("CHORUS_DHT_OPERATION_TIMEOUT", 10*time.Second),
}
// Load UCXL configuration
config.UCXL = HybridUCXLConfig{
CacheEnabled: getEnvBool("BZZZ_UCXL_CACHE_ENABLED", true),
CacheTTL: getEnvDuration("BZZZ_UCXL_CACHE_TTL", 5*time.Minute),
UseDistributed: getEnvBool("BZZZ_UCXL_USE_DISTRIBUTED", false),
MaxCacheSize: getEnvInt("BZZZ_UCXL_MAX_CACHE_SIZE", 10000),
CacheEnabled: getEnvBool("CHORUS_UCXL_CACHE_ENABLED", true),
CacheTTL: getEnvDuration("CHORUS_UCXL_CACHE_TTL", 5*time.Minute),
UseDistributed: getEnvBool("CHORUS_UCXL_USE_DISTRIBUTED", false),
MaxCacheSize: getEnvInt("CHORUS_UCXL_MAX_CACHE_SIZE", 10000),
}
// Load Discovery configuration
config.Discovery = DiscoveryConfig{
MDNSEnabled: getEnvBool("BZZZ_MDNS_ENABLED", true),
DHTDiscovery: getEnvBool("BZZZ_DHT_DISCOVERY", false),
AnnounceInterval: getEnvDuration("BZZZ_ANNOUNCE_INTERVAL", 30*time.Second),
ServiceName: getEnvString("BZZZ_SERVICE_NAME", "CHORUS"),
MDNSEnabled: getEnvBool("CHORUS_MDNS_ENABLED", true),
DHTDiscovery: getEnvBool("CHORUS_DHT_DISCOVERY", false),
AnnounceInterval: getEnvDuration("CHORUS_ANNOUNCE_INTERVAL", 30*time.Second),
ServiceName: getEnvString("CHORUS_SERVICE_NAME", "CHORUS"),
}
// Load Monitoring configuration
config.Monitoring = MonitoringConfig{
Enabled: getEnvBool("BZZZ_MONITORING_ENABLED", true),
MetricsInterval: getEnvDuration("BZZZ_METRICS_INTERVAL", 15*time.Second),
HealthEndpoint: getEnvString("BZZZ_HEALTH_ENDPOINT", "/health"),
MetricsEndpoint: getEnvString("BZZZ_METRICS_ENDPOINT", "/metrics"),
Enabled: getEnvBool("CHORUS_MONITORING_ENABLED", true),
MetricsInterval: getEnvDuration("CHORUS_METRICS_INTERVAL", 15*time.Second),
HealthEndpoint: getEnvString("CHORUS_HEALTH_ENDPOINT", "/health"),
MetricsEndpoint: getEnvString("CHORUS_METRICS_ENDPOINT", "/metrics"),
}
// Validate configuration