58adb68b2c
* refactor: move tracex outside of engine/netx Consistently with https://github.com/ooni/probe/issues/2121 and https://github.com/ooni/probe/issues/2115, we can now move tracex outside of engine/netx. The main reason why this makes sense now is that the package is now changed significantly from the one that we imported from ooni/probe-engine. We have improved its implementation, which had not been touched significantly for quite some time, and converted it to unit testing. I will document tomorrow some extra work I'd like to do with this package but likely could not do $soon. * go fmt * regen tutorials
36 lines
707 B
Go
36 lines
707 B
Go
package tracex
|
|
|
|
//
|
|
// Saver implementation
|
|
//
|
|
|
|
import "sync"
|
|
|
|
// The Saver saves a trace. The zero value of this type
|
|
// is valid and can be used without initialization.
|
|
type Saver struct {
|
|
// ops contains the saved events.
|
|
ops []Event
|
|
|
|
// mu provides mutual exclusion.
|
|
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)
|
|
}
|