Track per-side transport bytes for source and target · Entire

Track per-side transport bytes for source and target

d2d1bc0→main·

Soph·2mo ago·2 files·+108 added/-12 removed

Wraps both request and response bodies in the existing countingRoundTripper so byte counters update as data streams, not just at round-trip completion. Per-side counters keyed by transport label ("source", "target") feed an atomic byte total plus an elapsed nanos sample on each Stats snapshot.

Source totals are dominated by upload-pack response bytes (what we got from the source); target totals by receive-pack request bytes (what we pushed to the target). Counters are tracked unconditionally so a future live progress reader does not need ShowStats=true.

Sessions

42b26e1c2584View transcript

[?
can you rebase soph/progress-indicators onto soph/smart-subdivisionClaude Code·Opus 4.7[1m]·1 step](/content/gh/entireio/git-sync/session/3ee1ca7a-a436-44c1-906a-a912c6d33f96#timeline-42b26e1c2584/index.html)

Changes

2

5 unmodified lines

6
7
8
9
10
11
12
13
9 unmodified lines

23
24
25
26
27
28
29
30
31
32
33
34
35
36
26
27
37
38
39
40
41
42
30
43
44
45
46
47
48
49
50
51
52
32
33
34
53
54
55
56
57
58
59
60
61
38
62
63
64
65
66
67
68
69
70
17 unmodified lines

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
64
125
126
127
128
129
130
131
132
133
134
135
136
137
73
138
139
140
141
142
1 unmodified line

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
14 unmodified lines

173
174
175
176
177
178
179
5 unmodified lines

185
186
187
188
189
190
191
192
193
117
194
195
196
197
198
199
200
201
202

5 unmodified lines

"net/http"
    "strings"
    "sync"
    "sync/atomic"
    "time"

"entire.io/entire/git-sync/internal/gitproto"
)
9 unmodified lines

Commands      int    `json:"commands"`
}

// SideBytes reports cumulative bytes transferred at one transport label
// ("source" or "target"). Source is dominated by upload-pack response
// bytes (download), target is dominated by receive-pack request bytes
// (upload), so this is a useful "data moved on this side" number for both.
type SideBytes struct {
    Label string `json:"label"`
    Bytes int64  `json:"bytes"`
}

// Stats holds the collected transfer statistics.
type Stats struct {
    Enabled bool                     `json:"enabled"`
    Items   map[string]*ServiceStats `json:"items"`
    Enabled      bool                     `json:"enabled"`
    Items        map[string]*ServiceStats `json:"items"`
    Sides        []SideBytes              `json:"sides,omitempty"`
    ElapsedNanos int64                    `json:"elapsedNanos,omitempty"`
}

// statsCollector is a concurrency-safe stats collector (issue #8).
// sideCounter holds atomic byte counters for one transport label.
// Updated from the request and response body wrappers without holding
// the collector mutex, so a progress reader can sample at high
// frequency without contending with active transfers.
type sideCounter struct {
    bytes atomic.Int64
}

// statsCollector is a concurrency-safe stats collector.
type statsCollector struct {
    enabled bool
    mu      sync.Mutex
    items   map[string]*ServiceStats
    enabled   bool
    startedAt time.Time
    mu        sync.Mutex
    items     map[string]*ServiceStats
    sidesMu   sync.RWMutex
    sides     map[string]*sideCounter
}

func newStats(enabled bool) *statsCollector {
    return &statsCollector{enabled: enabled, items: map[string]*ServiceStats{}}
    return &statsCollector{
        enabled:   enabled,
        startedAt: time.Now(),
        items:     map[string]*ServiceStats{},
        sides:     map[string]*sideCounter{},
    }
}

func (s *statsCollector) ensure(name string) *ServiceStats {
17 unmodified lines

item.ResponseBytes += responseBytes
}

