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.
43 lines
733 B
Go
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])
|
|
}
|
|
}
|