* pool

* pool

* wpool
This commit is contained in:
joy.zhou
2018-04-04 13:49:52 +08:00
committed by GitHub
parent c0fea6a5ba
commit 5ed4728575
2 changed files with 41 additions and 38 deletions

View File

@@ -3,13 +3,14 @@
package broker
import (
"github.com/eclipse/paho.mqtt.golang/packets"
"go.uber.org/zap"
"net"
"reflect"
"strings"
"sync"
"time"
"github.com/eclipse/paho.mqtt.golang/packets"
"go.uber.org/zap"
)
const (
@@ -85,8 +86,11 @@ func (c *client) init() {
c.info.remoteIP = strings.Split(c.conn.RemoteAddr().String(), ":")[0]
}
func (c *client) keepAlive(ch chan int, mpool chan *Message) {
func (c *client) keepAlive(ch chan int) {
defer close(ch)
b := c.broker
keepalive := time.Duration(c.info.keepalive*3/2) * time.Second
timer := time.NewTimer(keepalive)
@@ -100,8 +104,10 @@ func (c *client) keepAlive(ch chan int, mpool chan *Message) {
continue
}
log.Error("Client exceeded timeout, disconnecting. ", zap.String("ClientID", c.info.clientID), zap.Uint16("keepalive", c.info.keepalive))
msg := &Message{client: c, packet: DisconnectdPacket}
mpool <- msg
b.SubmitWork(msg)
timer.Stop()
return
case _, ok := <-c.closed:
@@ -112,14 +118,15 @@ func (c *client) keepAlive(ch chan int, mpool chan *Message) {
}
}
func (c *client) readLoop(mpool chan *Message) {
func (c *client) readLoop() {
nc := c.conn
if nc == nil || mpool == nil {
b := c.broker
if nc == nil || b == nil {
return
}
ch := make(chan int, 1000)
go c.keepAlive(ch, mpool)
go c.keepAlive(ch)
for {
packet, err := packets.ReadPacket(nc)
@@ -134,11 +141,11 @@ func (c *client) readLoop(mpool chan *Message) {
client: c,
packet: packet,
}
mpool <- msg
b.SubmitWork(msg)
}
msg := &Message{client: c, packet: DisconnectdPacket}
mpool <- msg
b.SubmitWork(msg)
}
func ProcessMessage(msg *Message) {