Skip to content

Commit

Permalink
bugfix: heartbeat span wrap round
Browse files Browse the repository at this point in the history
  • Loading branch information
xjdrew committed Mar 27, 2020
1 parent 9abb25d commit 02281ea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/xjdrew/gotunnel/tunnel"
)

// Service .
type Service interface {
Start() error
Status()
Expand Down
15 changes: 8 additions & 7 deletions tunnel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import (
"time"
)

// client hub
// ClientHub manages client links
type ClientHub struct {
*Hub
sent uint16
rcvd uint16
}

func (h *ClientHub) heartbeat() {
ticker := time.NewTicker(time.Duration(Heartbeat) * time.Second)
heartbeat := getHeartbeat()
ticker := time.NewTicker(heartbeat)
defer ticker.Stop()

maxSpan := Timeout / Heartbeat
if maxSpan <= TunnelMinSpan {
maxSpan = TunnelMinSpan
maxSpan := int(getTimeout() / heartbeat)
if maxSpan <= tunnelMinSpan {
maxSpan = tunnelMinSpan
}

Debug("maxspan: %d", maxSpan)
for range ticker.C {
// id overflow
span := h.sent - h.rcvd
span := 0xffff - h.rcvd + h.sent + 1
if int(span) >= maxSpan {
Error("tunnel(%v) timeout, sent:%d, rcvd:%d", h.Hub.tunnel, h.sent, h.rcvd)
h.Hub.Close()
Expand Down
31 changes: 25 additions & 6 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,34 @@ import (
)

const (
TunnelMaxId = ^uint16(0)
TunnelMinSpan = 3 // 3次心跳无回应则断开
TunnelMaxId = ^uint16(0)

TunnelPacketSize = 8192
TunnelKeepAlivePeriod = time.Second * 180

defaultHeartbeat = 1
tunnelMinSpan = 3 // 3次心跳无回应则断开
)

var (
Heartbeat int = 1
Timeout int = 0
LogLevel uint = 1
mpool = NewMPool(TunnelPacketSize)
// Heartbeat interval for tunnel heartbeat, seconds.
Heartbeat int = 1 // seconds

// Timeout for tunnel write/read, seconds
Timeout int = 0 //

// LogLevel .
LogLevel uint = 1
mpool = NewMPool(TunnelPacketSize)
)

func getHeartbeat() time.Duration {
if Heartbeat <= 0 {
Heartbeat = defaultHeartbeat
}
return time.Duration(Heartbeat) * time.Second
}

func getTimeout() time.Duration {
return time.Duration(Timeout) * time.Second
}

0 comments on commit 02281ea

Please sign in to comment.