// side returns the per-label counter, creating it lazily on first use.
// Counters are tracked unconditionally (independent of ShowStats) so the
// live progress ticker works without forcing --stats.
func (s *statsCollector) side(label string) *sideCounter {
    s.sidesMu.RLock()
    if sc, ok := s.sides[label]; ok {
        s.sidesMu.RUnlock()
        return sc
    }
    s.sidesMu.RUnlock()

s.sidesMu.Lock()
    defer s.sidesMu.Unlock()
    if sc, ok := s.sides[label]; ok {
        return sc
    }
    sc := &sideCounter{}
    s.sides[label] = sc
    return sc
}

// liveSides returns a snapshot of per-side byte totals for live rendering.
func (s *statsCollector) liveSides() []SideBytes {
    s.sidesMu.RLock()
    defer s.sidesMu.RUnlock()
    out := make([]SideBytes, 0, len(s.sides))
    for label, sc := range s.sides {
        out = append(out, SideBytes{Label: label, Bytes: sc.bytes.Load()})
    }
    return out
}

func (s *statsCollector) snapshot() Stats {
    s.mu.Lock()
    defer s.mu.Unlock()
    out := Stats{Enabled: s.enabled, Items: make(map[string]*ServiceStats, len(s.items))}
    for key, item := range s.items {
        copyItem := *item
        out.Items[key] = &copyItem
    }
    s.mu.Unlock()
    if s.enabled {
        out.Sides = s.liveSides()
        out.ElapsedNanos = time.Since(s.startedAt).Nanoseconds()
    }
    return out
}

// countingRoundTripper wraps an HTTP transport to record transfer stats.
// countingRoundTripper wraps an HTTP transport to record transfer stats
// and feed per-side byte counters consumed by the live progress ticker.
type countingRoundTripper struct {
    base  http.RoundTripper
    label string
}

func (rt *countingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    side := rt.stats.side(rt.label)

// Wrap the request body so upload bytes (receive-pack POSTs) feed
    // the per-side counter as they stream out, not just on round-trip
    // completion. Skipped when there is no body (most GETs).
    if req.Body != nil {
        req.Body = &countingReadCloser{ReadCloser: req.Body, side: side}
    }

res, err := rt.base.RoundTrip(req)
    if err != nil {
        return nil, fmt.Errorf("round trip: %w", err)
    }
14 unmodified lines

res.Body = &countingReadCloser{
        ReadCloser: res.Body,
        side:       side,
        onClose: func(n int64) {
            rt.stats.recordRoundTrip(name, requestBytes, n)
        },
    }

return res, nil
}

type countingReadCloser struct {
    io.ReadCloser

n       int64
    side    *sideCounter
    onClose func(int64)
}

func (c *countingReadCloser) Read(p []byte) (int, error) {
    n, err := c.ReadCloser.Read(p)
    c.n += int64(n)
    if n > 0 {
        c.n += int64(n)
        if c.side != nil {
            c.side.bytes.Add(int64(n))
        }
    }
    return n, err //nolint:wrapcheck // Read must preserve io.EOF for io.Reader contract
}

Minternal/syncer/stats.go+92/-10

51 unmodified lines

52
53
54
55
56
57
58
59
60
56
57
61
62
63
64
65
66
67
126 unmodified lines

194
195
196
197
198
199
200
201
202
203
204
205
206

51 unmodified lines

Commands      int    `json:"commands"`
}

type SideBytes struct {
    Label string `json:"label"`
    Bytes int64  `json:"bytes"`
}

type Stats struct {
    Enabled bool                     `json:"enabled"`
    Items   map[string]*ServiceStats `json:"items"`
    Enabled      bool                     `json:"enabled"`
    Items        map[string]*ServiceStats `json:"items"`
    Sides        []SideBytes              `json:"sides,omitempty"`
    ElapsedNanos int64                    `json:"elapsedNanos,omitempty"`
}

type Measurement struct {
126 unmodified lines

Commands:      copyItem.Commands,
        }
    }
    if len(stats.Sides) > 0 {
        out.Sides = make([]SideBytes, 0, len(stats.Sides))
        for _, side := range stats.Sides {
            out.Sides = append(out.Sides, SideBytes{Label: side.Label, Bytes: side.Bytes})
        }
    }
    out.ElapsedNanos = stats.ElapsedNanos
    return out
}
}