diff options
Diffstat (limited to 'vendor/github.com/nlopes/slack/websocket.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/websocket.go | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/vendor/github.com/nlopes/slack/websocket.go b/vendor/github.com/nlopes/slack/websocket.go index e5dee68a..122807b9 100644 --- a/vendor/github.com/nlopes/slack/websocket.go +++ b/vendor/github.com/nlopes/slack/websocket.go @@ -2,7 +2,6 @@ package slack import ( "encoding/json" - "errors" "net/url" "sync" "time" @@ -33,11 +32,10 @@ type RTM struct { IncomingEvents chan RTMEvent outgoingMessages chan OutgoingMessage killChannel chan bool - disconnected chan struct{} // disconnected is closed when Disconnect is invoked, regardless of connection state. Allows for ManagedConnection to not leak. + disconnected chan struct{} + disconnectedm *sync.Once forcePing chan bool rawEvents chan json.RawMessage - wasIntentional bool - isConnected bool // UserDetails upon connection info *Info @@ -58,32 +56,30 @@ type RTM struct { connParams url.Values } +// signal that we are disconnected by closing the channel. +// protect it with a mutex to ensure it only happens once. +func (rtm *RTM) disconnect() { + rtm.disconnectedm.Do(func() { + close(rtm.disconnected) + }) +} + // Disconnect and wait, blocking until a successful disconnection. func (rtm *RTM) Disconnect() error { - // avoid RTM disconnect race conditions - rtm.mu.Lock() - defer rtm.mu.Unlock() - - // always push into the disconnected channel when invoked, + // always push into the kill channel when invoked, // this lets the ManagedConnection() function properly clean up. // if the buffer is full then just continue on. select { - case rtm.disconnected <- struct{}{}: - default: + case rtm.killChannel <- true: + return nil + case <-rtm.disconnected: + return ErrAlreadyDisconnected } - - if !rtm.isConnected { - return errors.New("Invalid call to Disconnect - Slack API is already disconnected") - } - - rtm.killChannel <- true - return nil } // GetInfo returns the info structure received when calling -// "startrtm", holding all channels, groups and other metadata needed -// to implement a full chat client. It will be non-nil after a call to -// StartRTM(). +// "startrtm", holding metadata needed to implement a full +// chat client. It will be non-nil after a call to StartRTM(). func (rtm *RTM) GetInfo() *Info { return rtm.info } |