refactor(netxlite): better integration with tracex (#774)
Rather than passing functions to construct complex objects such as Dialer and QUICDialer, pass interface implementations. Ensure that a nil implementation does not cause harm. Make Saver implement the correct interface either directly or indirectly. We need to implement the correct interface indirectly for TCP conns (or connected UDP sockets) because we have two distinct use cases inside netx: observing just the connect event and observing just the I/O events. With this change, the construction of composed Dialers and QUICDialers is greatly simplified and more obvious. Part of https://github.com/ooni/probe/issues/2121
This commit is contained in:
parent
f4f3ed7c42
commit
7e0b47311d
10 changed files with 119 additions and 58 deletions
|
|
@ -22,6 +22,31 @@ type SaverDialer struct {
|
|||
Saver *Saver
|
||||
}
|
||||
|
||||
// NewConnectObserver returns a DialerWrapper that observes the
|
||||
// connect event. This function will return nil, which is a valid
|
||||
// DialerWrapper for netxlite.WrapDialer, if Saver is nil.
|
||||
func (s *Saver) NewConnectObserver() model.DialerWrapper {
|
||||
if s == nil {
|
||||
return nil // valid DialerWrapper according to netxlite's docs
|
||||
}
|
||||
return &saverDialerWrapper{
|
||||
saver: s,
|
||||
}
|
||||
}
|
||||
|
||||
type saverDialerWrapper struct {
|
||||
saver *Saver
|
||||
}
|
||||
|
||||
var _ model.DialerWrapper = &saverDialerWrapper{}
|
||||
|
||||
func (w *saverDialerWrapper) WrapDialer(d model.Dialer) model.Dialer {
|
||||
return &SaverDialer{
|
||||
Dialer: d,
|
||||
Saver: w.saver,
|
||||
}
|
||||
}
|
||||
|
||||
// DialContext implements Dialer.DialContext
|
||||
func (d *SaverDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
start := time.Now()
|
||||
|
|
@ -52,6 +77,31 @@ type SaverConnDialer struct {
|
|||
Saver *Saver
|
||||
}
|
||||
|
||||
// NewReadWriteObserver returns a DialerWrapper that observes the
|
||||
// I/O events. This function will return nil, which is a valid
|
||||
// DialerWrapper for netxlite.WrapDialer, if Saver is nil.
|
||||
func (s *Saver) NewReadWriteObserver() model.DialerWrapper {
|
||||
if s == nil {
|
||||
return nil // valid DialerWrapper according to netxlite's docs
|
||||
}
|
||||
return &saverReadWriteWrapper{
|
||||
saver: s,
|
||||
}
|
||||
}
|
||||
|
||||
type saverReadWriteWrapper struct {
|
||||
saver *Saver
|
||||
}
|
||||
|
||||
var _ model.DialerWrapper = &saverReadWriteWrapper{}
|
||||
|
||||
func (w *saverReadWriteWrapper) WrapDialer(d model.Dialer) model.Dialer {
|
||||
return &SaverConnDialer{
|
||||
Dialer: d,
|
||||
Saver: w.saver,
|
||||
}
|
||||
}
|
||||
|
||||
// DialContext implements Dialer.DialContext
|
||||
func (d *SaverConnDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
conn, err := d.Dialer.DialContext(ctx, network, address)
|
||||
|
|
|
|||
Loading…
Reference in a new issue