summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nlopes/slack/websocket.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/nlopes/slack/websocket.go')
-rw-r--r--vendor/github.com/nlopes/slack/websocket.go43
1 files changed, 31 insertions, 12 deletions
diff --git a/vendor/github.com/nlopes/slack/websocket.go b/vendor/github.com/nlopes/slack/websocket.go
index 77906e07..242acf40 100644
--- a/vendor/github.com/nlopes/slack/websocket.go
+++ b/vendor/github.com/nlopes/slack/websocket.go
@@ -3,9 +3,10 @@ package slack
import (
"encoding/json"
"errors"
+ "sync"
"time"
- "golang.org/x/net/websocket"
+ "github.com/gorilla/websocket"
)
const (
@@ -19,8 +20,9 @@ const (
//
// Create this element with Client's NewRTM() or NewRTMWithOptions(*RTMOptions)
type RTM struct {
- idGen IDGenerator
- pings map[int]time.Time
+ idGen IDGenerator
+ pingInterval time.Duration
+ pingDeadman *time.Timer
// Connection life-cycle
conn *websocket.Conn
@@ -44,6 +46,13 @@ type RTM struct {
// rtm.start to connect to Slack, otherwise it will use
// rtm.connect
useRTMStart bool
+
+ // dialer is a gorilla/websocket Dialer. If nil, use the default
+ // Dialer.
+ dialer *websocket.Dialer
+
+ // mu is mutex used to prevent RTM connection race conditions
+ mu *sync.Mutex
}
// RTMOptions allows configuration of various options available for RTM messaging
@@ -60,9 +69,17 @@ type RTMOptions struct {
// Disconnect and wait, blocking until a successful disconnection.
func (rtm *RTM) Disconnect() error {
- // this channel is always closed on disconnect. lets the ManagedConnection() function
- // properly clean up.
- close(rtm.disconnected)
+ // avoid RTM disconnect race conditions
+ rtm.mu.Lock()
+ defer rtm.mu.Unlock()
+
+ // always push into the disconnected 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:
+ }
if !rtm.isConnected {
return errors.New("Invalid call to Disconnect - Slack API is already disconnected")
@@ -72,12 +89,6 @@ func (rtm *RTM) Disconnect() error {
return nil
}
-// Reconnect only makes sense if you've successfully disconnectd with Disconnect().
-func (rtm *RTM) Reconnect() error {
- logger.Println("RTM::Reconnect not implemented!")
- 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
@@ -97,3 +108,11 @@ func (rtm *RTM) SendMessage(msg *OutgoingMessage) {
rtm.outgoingMessages <- *msg
}
+
+func (rtm *RTM) resetDeadman() {
+ timerReset(rtm.pingDeadman, deadmanDuration(rtm.pingInterval))
+}
+
+func deadmanDuration(d time.Duration) time.Duration {
+ return d * 4
+}