Transport init
This commit is contained in:
		| @@ -118,7 +118,7 @@ var ( | |||||||
| 		"random": selector.NewSelector, | 		"random": selector.NewSelector, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	DefaultTransports = map[string]func([]string, ...transport.Option) transport.Transport{ | 	DefaultTransports = map[string]func(...transport.Option) transport.Transport{ | ||||||
| 		"http": transport.NewTransport, | 		"http": transport.NewTransport, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -251,7 +251,7 @@ func (c *cmd) Before(ctx *cli.Context) error { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if t, ok := c.opts.Transports[name]; ok { | 		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 | 			*c.opts.Transport = n | ||||||
| 		} else { | 		} else { | ||||||
| 			return fmt.Errorf("Transport %s not found", name) | 			return fmt.Errorf("Transport %s not found", name) | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ type Options struct { | |||||||
| 	Brokers    map[string]func(...broker.Option) broker.Broker | 	Brokers    map[string]func(...broker.Option) broker.Broker | ||||||
| 	Registries map[string]func(...registry.Option) registry.Registry | 	Registries map[string]func(...registry.Option) registry.Registry | ||||||
| 	Selectors  map[string]func(...selector.Option) selector.Selector | 	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 | 	// Other options for implementations of the interface | ||||||
| 	// can be stored in a context | 	// can be stored in a context | ||||||
| @@ -114,7 +114,7 @@ func NewSelector(name string, s func(...selector.Option) selector.Selector) Opti | |||||||
| } | } | ||||||
|  |  | ||||||
| // New transport func | // 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) { | 	return func(o *Options) { | ||||||
| 		o.Transports[name] = t | 		o.Transports[name] = t | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -9,6 +9,6 @@ func init() { | |||||||
| 	cmd.DefaultTransports["http"] = NewTransport | 	cmd.DefaultTransports["http"] = NewTransport | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewTransport(addrs []string, opts ...transport.Option) transport.Transport { | func NewTransport(opts ...transport.Option) transport.Transport { | ||||||
| 	return transport.NewTransport(addrs, opts...) | 	return transport.NewTransport(opts...) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -420,7 +420,7 @@ func (h *httpTransport) String() string { | |||||||
| 	return "http" | 	return "http" | ||||||
| } | } | ||||||
|  |  | ||||||
| func newHttpTransport(addrs []string, opts ...Option) *httpTransport { | func newHttpTransport(opts ...Option) *httpTransport { | ||||||
| 	var options Options | 	var options Options | ||||||
| 	for _, o := range opts { | 	for _, o := range opts { | ||||||
| 		o(&options) | 		o(&options) | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ func expectedPort(t *testing.T, expected string, lsn transport.Listener) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestHTTPTransportPortRange(t *testing.T) { | func TestHTTPTransportPortRange(t *testing.T) { | ||||||
| 	tp := transport.NewTransport([]string{}) | 	tp := transport.NewTransport() | ||||||
|  |  | ||||||
| 	lsn1, err := tp.Listen(":44444-44448") | 	lsn1, err := tp.Listen(":44444-44448") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -43,7 +43,7 @@ func TestHTTPTransportPortRange(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestHTTPTransportCommunication(t *testing.T) { | func TestHTTPTransportCommunication(t *testing.T) { | ||||||
| 	tr := transport.NewTransport([]string{}) | 	tr := transport.NewTransport() | ||||||
|  |  | ||||||
| 	l, err := tr.Listen(":0") | 	l, err := tr.Listen(":0") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|   | |||||||
| @@ -8,6 +8,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| type Options struct { | type Options struct { | ||||||
|  | 	Addrs     []string | ||||||
| 	Secure    bool | 	Secure    bool | ||||||
| 	TLSConfig *tls.Config | 	TLSConfig *tls.Config | ||||||
|  |  | ||||||
| @@ -37,6 +38,13 @@ type ListenOptions struct { | |||||||
| 	Context context.Context | 	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 secure communication. If TLSConfig is not specified we | ||||||
| // use InsecureSkipVerify and generate a self signed cert | // use InsecureSkipVerify and generate a self signed cert | ||||||
| func Secure(b bool) Option { | func Secure(b bool) Option { | ||||||
|   | |||||||
| @@ -43,13 +43,13 @@ type DialOption func(*DialOptions) | |||||||
| type ListenOption func(*ListenOptions) | type ListenOption func(*ListenOptions) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| 	DefaultTransport Transport = newHttpTransport([]string{}) | 	DefaultTransport Transport = newHttpTransport() | ||||||
|  |  | ||||||
| 	DefaultDialTimeout = time.Second * 5 | 	DefaultDialTimeout = time.Second * 5 | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func NewTransport(addrs []string, opt ...Option) Transport { | func NewTransport(opts ...Option) Transport { | ||||||
| 	return newHttpTransport(addrs, opt...) | 	return newHttpTransport(opts...) | ||||||
| } | } | ||||||
|  |  | ||||||
| func Dial(addr string, opts ...DialOption) (Client, error) { | func Dial(addr string, opts ...DialOption) (Client, error) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user