Simplify index sending, prevent ping timeout

This commit is contained in:
Jakob Borg
2013-12-31 21:22:49 -05:00
parent 632bcae856
commit 7e0be89052
4 changed files with 53 additions and 33 deletions

View File

@@ -57,6 +57,9 @@ type Connection struct {
nextId int
indexSent map[string]int64
hasSentIndex bool
hasRecvdIndex bool
lastStatistics Statistics
statisticsLock sync.Mutex
}
@@ -126,7 +129,9 @@ func (c *Connection) Index(idx []FileInfo) {
c.mwriter.writeIndex(idx)
err := c.flush()
c.nextId = (c.nextId + 1) & 0xfff
c.hasSentIndex = true
c.Unlock()
if err != nil {
c.Close(err)
return
@@ -252,6 +257,9 @@ loop:
} else {
c.receiver.Index(c.ID, files)
}
c.Lock()
c.hasRecvdIndex = true
c.Unlock()
case messageTypeIndexUpdate:
files := c.mreader.readIndex()
@@ -343,16 +351,23 @@ func (c *Connection) pingerLoop() {
var rc = make(chan bool, 1)
for {
time.Sleep(pingIdleTime / 2)
go func() {
rc <- c.Ping()
}()
select {
case ok := <-rc:
if !ok {
c.Close(fmt.Errorf("Ping failure"))
c.RLock()
ready := c.hasRecvdIndex && c.hasSentIndex
c.RUnlock()
if ready {
go func() {
rc <- c.Ping()
}()
select {
case ok := <-rc:
if !ok {
c.Close(fmt.Errorf("Ping failure"))
}
case <-time.After(pingTimeout):
c.Close(fmt.Errorf("Ping timeout"))
}
case <-time.After(pingTimeout):
c.Close(fmt.Errorf("Ping timeout"))
}
}
}