fixed setting of address through Init() function.

This commit is contained in:
darren-west 2017-05-23 17:57:54 +01:00 committed by Vasiliy Tolstov
parent 8898b42bda
commit 260b112006

33
nats.go
View File

@ -61,6 +61,23 @@ func (n *nbroker) Address() string {
return ""
}
func setAddrs(addrs []string) []string {
var cAddrs []string
for _, addr := range addrs {
if len(addr) == 0 {
continue
}
if !strings.HasPrefix(addr, "nats://") {
addr = "nats://" + addr
}
cAddrs = append(cAddrs, addr)
}
if len(cAddrs) == 0 {
cAddrs = []string{nats.DefaultURL}
}
return cAddrs
}
func (n *nbroker) Connect() error {
if n.conn != nil {
return nil
@ -93,6 +110,7 @@ func (n *nbroker) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&n.opts)
}
n.addrs = setAddrs(n.opts.Addrs)
return nil
}
@ -153,21 +171,8 @@ func NewBroker(opts ...broker.Option) broker.Broker {
o(&options)
}
var cAddrs []string
for _, addr := range options.Addrs {
if len(addr) == 0 {
continue
}
if !strings.HasPrefix(addr, "nats://") {
addr = "nats://" + addr
}
cAddrs = append(cAddrs, addr)
}
if len(cAddrs) == 0 {
cAddrs = []string{nats.DefaultURL}
}
return &nbroker{
addrs: cAddrs,
addrs: setAddrs(options.Addrs),
opts: options,
}
}