feat(protocol): allow override of dial func

This commit is contained in:
2024-08-05 18:10:19 +02:00
parent b976bde363
commit 6842d4d88a
16 changed files with 158 additions and 18 deletions

View File

@ -3,12 +3,16 @@ package protocol
import (
"context"
"log/slog"
"net"
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
"github.com/pkg/errors"
)
type Identifier string
type DialFunc func(network string, addr string) (net.Conn, error)
type Protocol interface {
Identifier() Identifier
Available(ctx context.Context, addr string) (bool, error)
@ -17,6 +21,16 @@ type Protocol interface {
type ProtocolOptions struct {
Logger logger.Logger
Dial DialFunc
}
var DefaultDialFunc = func(network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, errors.WithStack(err)
}
return conn, nil
}
type ProtocolFactory func(opts *ProtocolOptions) (Protocol, error)
@ -40,3 +54,9 @@ func WithProtocolLogger(logger logger.Logger) ProtocolOptionFunc {
opts.Logger = logger
}
}
func WithProtocolDial(dial DialFunc) ProtocolOptionFunc {
return func(opts *ProtocolOptions) {
opts.Dial = dial
}
}