Skip to content

Commit

Permalink
Merge pull request #305 from johnhydroware/wall-time
Browse files Browse the repository at this point in the history
Use monotonic time for keep alive
  • Loading branch information
alsm authored Apr 17, 2019
2 parents 54767d4 + 52b2e62 commit 1c3a9d2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
12 changes: 6 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ type Client interface {

// client implements the Client interface
type client struct {
lastSent int64
lastReceived int64
lastSent atomic.Value
lastReceived atomic.Value
pingOutstanding int32
status uint32
sync.RWMutex
Expand Down Expand Up @@ -300,8 +300,8 @@ func (c *client) Connect() Token {

if c.options.KeepAlive != 0 {
atomic.StoreInt32(&c.pingOutstanding, 0)
atomic.StoreInt64(&c.lastReceived, time.Now().Unix())
atomic.StoreInt64(&c.lastSent, time.Now().Unix())
c.lastReceived.Store(time.Now())
c.lastSent.Store(time.Now())
c.workers.Add(1)
go keepalive(c)
}
Expand Down Expand Up @@ -412,8 +412,8 @@ func (c *client) reconnect() {

if c.options.KeepAlive != 0 {
atomic.StoreInt32(&c.pingOutstanding, 0)
atomic.StoreInt64(&c.lastReceived, time.Now().Unix())
atomic.StoreInt64(&c.lastSent, time.Now().Unix())
c.lastReceived.Store(time.Now())
c.lastSent.Store(time.Now())
c.workers.Add(1)
go keepalive(c)
}
Expand Down
4 changes: 2 additions & 2 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func incoming(c *client) {
case c.ibound <- cp:
// Notify keepalive logic that we recently received a packet
if c.options.KeepAlive != 0 {
atomic.StoreInt64(&c.lastReceived, time.Now().Unix())
c.lastReceived.Store(time.Now())
}
case <-c.stop:
// This avoids a deadlock should a message arrive while shutting down.
Expand Down Expand Up @@ -221,7 +221,7 @@ func outgoing(c *client) {
}
// Reset ping timer after sending control packet.
if c.options.KeepAlive != 0 {
atomic.StoreInt64(&c.lastSent, time.Now().Unix())
c.lastSent.Store(time.Now())
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ func keepalive(c *client) {
DEBUG.Println(PNG, "keepalive stopped")
return
case <-intervalTicker.C:
DEBUG.Println(PNG, "ping check", time.Now().Unix()-atomic.LoadInt64(&c.lastSent))
if time.Now().Unix()-atomic.LoadInt64(&c.lastSent) >= c.options.KeepAlive || time.Now().Unix()-atomic.LoadInt64(&c.lastReceived) >= c.options.KeepAlive {
lastSent := c.lastSent.Load().(time.Time)
lastReceived := c.lastReceived.Load().(time.Time)

DEBUG.Println(PNG, "ping check", time.Since(lastSent).Seconds())
if time.Since(lastSent) >= time.Duration(c.options.KeepAlive*int64(time.Second)) || time.Since(lastReceived) >= time.Duration(c.options.KeepAlive*int64(time.Second)) {
if atomic.LoadInt32(&c.pingOutstanding) == 0 {
DEBUG.Println(PNG, "keepalive sending ping")
ping := packets.NewControlPacket(packets.Pingreq).(*packets.PingreqPacket)
//We don't want to wait behind large messages being sent, the Write call
//will block until it it able to send the packet.
atomic.StoreInt32(&c.pingOutstanding, 1)
ping.Write(c.conn)
atomic.StoreInt64(&c.lastSent, time.Now().Unix())
c.lastSent.Store(time.Now())
pingSent = time.Now()
}
}
Expand Down

0 comments on commit 1c3a9d2

Please sign in to comment.