feat(v2): allow override of dial func
This commit is contained in:
parent
b976bde363
commit
4d7741c539
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module forge.cadoles.com/cadoles/go-emlid
|
|||
go 1.22.5
|
||||
|
||||
require (
|
||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
|
||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20240805155359-f54949ba3a46
|
||||
github.com/Masterminds/semver/v3 v3.2.1
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/grandcat/zeroconf v1.0.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
|||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 h1:o3G5+9RjczCK1xAYFaRMknk1kY9Ule6PNfiW6N6hEpg=
|
||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0=
|
||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20240805155359-f54949ba3a46 h1:vLTYHA4+pYeI9mZvCMrc29AmnNjeGEpEG1mTwtCOoDI=
|
||||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20240805155359-f54949ba3a46/go.mod h1:bT+HWia42VRX1TzTUlEM645tPJEOtsEdzlKBiEqVchY=
|
||||
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
||||
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
||||
|
|
|
@ -3,12 +3,15 @@ package protocol
|
|||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net"
|
||||
|
||||
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
|
||||
)
|
||||
|
||||
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 +20,7 @@ type Protocol interface {
|
|||
|
||||
type ProtocolOptions struct {
|
||||
Logger logger.Logger
|
||||
Dial DialFunc
|
||||
}
|
||||
|
||||
type ProtocolFactory func(opts *ProtocolOptions) (Protocol, error)
|
||||
|
|
|
@ -49,7 +49,7 @@ func (o *Operations) Connect(ctx context.Context) error {
|
|||
o.client.Close()
|
||||
}
|
||||
|
||||
endpoint, err := socketio.EndpointFromHAddr(o.addr)
|
||||
endpoint, err := socketio.EndpointFromAddr(o.addr)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
|
|
@ -20,7 +20,9 @@ func (o *Operations) GetJSON(path string, dst any) error {
|
|||
var res *http.Response
|
||||
|
||||
url := o.getURL(path)
|
||||
res, err := http.Get(url)
|
||||
client := o.getHTTPClient()
|
||||
|
||||
res, err := client.Get(url)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
@ -50,6 +52,7 @@ func (o *Operations) GetJSON(path string, dst any) error {
|
|||
}
|
||||
|
||||
func (o *Operations) PostJSON(path string, data any, dst any) error {
|
||||
|
||||
var res *http.Response
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
@ -60,7 +63,9 @@ func (o *Operations) PostJSON(path string, data any, dst any) error {
|
|||
}
|
||||
|
||||
url := o.getURL(path)
|
||||
res, err := http.Post(url, "application/json", &buf)
|
||||
client := o.getHTTPClient()
|
||||
|
||||
res, err := client.Post(url, "application/json", &buf)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
@ -89,6 +94,18 @@ func (o *Operations) PostJSON(path string, data any, dst any) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o *Operations) getHTTPClient() *http.Client {
|
||||
o.getClientOnce.Do(func() {
|
||||
o.httpClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: o.dial,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return o.httpClient
|
||||
}
|
||||
|
||||
func (o *Operations) PostBaseCoordinates(ctx context.Context, base *model.Base) (*model.Base, error) {
|
||||
var updated model.Base
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package v2
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
|
||||
|
@ -17,6 +18,10 @@ type Operations struct {
|
|||
client *socketio.Client
|
||||
mutex sync.RWMutex
|
||||
logger logger.Logger
|
||||
dial protocol.DialFunc
|
||||
|
||||
getClientOnce sync.Once
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// Reboot implements protocol.Operations.
|
||||
|
@ -161,12 +166,12 @@ func (o *Operations) Connect(ctx context.Context) error {
|
|||
o.client.Close()
|
||||
}
|
||||
|
||||
endpoint, err := socketio.EndpointFromHAddr(o.addr)
|
||||
endpoint, err := socketio.EndpointFromAddr(o.addr)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
client := socketio.NewClient(endpoint)
|
||||
client := socketio.NewClient(endpoint, socketio.WithDialFunc(socketio.DialFunc(o.dial)))
|
||||
|
||||
if err := client.Connect(); err != nil {
|
||||
return errors.WithStack(err)
|
||||
|
|
|
@ -15,6 +15,7 @@ const compatibleVersionConstraint = ">= 32"
|
|||
|
||||
type Protocol struct {
|
||||
logger logger.Logger
|
||||
dial protocol.DialFunc
|
||||
}
|
||||
|
||||
// Available implements protocol.Protocol.
|
||||
|
@ -50,7 +51,11 @@ func (p *Protocol) Identifier() protocol.Identifier {
|
|||
|
||||
// Operations implements protocol.Protocol.
|
||||
func (p *Protocol) Operations(addr string) protocol.Operations {
|
||||
return &Operations{addr: addr, logger: p.logger}
|
||||
return &Operations{
|
||||
dial: p.dial,
|
||||
addr: addr,
|
||||
logger: p.logger,
|
||||
}
|
||||
}
|
||||
|
||||
var _ protocol.Protocol = &Protocol{}
|
||||
|
|
|
@ -32,12 +32,15 @@ func (c *Client) Connect() error {
|
|||
|
||||
wg.Add(1)
|
||||
|
||||
transport := &transport.WebsocketTransport{
|
||||
PingInterval: c.opts.PingInterval,
|
||||
PingTimeout: c.opts.PingTimeout,
|
||||
ReceiveTimeout: c.opts.ReceiveTimeout,
|
||||
SendTimeout: c.opts.SendTimeout,
|
||||
BufferSize: c.opts.BufferSize,
|
||||
transport := &Transport{
|
||||
dial: c.opts.DialFunc,
|
||||
ws: &transport.WebsocketTransport{
|
||||
PingInterval: c.opts.PingInterval,
|
||||
PingTimeout: c.opts.PingTimeout,
|
||||
ReceiveTimeout: c.opts.ReceiveTimeout,
|
||||
SendTimeout: c.opts.SendTimeout,
|
||||
BufferSize: c.opts.BufferSize,
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := gosocketio.Dial(c.endpoint, transport)
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func EndpointFromHAddr(addr string) (string, error) {
|
||||
func EndpointFromAddr(addr string) (string, error) {
|
||||
host, rawPort, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
var addrErr *net.AddrError
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package socketio
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type DialFunc func(network, addr string) (net.Conn, error)
|
||||
|
||||
type Options struct {
|
||||
PingInterval time.Duration
|
||||
|
@ -8,6 +15,7 @@ type Options struct {
|
|||
ReceiveTimeout time.Duration
|
||||
SendTimeout time.Duration
|
||||
BufferSize int
|
||||
DialFunc DialFunc
|
||||
}
|
||||
|
||||
type OptionFunc func(opts *Options)
|
||||
|
@ -19,6 +27,7 @@ func NewOptions(funcs ...OptionFunc) *Options {
|
|||
ReceiveTimeout: 60 * time.Second,
|
||||
SendTimeout: 60 * time.Second,
|
||||
BufferSize: 1024 * 32,
|
||||
DialFunc: DefaultDialFunc,
|
||||
}
|
||||
for _, fn := range funcs {
|
||||
fn(opts)
|
||||
|
@ -60,3 +69,19 @@ func WithBufferSize(size int) OptionFunc {
|
|||
opts.BufferSize = size
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// WithDialFunc configures the client to use the given dial func
|
||||
func WithDialFunc(dial DialFunc) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.DialFunc = dial
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package socketio
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio/transport"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type Transport struct {
|
||||
dial DialFunc
|
||||
ws *transport.WebsocketTransport
|
||||
}
|
||||
|
||||
// Connect implements transport.Transport.
|
||||
func (t *Transport) Connect(url string) (conn transport.Connection, err error) {
|
||||
if t.dial == nil {
|
||||
return t.ws.Connect(url)
|
||||
} else {
|
||||
dialer := websocket.Dialer{
|
||||
NetDial: func(network, addr string) (net.Conn, error) {
|
||||
return t.dial(network, addr)
|
||||
},
|
||||
}
|
||||
|
||||
socket, _, err := dialer.Dial(url, t.ws.RequestHeader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return transport.NewWebsocketConnection(socket, t.ws), nil
|
||||
}
|
||||
}
|
||||
|
||||
// HandleConnection implements transport.Transport.
|
||||
func (t *Transport) HandleConnection(w http.ResponseWriter, r *http.Request) (conn transport.Connection, err error) {
|
||||
return t.ws.HandleConnection(w, r)
|
||||
}
|
||||
|
||||
// Serve implements transport.Transport.
|
||||
func (t *Transport) Serve(w http.ResponseWriter, r *http.Request) {
|
||||
t.ws.Serve(w, r)
|
||||
}
|
||||
|
||||
var _ transport.Transport = &Transport{}
|
Loading…
Reference in New Issue