Skip to content

Commit

Permalink
fix Issue #16
Browse files Browse the repository at this point in the history
connection cleanning gorotine fix.
  • Loading branch information
ruizeng committed Dec 31, 2015
1 parent 46e63b2 commit 50d1354
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/mqtt/connection.go
Expand Up @@ -135,6 +135,7 @@ func (c *Connection) RcvMsgFromClient() {
}

server.Log.Infof("%s, come msg===\n%v\n=====", host, msg)
c.LastHbTime = time.Now().Unix()
switch msg := msg.(type) {
case *Connect:
ret := RetCodeAccepted
Expand Down Expand Up @@ -246,7 +247,6 @@ func (c *Connection) RcvMsgFromClient() {

case *PingReq:
server.Log.Infof("%s, ping req comes", host)
c.LastHbTime = time.Now().Unix()
pingrsp := &PingResp{}
err := c.Mgr.Provider.OnDeviceHeartBeat(c.DeviceId)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/mqtt/manager.go
@@ -1,6 +1,7 @@
package mqtt

import (
"github.com/PandoCloud/pando-cloud/pkg/server"
"net"
"sync"
"time"
Expand Down Expand Up @@ -81,14 +82,16 @@ func (m *Manager) PublishMessage2Server(deviceid uint64, msg *Publish) error {

func (m *Manager) CleanWorker() {
for {
server.Log.Infoln("scanning and removing inactive connections...")
curTime := time.Now().Unix()

for _, con := range m.IdToConn {
if con.KeepAlive == 0 {
continue
}

if uint16(curTime-con.LastHbTime) > uint16(2*con.KeepAlive/2) {
if uint16(curTime-con.LastHbTime) > uint16(3*con.KeepAlive/2) {
server.Log.Infof("connection %v inactive , removing", con)
con.Close()
}
}
Expand Down

0 comments on commit 50d1354

Please sign in to comment.