log what arrives: one line per sync and per minute of gossip, every listing at debug; --log-level
A sync with a peer logs one info line when it brought something new (new and updated counts, per region). Listings that arrive by gossip are summed up once a minute. At debug level every stored listing gets a line with its id, region, outcome and the peer it came from. A duplicate is not news and logs nothing. --log-level takes debug, info, warn or error. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
// defaultIntakeSummary is how often a node sums up the listings that reached
|
||||
// it by gossip.
|
||||
const defaultIntakeSummary = time.Minute
|
||||
|
||||
// tally counts the listings a node newly stored, so that one line can say so
|
||||
// instead of one line per listing: a catch-up can bring in hundreds.
|
||||
type tally struct {
|
||||
mu sync.Mutex
|
||||
inserted int
|
||||
replaced int
|
||||
regions map[string]int
|
||||
}
|
||||
|
||||
func (t *tally) add(region string, out store.Outcome) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if t.regions == nil {
|
||||
t.regions = map[string]int{}
|
||||
}
|
||||
t.regions[region]++
|
||||
if out == store.Replaced {
|
||||
t.replaced++
|
||||
} else {
|
||||
t.inserted++
|
||||
}
|
||||
}
|
||||
|
||||
// log writes one info line with what was counted since the last call, and
|
||||
// nothing when nothing new arrived, so a quiet node stays quiet.
|
||||
func (t *tally) log(l *slog.Logger, msg string, attrs ...any) {
|
||||
t.mu.Lock()
|
||||
inserted, replaced, regions := t.inserted, t.replaced, t.regions
|
||||
t.inserted, t.replaced, t.regions = 0, 0, nil
|
||||
t.mu.Unlock()
|
||||
if inserted+replaced == 0 {
|
||||
return
|
||||
}
|
||||
cells := make([]string, 0, len(regions))
|
||||
for region, n := range regions {
|
||||
cells = append(cells, fmt.Sprintf("%s=%d", region, n))
|
||||
}
|
||||
slices.Sort(cells)
|
||||
l.Info(msg, append(attrs, "new", inserted, "updated", replaced, "regions", strings.Join(cells, " "))...)
|
||||
}
|
||||
|
||||
type tallyKey struct{}
|
||||
|
||||
// withTally makes the listings stored under ctx count into t (one sync).
|
||||
func withTally(ctx context.Context, t *tally) context.Context {
|
||||
return context.WithValue(ctx, tallyKey{}, t)
|
||||
}
|
||||
|
||||
// noteListing records a listing that was just stored. Debug level says what
|
||||
// it was and where it came from; the count is summed up by whoever owns the
|
||||
// tally: a sync with one peer, or the gossip summary. A duplicate or older
|
||||
// version (Ignored) is not news and is not counted.
|
||||
func (n *Node) noteListing(ctx context.Context, l *listing.Listing, out store.Outcome, via string, from peer.ID) {
|
||||
if out == store.Ignored {
|
||||
return
|
||||
}
|
||||
region := listing.Region(l.Geohash)
|
||||
n.log.Debug("listing stored", "id", l.ID, "author", l.Author[:12], "region", region,
|
||||
"kind", l.Kind, "tombstone", l.Tombstone, "outcome", out.String(), "via", via, "from", from)
|
||||
t, ok := ctx.Value(tallyKey{}).(*tally)
|
||||
if !ok {
|
||||
t = &n.gossipIn
|
||||
}
|
||||
t.add(region, out)
|
||||
}
|
||||
|
||||
// summarizeIntake logs the listings that reached this node by gossip, once per
|
||||
// interval, and once more when the node stops.
|
||||
func (n *Node) summarizeIntake() {
|
||||
defer n.wg.Done()
|
||||
t := time.NewTicker(n.cfg.IntakeSummaryEvery)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-n.ctx.Done():
|
||||
n.gossipIn.log(n.log, "received listings by gossip")
|
||||
return
|
||||
case <-t.C:
|
||||
n.gossipIn.log(n.log, "received listings by gossip")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user