From dfd1862cfd0d770ee3d30a2fe72c64a8c6804cb3 Mon Sep 17 00:00:00 2001 From: anonpenguin Date: Tue, 12 Aug 2025 08:04:58 +0300 Subject: [PATCH] Use default peers during local development The change keeps default peers in local dev mode instead of clearing them, while adding a global pubsub logger and updating bootstrap peer config. --- pkg/client/interface.go | 82 +++++++++++++++++++------------------- pkg/constants/bootstrap.go | 10 +++-- pkg/pubsub/logging.go | 29 +++++++++----- 3 files changed, 67 insertions(+), 54 deletions(-) diff --git a/pkg/client/interface.go b/pkg/client/interface.go index dd17c98..23e8774 100644 --- a/pkg/client/interface.go +++ b/pkg/client/interface.go @@ -123,55 +123,55 @@ type HealthStatus struct { // ClientConfig represents configuration for network clients type ClientConfig struct { - AppName string `json:"app_name"` - DatabaseName string `json:"database_name"` - BootstrapPeers []string `json:"bootstrap_peers"` - DatabaseEndpoints []string `json:"database_endpoints"` - ConnectTimeout time.Duration `json:"connect_timeout"` - RetryAttempts int `json:"retry_attempts"` - RetryDelay time.Duration `json:"retry_delay"` - QuietMode bool `json:"quiet_mode"` // Suppress debug/info logs + AppName string `json:"app_name"` + DatabaseName string `json:"database_name"` + BootstrapPeers []string `json:"bootstrap_peers"` + DatabaseEndpoints []string `json:"database_endpoints"` + ConnectTimeout time.Duration `json:"connect_timeout"` + RetryAttempts int `json:"retry_attempts"` + RetryDelay time.Duration `json:"retry_delay"` + QuietMode bool `json:"quiet_mode"` // Suppress debug/info logs } // DefaultClientConfig returns a default client configuration func DefaultClientConfig(appName string) *ClientConfig { - // Base defaults - peers := []string{} - endpoints := DefaultDatabaseEndpoints() + // Base defaults + peers := DefaultBootstrapPeers() + endpoints := DefaultDatabaseEndpoints() - // Development local-only override via env - if isTruthyEnv("NETWORK_DEV_LOCAL") { - port := 5001 - if v := os.Getenv("RQLITE_PORT"); v != "" { - if n, err := strconv.Atoi(v); err == nil && n > 0 { port = n } - } - endpoints = []string{fmt.Sprintf("http://127.0.0.1:%d", port)} - if ma := os.Getenv("LOCAL_BOOTSTRAP_MULTIADDR"); ma != "" { - peers = []string{ma} - } else { - // Leave peers empty if no local bootstrap multiaddr provided - peers = []string{} - } - } + // Development local-only override via env + if isTruthyEnv("NETWORK_DEV_LOCAL") { + port := 5001 + if v := os.Getenv("RQLITE_PORT"); v != "" { + if n, err := strconv.Atoi(v); err == nil && n > 0 { + port = n + } + } + endpoints = []string{fmt.Sprintf("http://127.0.0.1:%d", port)} + if ma := os.Getenv("LOCAL_BOOTSTRAP_MULTIADDR"); ma != "" { + peers = []string{ma} + } + // else: keep the peers from DefaultBootstrapPeers() which handles NETWORK_DEV_LOCAL appropriately + } - return &ClientConfig{ - AppName: appName, - DatabaseName: fmt.Sprintf("%s_db", appName), - BootstrapPeers: peers, - DatabaseEndpoints: endpoints, - ConnectTimeout: time.Second * 30, - RetryAttempts: 3, - RetryDelay: time.Second * 5, - } + return &ClientConfig{ + AppName: appName, + DatabaseName: fmt.Sprintf("%s_db", appName), + BootstrapPeers: peers, + DatabaseEndpoints: endpoints, + ConnectTimeout: time.Second * 30, + RetryAttempts: 3, + RetryDelay: time.Second * 5, + } } // isTruthyEnv returns true if the env var is set to a common truthy value. func isTruthyEnv(key string) bool { - v := os.Getenv(key) - switch v { - case "1", "true", "TRUE", "True", "yes", "YES", "on", "ON": - return true - default: - return false - } + v := os.Getenv(key) + switch v { + case "1", "true", "TRUE", "True", "yes", "YES", "on", "ON": + return true + default: + return false + } } diff --git a/pkg/constants/bootstrap.go b/pkg/constants/bootstrap.go index 84b1ed3..f9824ca 100644 --- a/pkg/constants/bootstrap.go +++ b/pkg/constants/bootstrap.go @@ -33,12 +33,14 @@ func init() { func setDefaultBootstrapConfig() { // Check if we're in production environment BootstrapPeerIDs = []string{ - "12D3KooWNxt9bNvqftdqXg98JcUHreGxedWSZRUbyqXJ6CW7GaD4", - "12D3KooWGbdnA22bN24X2gyY1o9jozwTBq9wbfvwtJ7G4XQ9JgFm", + // "12D3KooWNxt9bNvqftdqXg98JcUHreGxedWSZRUbyqXJ6CW7GaD4", + // "12D3KooWGbdnA22bN24X2gyY1o9jozwTBq9wbfvwtJ7G4XQ9JgFm", + "12D3KooWDL6LSjwwP5FwboV9JaTZzuxr8EhjbcZGFfnyFMDt1UDx", } BootstrapAddresses = []string{ - "/ip4/57.129.81.31/tcp/4001/p2p/12D3KooWNxt9bNvqftdqXg98JcUHreGxedWSZRUbyqXJ6CW7GaD4", - "/ip4/38.242.250.186/tcp/4001/p2p/12D3KooWGbdnA22bN24X2gyY1o9jozwTBq9wbfvwtJ7G4XQ9JgFm", + // "/ip4/57.129.81.31/tcp/4001/p2p/12D3KooWNxt9bNvqftdqXg98JcUHreGxedWSZRUbyqXJ6CW7GaD4", + // "/ip4/38.242.250.186/tcp/4001/p2p/12D3KooWGbdnA22bN24X2gyY1o9jozwTBq9wbfvwtJ7G4XQ9JgFm", + "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWDL6LSjwwP5FwboV9JaTZzuxr8EhjbcZGFfnyFMDt1UDx", } BootstrapPort = 4001 diff --git a/pkg/pubsub/logging.go b/pkg/pubsub/logging.go index d221994..c95a1c6 100644 --- a/pkg/pubsub/logging.go +++ b/pkg/pubsub/logging.go @@ -1,17 +1,28 @@ package pubsub -import "go.uber.org/zap" +import ( + "fmt" + "go.uber.org/zap" +) + +// Logf is a package-level logger function used by pubsub internals. +// By default it is a no-op to avoid polluting stdout; applications can +// assign it (e.g., to a UI-backed logger) to surface logs as needed. +var Logf = func(format string, args ...interface{}) { _ = fmt.Sprintf(format, args...) } + +// SetLogFunc allows applications to provide a custom logger sink. +func SetLogFunc(f func(string, ...interface{})) { if f != nil { Logf = f } } // newPubSubLogger creates a zap.Logger for pubsub components. // Quiet mode can be handled by callers by using production config externally; // here we default to development logger for richer diagnostics during dev. func newPubSubLogger(quiet bool) (*zap.Logger, error) { - if quiet { - cfg := zap.NewProductionConfig() - cfg.Level = zap.NewAtomicLevelAt(zap.WarnLevel) - cfg.DisableCaller = true - cfg.DisableStacktrace = true - return cfg.Build() - } - return zap.NewDevelopment() + if quiet { + cfg := zap.NewProductionConfig() + cfg.Level = zap.NewAtomicLevelAt(zap.WarnLevel) + cfg.DisableCaller = true + cfg.DisableStacktrace = true + return cfg.Build() + } + return zap.NewDevelopment() }