mirror of
https://github.com/pgsty/minio.git
synced 2026-08-10 08:13:28 +03:00
Persist offline mqtt events in the queueDir and replay (#7037)
This commit is contained in:
committed by
Harshavardhana
parent
8757c963ba
commit
6571641735
+30
-66
@@ -16,87 +16,51 @@ package mqtt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/eclipse/paho.mqtt.golang/packets"
|
||||
)
|
||||
|
||||
func keepalive(c *client) {
|
||||
defer c.workers.Done()
|
||||
DEBUG.Println(PNG, "keepalive starting")
|
||||
var checkInterval int64
|
||||
var pingSent time.Time
|
||||
|
||||
receiveInterval := c.options.KeepAlive + (1 * time.Second)
|
||||
pingTimer := timer{Timer: time.NewTimer(c.options.KeepAlive)}
|
||||
receiveTimer := timer{Timer: time.NewTimer(receiveInterval)}
|
||||
pingRespTimer := timer{Timer: time.NewTimer(c.options.PingTimeout)}
|
||||
if c.options.KeepAlive > 10 {
|
||||
checkInterval = 5
|
||||
} else {
|
||||
checkInterval = c.options.KeepAlive / 2
|
||||
}
|
||||
|
||||
pingRespTimer.Stop()
|
||||
intervalTicker := time.NewTicker(time.Duration(checkInterval * int64(time.Second)))
|
||||
defer intervalTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-c.stop:
|
||||
DEBUG.Println(PNG, "keepalive stopped")
|
||||
c.workers.Done()
|
||||
return
|
||||
case <-pingTimer.C:
|
||||
sendPing(&pingTimer, &pingRespTimer, c)
|
||||
case <-c.keepaliveReset:
|
||||
DEBUG.Println(NET, "resetting ping timer")
|
||||
pingTimer.Reset(c.options.KeepAlive)
|
||||
case <-c.pingResp:
|
||||
DEBUG.Println(NET, "resetting ping timeout timer")
|
||||
pingRespTimer.Stop()
|
||||
pingTimer.Reset(c.options.KeepAlive)
|
||||
receiveTimer.Reset(receiveInterval)
|
||||
case <-c.packetResp:
|
||||
DEBUG.Println(NET, "resetting receive timer")
|
||||
receiveTimer.Reset(receiveInterval)
|
||||
case <-receiveTimer.C:
|
||||
receiveTimer.SetRead(true)
|
||||
receiveTimer.Reset(receiveInterval)
|
||||
sendPing(&pingTimer, &pingRespTimer, c)
|
||||
case <-pingRespTimer.C:
|
||||
pingRespTimer.SetRead(true)
|
||||
CRITICAL.Println(PNG, "pingresp not received, disconnecting")
|
||||
c.workers.Done()
|
||||
c.internalConnLost(errors.New("pingresp not received, disconnecting"))
|
||||
pingTimer.Stop()
|
||||
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 {
|
||||
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())
|
||||
pingSent = time.Now()
|
||||
}
|
||||
}
|
||||
if atomic.LoadInt32(&c.pingOutstanding) > 0 && time.Now().Sub(pingSent) >= c.options.PingTimeout {
|
||||
CRITICAL.Println(PNG, "pingresp not received, disconnecting")
|
||||
c.errors <- errors.New("pingresp not received, disconnecting")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type timer struct {
|
||||
*time.Timer
|
||||
readFrom bool
|
||||
}
|
||||
|
||||
func (t *timer) SetRead(v bool) {
|
||||
t.readFrom = v
|
||||
}
|
||||
|
||||
func (t *timer) Stop() bool {
|
||||
defer t.SetRead(true)
|
||||
|
||||
if !t.Timer.Stop() && !t.readFrom {
|
||||
<-t.C
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *timer) Reset(d time.Duration) bool {
|
||||
defer t.SetRead(false)
|
||||
t.Stop()
|
||||
return t.Timer.Reset(d)
|
||||
}
|
||||
|
||||
func sendPing(pt *timer, rt *timer, c *client) {
|
||||
pt.SetRead(true)
|
||||
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.
|
||||
ping.Write(c.conn)
|
||||
|
||||
rt.Reset(c.options.PingTimeout)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user