Skip to content

Commit

Permalink
Optimize keep-alive mechanism
Browse files Browse the repository at this point in the history
heartbeat interval can be configured by the client

Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
Jianhui Zhao committed Sep 21, 2018
1 parent c5dcaeb commit 94384d3
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import (
const (
RTTY_PROTO_VERSION = 1

// pings period of client.
pingPeriod = 5 * time.Second

// Max lose ping times
aliveTimes = 3
)
Expand Down Expand Up @@ -147,8 +144,10 @@ func (c *Client) writePump() {
}
}

func (c *Client) keepAlive() {
ticker := time.NewTicker(pingPeriod)
func (c *Client) keepAlive(keepalive int64) {
ticker := time.NewTicker(time.Second)
last := time.Now().Unix()
keepalive = keepalive + 3
alive := aliveTimes

defer func() {
Expand All @@ -160,6 +159,7 @@ func (c *Client) keepAlive() {

c.conn.SetPingHandler(func(appData string) error {
alive = aliveTimes
last = time.Now().Unix()
return pingHandler(appData)
})

Expand All @@ -168,29 +168,33 @@ func (c *Client) keepAlive() {
case <- c.closeChan:
return
case <- ticker.C:
alive--
if alive == 0 {
rlog.Printf("Inactive device in long time, now kill it(%s)\n", c.devid)
return
now := time.Now().Unix()
if now - last > keepalive {
alive--
last = now
if alive == 0 {
rlog.Printf("Inactive device in long time, now kill it(%s)\n", c.devid)
return
}
}
}
}
}

/* serveWs handles websocket requests from the peer. */
func serveWs(br *Broker, w http.ResponseWriter, r *http.Request) {
keepalive, _ := strconv.ParseInt(r.URL.Query().Get("keepalive"), 10, 64)
proto,_ := strconv.Atoi(r.URL.Query().Get("proto"))
isDev := r.URL.Query().Get("device") != ""
devid := r.URL.Query().Get("devid")
proto := r.URL.Query().Get("proto")

if devid == "" {
rlog.Println("devid required")
return
}

if isDev {
proto_num, err := strconv.Atoi(proto)
if err != nil || proto_num != RTTY_PROTO_VERSION {
if proto != RTTY_PROTO_VERSION {
rlog.Printf("proto number is not matched for device '%s', you need to update your server(rttys) or client(rtty) or both them", devid)
return
}
Expand Down Expand Up @@ -223,7 +227,7 @@ func serveWs(br *Broker, w http.ResponseWriter, r *http.Request) {
go client.readPump()
go client.writePump()

if client.isDev {
go client.keepAlive()
if client.isDev && keepalive > 0 {
go client.keepAlive(keepalive)
}
}

0 comments on commit 94384d3

Please sign in to comment.