From 26e2bbb477301219cab2f1e713c717464b87300f Mon Sep 17 00:00:00 2001 From: anonpenguin Date: Sat, 9 Aug 2025 12:10:15 +0300 Subject: [PATCH] feat: improve logging for peer discovery and connection monitoring --- AI_CONTEXT.md | 15 ++++++++++++--- README.md | 7 +++++++ pkg/client/client.go | 14 ++++++++++++++ pkg/client/discovery_aggressive.go | 10 ++++++++-- pkg/client/monitoring.go | 6 +++++- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/AI_CONTEXT.md b/AI_CONTEXT.md index 1640375..d026d39 100644 --- a/AI_CONTEXT.md +++ b/AI_CONTEXT.md @@ -120,11 +120,17 @@ Create a robust, decentralized network platform that enables applications to sea ``` network/ ├── cmd/ # Executables -│ ├── node/main.go # Network node (bootstrap via flag/auto) +│ ├── node/ # Network node (bootstrap via flag/auto) +│ │ ├── main.go # Entrypoint +│ │ └── configmap.go # Centralized flags/env → config mapping (flags > env > defaults) │ └── cli/main.go # Command-line interface ├── pkg/ # Core packages │ ├── client/ # Client API and implementations -│ │ ├── client.go # Main client implementation +│ │ ├── client.go # Main client (now slimmed) +│ │ ├── connect_bootstrap.go # Bootstrap helpers +│ │ ├── discovery_aggressive.go # Generic aggressive discovery +│ │ ├── monitoring.go # Connection monitoring +│ │ ├── pubsub_bridge.go # PubSub adapter bridge │ │ ├── implementations.go # Database, storage, network implementations │ │ └── interface.go # Public API interfaces │ ├── config/ # Configuration management @@ -145,7 +151,10 @@ network/ │ └── storage/ # Distributed storage │ ├── client.go # Storage client │ ├── protocol.go # Storage protocol -│ └── service.go # Storage service +│ ├── service.go # Service (struct/ctor/Close) +│ ├── rqlite_init.go # Schema initialization +│ ├── stream_handler.go # Stream handling +│ └── kv_ops.go # KV operation handlers ├── anchat/ # Example chat application │ ├── cmd/cli/main.go # Chat CLI │ └── pkg/ diff --git a/README.md b/README.md index e4a41e0..9f84c2e 100644 --- a/README.md +++ b/README.md @@ -460,6 +460,11 @@ Precedence: CLI flags > Environment variables > Code defaults. Set any of the fo - LOG_FORMAT: "json" | "console" - LOG_OUTPUT_FILE: path (empty = stdout) +### Centralized Flag/Env Mapping + +Flag and environment variable mapping is centralized in `cmd/node/configmap.go` via `MapFlagsAndEnvToConfig`. +This enforces precedence (flags > env > defaults) consistently across the node startup path. + ## CLI Commands The CLI can still accept `--bootstrap ` to override discovery when needed. @@ -511,6 +516,8 @@ The CLI can still accept `--bootstrap ` to override discovery when ne network/ ├── cmd/ │ ├── node/ # Network node (bootstrap via flag) +│ │ ├── main.go # Entrypoint +│ │ └── configmap.go # Centralized flags/env → config mapping │ └── cli/ # Command-line interface ├── pkg/ │ ├── client/ # Client library diff --git a/pkg/client/client.go b/pkg/client/client.go index 4e877ff..60a27ce 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -120,6 +120,16 @@ func (c *Client) Connect() error { c.host = h + // Log host identity and listen addresses + addrs := c.host.Addrs() + addrStrs := make([]string, 0, len(addrs)) + for _, a := range addrs { addrStrs = append(addrStrs, a.String()) } + c.logger.Info("LibP2P host created", + zap.String("peer_id", c.host.ID().String()), + zap.Int("listen_addr_count", len(addrStrs)), + zap.Strings("listen_addrs", addrStrs), + ) + // Create LibP2P PubSub with enhanced discovery for Anchat var ps *libp2ppubsub.PubSub if c.config.AppName == "anchat" { @@ -219,6 +229,8 @@ func (c *Client) Connect() error { c.connected = true + c.logger.Info("Client connected", zap.String("namespace", c.getAppNamespace())) + return nil } @@ -260,6 +272,8 @@ func (c *Client) Disconnect() error { c.connected = false + c.logger.Info("Client disconnected") + return nil } diff --git a/pkg/client/discovery_aggressive.go b/pkg/client/discovery_aggressive.go index 23648b6..a1df03a 100644 --- a/pkg/client/discovery_aggressive.go +++ b/pkg/client/discovery_aggressive.go @@ -17,8 +17,10 @@ func (c *Client) startAggressivePeerDiscovery() { if !c.isConnected() { return } connectedPeers := c.host.Network().Peers() + routingCount := 0 if c.dht != nil { routingPeers := c.dht.RoutingTable().ListPeers() + routingCount = len(routingPeers) for _, pid := range routingPeers { if pid == c.host.ID() { continue } already := false @@ -28,7 +30,7 @@ func (c *Client) startAggressivePeerDiscovery() { pi := c.host.Peerstore().PeerInfo(pid) if len(pi.Addrs) > 0 { if err := c.host.Connect(ctx, pi); err == nil { - c.logger.Debug("Connected to discovered peer", zap.String("peer", pid.String()[:8]+"...")) + c.logger.Debug("Connected to DHT peer", zap.String("peer", pid.String())) } } cancel() @@ -36,7 +38,11 @@ func (c *Client) startAggressivePeerDiscovery() { } } if i%10 == 0 { - c.logger.Debug("Peer discovery status", zap.Int("iteration", i+1), zap.Int("connected_peers", len(connectedPeers))) + c.logger.Debug("Peer discovery status", + zap.Int("iteration", i+1), + zap.Int("connected_peers", len(connectedPeers)), + zap.Int("routing_peers", routingCount), + ) } } } diff --git a/pkg/client/monitoring.go b/pkg/client/monitoring.go index 46f749b..209e168 100644 --- a/pkg/client/monitoring.go +++ b/pkg/client/monitoring.go @@ -1,6 +1,8 @@ package client -import "time" +import ( + "time" +) // startConnectionMonitoring monitors connection health and logs status func (c *Client) startConnectionMonitoring() { @@ -8,8 +10,10 @@ func (c *Client) startConnectionMonitoring() { ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() + c.logger.Debug("Connection monitoring started") for range ticker.C { if !c.isConnected() { + c.logger.Debug("Connection monitoring stopped: client disconnected") return }