many fixes for lint and context.Context usage (#5)
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user