From 5a6ff655db9f8f12e61b1001b2be08382f0d4292 Mon Sep 17 00:00:00 2001 From: Asim Date: Tue, 15 Mar 2016 22:25:32 +0000 Subject: [PATCH] Transport init --- cmd/cmd.go | 4 ++-- cmd/options.go | 4 ++-- transport/http/http.go | 4 ++-- transport/http_transport.go | 2 +- transport/http_transport_test.go | 4 ++-- transport/options.go | 8 ++++++++ transport/transport.go | 6 +++--- 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 723e52d5..e90e5d12 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -118,7 +118,7 @@ var ( "random": selector.NewSelector, } - DefaultTransports = map[string]func([]string, ...transport.Option) transport.Transport{ + DefaultTransports = map[string]func(...transport.Option) transport.Transport{ "http": transport.NewTransport, } @@ -251,7 +251,7 @@ func (c *cmd) Before(ctx *cli.Context) error { } if t, ok := c.opts.Transports[name]; ok { - n := t(strings.Split(ctx.String("transport_address"), ",")) + n := t(transport.Addrs(strings.Split(ctx.String("transport_address"), ",")...)) *c.opts.Transport = n } else { return fmt.Errorf("Transport %s not found", name) diff --git a/cmd/options.go b/cmd/options.go index 0cef27c2..fb1f05e9 100644 --- a/cmd/options.go +++ b/cmd/options.go @@ -28,7 +28,7 @@ type Options struct { Brokers map[string]func(...broker.Option) broker.Broker Registries map[string]func(...registry.Option) registry.Registry Selectors map[string]func(...selector.Option) selector.Selector - Transports map[string]func([]string, ...transport.Option) transport.Transport + Transports map[string]func(...transport.Option) transport.Transport // Other options for implementations of the interface // can be stored in a context @@ -114,7 +114,7 @@ func NewSelector(name string, s func(...selector.Option) selector.Selector) Opti } // New transport func -func NewTransport(name string, t func([]string, ...transport.Option) transport.Transport) Option { +func NewTransport(name string, t func(...transport.Option) transport.Transport) Option { return func(o *Options) { o.Transports[name] = t } diff --git a/transport/http/http.go b/transport/http/http.go index 16cdc102..eb5b56ba 100644 --- a/transport/http/http.go +++ b/transport/http/http.go @@ -9,6 +9,6 @@ func init() { cmd.DefaultTransports["http"] = NewTransport } -func NewTransport(addrs []string, opts ...transport.Option) transport.Transport { - return transport.NewTransport(addrs, opts...) +func NewTransport(opts ...transport.Option) transport.Transport { + return transport.NewTransport(opts...) } diff --git a/transport/http_transport.go b/transport/http_transport.go index 4cb9c152..9d4105a5 100644 --- a/transport/http_transport.go +++ b/transport/http_transport.go @@ -420,7 +420,7 @@ func (h *httpTransport) String() string { return "http" } -func newHttpTransport(addrs []string, opts ...Option) *httpTransport { +func newHttpTransport(opts ...Option) *httpTransport { var options Options for _, o := range opts { o(&options) diff --git a/transport/http_transport_test.go b/transport/http_transport_test.go index 596b3bbb..a0eb1468 100644 --- a/transport/http_transport_test.go +++ b/transport/http_transport_test.go @@ -18,7 +18,7 @@ func expectedPort(t *testing.T, expected string, lsn transport.Listener) { } func TestHTTPTransportPortRange(t *testing.T) { - tp := transport.NewTransport([]string{}) + tp := transport.NewTransport() lsn1, err := tp.Listen(":44444-44448") if err != nil { @@ -43,7 +43,7 @@ func TestHTTPTransportPortRange(t *testing.T) { } func TestHTTPTransportCommunication(t *testing.T) { - tr := transport.NewTransport([]string{}) + tr := transport.NewTransport() l, err := tr.Listen(":0") if err != nil { diff --git a/transport/options.go b/transport/options.go index cb7ba98a..8d0ecc54 100644 --- a/transport/options.go +++ b/transport/options.go @@ -8,6 +8,7 @@ import ( ) type Options struct { + Addrs []string Secure bool TLSConfig *tls.Config @@ -37,6 +38,13 @@ type ListenOptions struct { Context context.Context } +// Addrs to use for transport +func Addrs(addrs ...string) Option { + return func(o *Options) { + o.Addrs = addrs + } +} + // Use secure communication. If TLSConfig is not specified we // use InsecureSkipVerify and generate a self signed cert func Secure(b bool) Option { diff --git a/transport/transport.go b/transport/transport.go index dc130edd..1010d6b8 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -43,13 +43,13 @@ type DialOption func(*DialOptions) type ListenOption func(*ListenOptions) var ( - DefaultTransport Transport = newHttpTransport([]string{}) + DefaultTransport Transport = newHttpTransport() DefaultDialTimeout = time.Second * 5 ) -func NewTransport(addrs []string, opt ...Option) Transport { - return newHttpTransport(addrs, opt...) +func NewTransport(opts ...Option) Transport { + return newHttpTransport(opts...) } func Dial(addr string, opts ...DialOption) (Client, error) {