mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-10 15:20:56 +00:00
Do ping check after 5 minute inactivity
This commit is contained in:
parent
768a7d5052
commit
bef9ccfa71
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/calmh/syncthing/buffers"
|
"github.com/calmh/syncthing/buffers"
|
||||||
)
|
)
|
||||||
@ -40,6 +41,7 @@ type Model interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
|
ID string
|
||||||
receiver Model
|
receiver Model
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
mreader *marshalReader
|
mreader *marshalReader
|
||||||
@ -50,7 +52,8 @@ type Connection struct {
|
|||||||
closedLock sync.RWMutex
|
closedLock sync.RWMutex
|
||||||
awaiting map[int]chan asyncResult
|
awaiting map[int]chan asyncResult
|
||||||
nextId int
|
nextId int
|
||||||
ID string
|
lastReceive time.Time
|
||||||
|
peerLatency time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrClosed = errors.New("Connection closed")
|
var ErrClosed = errors.New("Connection closed")
|
||||||
@ -60,6 +63,9 @@ type asyncResult struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pingTimeout = 30 * time.Second
|
||||||
|
const pingIdleTime = 5 * time.Minute
|
||||||
|
|
||||||
func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver Model) *Connection {
|
func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver Model) *Connection {
|
||||||
flrd := flate.NewReader(reader)
|
flrd := flate.NewReader(reader)
|
||||||
flwr, err := flate.NewWriter(writer, flate.BestSpeed)
|
flwr, err := flate.NewWriter(writer, flate.BestSpeed)
|
||||||
@ -78,6 +84,7 @@ func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver M
|
|||||||
}
|
}
|
||||||
|
|
||||||
go c.readerLoop()
|
go c.readerLoop()
|
||||||
|
go c.pingerLoop()
|
||||||
|
|
||||||
return &c
|
return &c
|
||||||
}
|
}
|
||||||
@ -166,6 +173,10 @@ func (c *Connection) readerLoop() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.wLock.Lock()
|
||||||
|
c.lastReceive = time.Now()
|
||||||
|
c.wLock.Unlock()
|
||||||
|
|
||||||
switch hdr.msgType {
|
switch hdr.msgType {
|
||||||
case messageTypeIndex:
|
case messageTypeIndex:
|
||||||
files := c.mreader.readIndex()
|
files := c.mreader.readIndex()
|
||||||
@ -237,3 +248,29 @@ func (c *Connection) processRequest(msgID int) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Connection) pingerLoop() {
|
||||||
|
var rc = make(chan time.Duration)
|
||||||
|
for !c.isClosed() {
|
||||||
|
c.wLock.RLock()
|
||||||
|
lr := c.lastReceive
|
||||||
|
c.wLock.RUnlock()
|
||||||
|
|
||||||
|
if time.Since(lr) > pingIdleTime {
|
||||||
|
go func() {
|
||||||
|
t0 := time.Now()
|
||||||
|
c.Ping()
|
||||||
|
rc <- time.Since(t0)
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case lat := <-rc:
|
||||||
|
c.wLock.Lock()
|
||||||
|
c.peerLatency = (c.peerLatency + lat) / 2
|
||||||
|
c.wLock.Unlock()
|
||||||
|
case <-time.After(pingTimeout):
|
||||||
|
c.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user