bbcd2e2280
This diff creates a new package under netx called tracex that contains everything we need to perform measurements using events tracing and postprocessing (which is the technique with which we implement most network experiments). The general idea here is to (1) create a unique package out of all of these packages; (2) clean up the code a bit (improve tests, docs, apply more recent code patterns); (3) move the resulting code as a toplevel package inside of internal. Once this is done, netx can be further refactored to avoid subpackages and we can search for more code to salvage/refactor. See https://github.com/ooni/probe/issues/2121
28 lines
526 B
Go
28 lines
526 B
Go
package tracex
|
|
|
|
import "sync"
|
|
|
|
// The Saver saves a trace
|
|
type Saver struct {
|
|
ops []Event
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// Read reads and returns events inside the trace. It advances
|
|
// the read pointer so you won't see such events again.
|
|
func (s *Saver) Read() []Event {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
v := s.ops
|
|
s.ops = nil
|
|
return v
|
|
}
|
|
|
|
// Write adds the given event to the trace. A subsequent call
|
|
// to Read will read this event.
|
|
func (s *Saver) Write(ev Event) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.ops = append(s.ops, ev)
|
|
}
|