Simplify reconnect code.

This commit is contained in:
Bruce Marriner 2016-07-05 21:38:37 -05:00
parent 604872ed4b
commit 956961ac3e

View file

@ -794,7 +794,7 @@ func (v *VoiceConnection) reconnect() {
v.Lock()
if v.reconnecting {
v.log(LogInformational, "already reconnecting, exiting.")
v.log(LogInformational, "already reconnecting to channel %s, exiting", v.ChannelID)
v.Unlock()
return
}
@ -803,10 +803,9 @@ func (v *VoiceConnection) reconnect() {
defer func() { v.reconnecting = false }()
/*
if v.session == nil {
v.log(LogInformational, "cannot reconnect with nil session")
v.log(LogInformational, "Deleting VoiceConnection %s", v.GuildID)
delete(v.session.VoiceConnections, v.GuildID)
return
}
@ -832,43 +831,31 @@ func (v *VoiceConnection) reconnect() {
data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
v.session.wsMutex.Lock()
err := v.session.wsConn.WriteJSON(data)
v.session.wsMutex.Unlock()
if err != nil {
v.log(LogError, "error sending disconnect packet, %s", err)
}
v.session.wsMutex.Unlock()
v.sessionID = ""
}
*/
// Close websocket and udp connections
// Close any currently open connections
v.Close()
wait := time.Duration(1)
i = 0
for {
if v.session == nil {
v.log(LogInformational, "cannot reconnect with nil session")
v.log(LogInformational, "Deleting VoiceConnection %s", v.GuildID)
delete(v.session.VoiceConnections, v.GuildID)
return
}
<-time.After(wait * time.Second)
wait *= 2
if wait > 600 {
wait = 600
}
i++
if v.session.DataReady == false || v.session.wsConn == nil {
v.log(LogInformational, "cannot reconenct with unready session")
v.log(LogInformational, "cannot reconenct to channel %s with unready session", v.ChannelID)
continue
}
v.log(LogInformational, "trying to reconnect to voice")
v.log(LogInformational, "trying to reconnect to channel %s", v.ChannelID)
// Below is required because ChannelVoiceJoin checks the GuildID
// to decide if we should change channels or open a new connection.
@ -879,20 +866,10 @@ func (v *VoiceConnection) reconnect() {
_, err := v.session.ChannelVoiceJoin(gID, v.ChannelID, v.mute, v.deaf)
if err == nil {
v.log(LogInformational, "successfully reconnected to voice")
return
}
v.log(LogInformational, "error reconnecting to voice, %s", err)
if i >= 10 {
// NOTE: this will probably change but it's a safety net
// here to prevent this goroutine from becomming abandoned.
v.log(LogInformational, "timeout reconnecting, I give up.")
v.log(LogInformational, "Deleting VoiceConnection %s", v.GuildID)
delete(v.session.VoiceConnections, v.GuildID)
v.log(LogInformational, "successfully reconnected to channel %s", v.ChannelID)
return
}
v.log(LogInformational, "error reconnecting to channel %s, %s", v.ChannelID, err)
}
}