move nats local logic

This commit is contained in:
Asim Aslam 2020-01-18 01:29:53 +00:00
parent fd6eb23307
commit 65df711b01

View File

@ -119,17 +119,17 @@ func (n *natsBroker) serve(exit chan bool) error {
// with no address we just default it // with no address we just default it
// this is a local client address // this is a local client address
if len(n.addrs) == 0 || n.local { if len(n.addrs) == 0 {
host = "127.0.0.1" host = "127.0.0.1"
port = -1 port = -1
local = true local = true
// with a local address we parse it
} else { } else {
address := n.addrs[0] address := n.addrs[0]
if strings.HasPrefix(address, "nats://") { if strings.HasPrefix(address, "nats://") {
address = strings.TrimPrefix(address, "nats://") address = strings.TrimPrefix(address, "nats://")
} }
// check if its a local address and only then embed
if addr.IsLocal(address) { if addr.IsLocal(address) {
h, p, err := net.SplitHostPort(address) h, p, err := net.SplitHostPort(address)
if err == nil { if err == nil {
@ -266,9 +266,11 @@ func (n *natsBroker) Connect() error {
// create exit chan // create exit chan
n.exit = make(chan bool) n.exit = make(chan bool)
// start the server if needed // start embedded server if asked to
if err := n.serve(n.exit); err != nil { if n.local {
return err if err := n.serve(n.exit); err != nil {
return err
}
} }
// set to connected // set to connected