Pings are also wrapped with own sync.Mutex.

This commit is contained in:
Stanislav N. aka pztrn 2017-10-03 02:19:13 +05:00
parent e39cceace6
commit 5a0a900995
2 changed files with 5 additions and 2 deletions

6
irc.go
View File

@ -74,9 +74,9 @@ func (irc *Connection) readLoop() {
irc.Log.Printf("<-- %s\n", strings.TrimSpace(msg))
}
irc.Lock()
irc.lastMessageMutex.Lock()
irc.lastMessage = time.Now()
irc.Unlock()
irc.lastMessageMutex.Unlock()
event, err := parseToEvent(msg)
event.Connection = irc
if err == nil {
@ -166,9 +166,11 @@ func (irc *Connection) pingLoop() {
select {
case <-ticker.C:
//Ping if we haven't received anything from the server within the keep alive period
irc.lastMessageMutex.Lock()
if time.Since(irc.lastMessage) >= irc.KeepAlive {
irc.SendRawf("PING %d", time.Now().UnixNano())
}
irc.lastMessageMutex.Unlock()
case <-ticker2.C:
//Ping at the ping frequency
irc.SendRawf("PING %d", time.Now().UnixNano())

View File

@ -43,6 +43,7 @@ type Connection struct {
QuitMessage string
lastMessage time.Time
lastMessageMutex sync.Mutex
VerboseCallbackHandler bool
Log *log.Logger