many fixes for lint and context.Context usage (#5)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-11-03 02:02:32 +03:00
committed by GitHub
parent 40b0870cf8
commit 8a2b122015
44 changed files with 152 additions and 1175 deletions

View File

@@ -1,5 +1,7 @@
package transport
import "context"
type noopTransport struct {
opts Options
}
@@ -19,12 +21,12 @@ func (t *noopTransport) Options() Options {
return t.opts
}
func (t *noopTransport) Dial(addr string, opts ...DialOption) (Client, error) {
func (t *noopTransport) Dial(ctx context.Context, addr string, opts ...DialOption) (Client, error) {
options := NewDialOptions(opts...)
return &noopClient{opts: options}, nil
}
func (t *noopTransport) Listen(addr string, opts ...ListenOption) (Listener, error) {
func (t *noopTransport) Listen(ctx context.Context, addr string, opts ...ListenOption) (Listener, error) {
options := NewListenOptions(opts...)
return &noopListener{opts: options}, nil
}

View File

@@ -31,6 +31,7 @@ type Options struct {
Context context.Context
}
// NewOptions returns new options
func NewOptions(opts ...Option) Options {
options := Options{
Logger: logger.DefaultLogger,
@@ -44,6 +45,7 @@ func NewOptions(opts ...Option) Options {
return options
}
// DialOptions struct
type DialOptions struct {
// Tells the transport this is a streaming connection with
// multiple calls to send/recv and that send may not even be called
@@ -59,6 +61,7 @@ type DialOptions struct {
Context context.Context
}
// NewDialOptions returns new DialOptions
func NewDialOptions(opts ...DialOption) DialOptions {
options := DialOptions{
Context: context.Background(),
@@ -71,6 +74,7 @@ func NewDialOptions(opts ...DialOption) DialOptions {
return options
}
// ListenOptions struct
type ListenOptions struct {
// TODO: add tls options when listening
// Currently set in global options
@@ -80,6 +84,7 @@ type ListenOptions struct {
Context context.Context
}
// NewListenOptions returns new ListenOptions
func NewListenOptions(opts ...ListenOption) ListenOptions {
options := ListenOptions{
Context: context.Background(),
@@ -106,6 +111,7 @@ func Logger(l logger.Logger) Option {
}
}
// Context sets the context
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
@@ -142,14 +148,14 @@ func TLSConfig(t *tls.Config) Option {
}
}
// Indicates whether this is a streaming connection
// WithStream indicates whether this is a streaming connection
func WithStream() DialOption {
return func(o *DialOptions) {
o.Stream = true
}
}
// Timeout used when dialling the remote side
// WithTimeout used when dialling the remote side
func WithTimeout(d time.Duration) DialOption {
return func(o *DialOptions) {
o.Timeout = d

View File

@@ -2,6 +2,7 @@
package transport
import (
"context"
"time"
)
@@ -16,8 +17,8 @@ var (
type Transport interface {
Init(...Option) error
Options() Options
Dial(addr string, opts ...DialOption) (Client, error)
Listen(addr string, opts ...ListenOption) (Listener, error)
Dial(ctx context.Context, addr string, opts ...DialOption) (Client, error)
Listen(ctx context.Context, addr string, opts ...ListenOption) (Listener, error)
String() string
}

View File

@@ -50,17 +50,17 @@ func (t *tunBroker) Address() string {
}
func (t *tunBroker) Connect(ctx context.Context) error {
return t.tunnel.Connect()
return t.tunnel.Connect(ctx)
}
func (t *tunBroker) Disconnect(ctx context.Context) error {
return t.tunnel.Close()
return t.tunnel.Close(ctx)
}
func (t *tunBroker) Publish(ctx context.Context, topic string, m *broker.Message, opts ...broker.PublishOption) error {
// TODO: this is probably inefficient, we might want to just maintain an open connection
// it may be easier to add broadcast to the tunnel
c, err := t.tunnel.Dial(topic, tunnel.DialMode(tunnel.Multicast))
c, err := t.tunnel.Dial(ctx, topic, tunnel.DialMode(tunnel.Multicast))
if err != nil {
return err
}
@@ -73,7 +73,7 @@ func (t *tunBroker) Publish(ctx context.Context, topic string, m *broker.Message
}
func (t *tunBroker) Subscribe(ctx context.Context, topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
l, err := t.tunnel.Listen(topic, tunnel.ListenMode(tunnel.Multicast))
l, err := t.tunnel.Listen(ctx, topic, tunnel.ListenMode(tunnel.Multicast))
if err != nil {
return nil, err
}

View File

@@ -26,7 +26,7 @@ func (t *tunTransport) Init(opts ...transport.Option) error {
// close the existing tunnel
if t.tunnel != nil {
t.tunnel.Close()
t.tunnel.Close(context.TODO())
}
// get the tunnel
@@ -47,12 +47,12 @@ func (t *tunTransport) Init(opts ...transport.Option) error {
return nil
}
func (t *tunTransport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
if err := t.tunnel.Connect(); err != nil {
func (t *tunTransport) Dial(ctx context.Context, addr string, opts ...transport.DialOption) (transport.Client, error) {
if err := t.tunnel.Connect(ctx); err != nil {
return nil, err
}
c, err := t.tunnel.Dial(addr)
c, err := t.tunnel.Dial(ctx, addr)
if err != nil {
return nil, err
}
@@ -60,12 +60,12 @@ func (t *tunTransport) Dial(addr string, opts ...transport.DialOption) (transpor
return c, nil
}
func (t *tunTransport) Listen(addr string, opts ...transport.ListenOption) (transport.Listener, error) {
if err := t.tunnel.Connect(); err != nil {
func (t *tunTransport) Listen(ctx context.Context, addr string, opts ...transport.ListenOption) (transport.Listener, error) {
if err := t.tunnel.Connect(ctx); err != nil {
return nil, err
}
l, err := t.tunnel.Listen(addr)
l, err := t.tunnel.Listen(ctx, addr)
if err != nil {
return nil, err
}

View File

@@ -2,6 +2,7 @@
package tunnel
import (
"context"
"errors"
"time"
@@ -55,15 +56,15 @@ type Tunnel interface {
// Address returns the address the tunnel is listening on
Address() string
// Connect connects the tunnel
Connect() error
Connect(ctx context.Context) error
// Close closes the tunnel
Close() error
Close(ctx context.Context) error
// Links returns all the links the tunnel is connected to
Links() []Link
// Dial allows a client to connect to a channel
Dial(channel string, opts ...DialOption) (Session, error)
Dial(ctx context.Context, channel string, opts ...DialOption) (Session, error)
// Listen allows to accept connections on a channel
Listen(channel string, opts ...ListenOption) (Listener, error)
Listen(ctx context.Context, channel string, opts ...ListenOption) (Listener, error)
// String returns the name of the tunnel implementation
String() string
}