feat: improve logging for peer discovery and connection monitoring

This commit is contained in:
anonpenguin 2025-08-09 12:10:15 +03:00
parent 2daa86bd3c
commit 26e2bbb477
5 changed files with 46 additions and 6 deletions

View File

@ -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/

View File

@ -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 <multiaddr>` to override discovery when needed.
@ -511,6 +516,8 @@ The CLI can still accept `--bootstrap <multiaddr>` 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

View File

@ -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
}

View File

@ -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),
)
}
}
}

View File

@ -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
}