You must login to view /lordwelch/chasquid/src/commit/e8ccff749e4efc0efcb50fe37d1b360c62d329cb/docs.
The GitHub option should be usable for most people, it only links via username.

Files
chasquid/internal/nettrace/evtring.go
Alberto Bertogli 9c6661eca2 nettrace: Add a new tracing library
This commit introduces a new tracing library, that replaces
golang.org/x/net/trace, and supports (amongts other thing) nested
traces.

This is a minimal change, future patches will make use of the new
functionality.
2022-11-13 11:09:19 +00:00

43 lines
733 B
Go

package nettrace
import "time"
type evtRing struct {
evts []event
max int
pos int // Points to the latest element.
firstDrop time.Time
}
func newEvtRing(n int) *evtRing {
return &evtRing{
max: n,
pos: -1,
}
}
func (r *evtRing) Add(e *event) {
if len(r.evts) < r.max {
r.evts = append(r.evts, *e)
r.pos++
return
}
r.pos = (r.pos + 1) % r.max
// Record the first drop as the time of the first dropped message.
if r.firstDrop.IsZero() {
r.firstDrop = r.evts[r.pos].When
}
r.evts[r.pos] = *e
}
func (r *evtRing) Do(f func(e *event)) {
for i := 0; i < len(r.evts); i++ {
// Go from older to newer by starting at (r.pos+1).
pos := (r.pos + 1 + i) % len(r.evts)
f(&r.evts[pos])
}
}