Skip to content

Commit

Permalink
Check for there being any servers to connect to
Browse files Browse the repository at this point in the history
resolves #197
  • Loading branch information
alsm committed Mar 22, 2018
1 parent 36d01c2 commit 20fd5cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 12 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,18 @@ func (c *client) Connect() Token {
c.persist.Open()

c.setConnected(connecting)
c.errors = make(chan error, 1)
c.stop = make(chan struct{})

var rc byte
cm := newConnectMsgFromOptions(&c.options)
protocolVersion := c.options.ProtocolVersion

if len(c.options.Servers) == 0 {
t.setError(fmt.Errorf("No servers defined to connect to"))
return
}

for _, broker := range c.options.Servers {
c.options.ProtocolVersion = protocolVersion
CONN:
Expand Down Expand Up @@ -247,23 +255,19 @@ func (c *client) Connect() Token {

if c.conn == nil {
ERROR.Println(CLI, "Failed to connect to a broker")
c.setConnected(disconnected)
c.persist.Close()
t.returnCode = rc
if rc != packets.ErrNetworkError {
t.err = packets.ConnErrors[rc]
t.setError(packets.ConnErrors[rc])
} else {
t.err = fmt.Errorf("%s : %s", packets.ConnErrors[rc], err)
t.setError(fmt.Errorf("%s : %s", packets.ConnErrors[rc], err))
}
c.setConnected(disconnected)
c.persist.Close()
t.flowComplete()
return
}

c.options.protocolVersionExplicit = true

c.errors = make(chan error, 1)
c.stop = make(chan struct{})

if c.options.KeepAlive != 0 {
atomic.StoreInt32(&c.pingOutstanding, 0)
atomic.StoreInt64(&c.lastReceived, time.Now().Unix())
Expand Down
2 changes: 0 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ func (b *baseToken) Error() error {
}

func (b *baseToken) setError(e error) {
b.m.Lock()
defer b.m.Unlock()
b.err = e
b.flowComplete()
}
Expand Down

0 comments on commit 20fd5cb

Please sign in to comment.