go-emlid/reach/client/socketio/options.go

88 lines
2.0 KiB
Go

package socketio
import (
"net"
"time"
"github.com/pkg/errors"
)
type DialFunc func(network, addr string) (net.Conn, error)
type Options struct {
PingInterval time.Duration
PingTimeout time.Duration
ReceiveTimeout time.Duration
SendTimeout time.Duration
BufferSize int
DialFunc DialFunc
}
type OptionFunc func(opts *Options)
func NewOptions(funcs ...OptionFunc) *Options {
opts := &Options{
PingInterval: 5 * time.Second,
PingTimeout: 60 * time.Second,
ReceiveTimeout: 60 * time.Second,
SendTimeout: 60 * time.Second,
BufferSize: 1024 * 32,
DialFunc: DefaultDialFunc,
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
// WithPingInterval configures the client to use the given ping interval
func WithPingInterval(interval time.Duration) OptionFunc {
return func(opts *Options) {
opts.PingInterval = interval
}
}
// WithPingTimeout configures the client to use the given ping timeout
func WithPingTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.PingTimeout = timeout
}
}
// WithReceiveTimeout configures the client to use the given receive timeout
func WithReceiveTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.ReceiveTimeout = timeout
}
}
// WithSendTimeout configures the client to use the given send timeout
func WithSendTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.SendTimeout = timeout
}
}
// WithBufferSize configures the client to use the given buffer size
func WithBufferSize(size int) OptionFunc {
return func(opts *Options) {
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
}
}