Don't hold memory used for sending indexes forever

This commit is contained in:
Jakob Borg 2014-07-30 20:08:04 +02:00
parent f4d1632506
commit c47aebdd2a

View File

@ -563,31 +563,36 @@ func (m *Model) AddConnection(rawConn io.Closer, protoConn protocol.Connection)
func sendIndexes(conn protocol.Connection, repo string, fs *files.Set) {
nodeID := conn.ID()
name := conn.Name()
var err error
if debug {
l.Debugf("sendIndexes for %s-%s@/%q starting", nodeID, name, repo)
}
initial := true
minLocalVer := uint64(0)
var err error
defer func() {
if debug {
l.Debugf("sendIndexes for %s-%s@/%q exiting: %v", nodeID, name, repo, err)
}
}()
minLocalVer, err := sendIndexTo(true, 0, conn, repo, fs)
for err == nil {
if !initial {
time.Sleep(5 * time.Second)
if fs.LocalVersion(protocol.LocalNodeID) <= minLocalVer {
continue
}
}
minLocalVer, err = sendIndexTo(false, minLocalVer, conn, repo, fs)
}
}
func sendIndexTo(initial bool, minLocalVer uint64, conn protocol.Connection, repo string, fs *files.Set) (uint64, error) {
nodeID := conn.ID()
name := conn.Name()
batch := make([]protocol.FileInfo, 0, indexBatchSize)
maxLocalVer := uint64(0)
var err error
fs.WithHave(protocol.LocalNodeID, func(f protocol.FileInfo) bool {
if f.LocalVersion <= minLocalVer {
@ -623,21 +628,19 @@ func sendIndexes(conn protocol.Connection, repo string, fs *files.Set) {
return true
})
if initial {
if initial && err == nil {
err = conn.Index(repo, batch)
if debug && err == nil {
l.Debugf("sendIndexes for %s-%s/%q: %d files (small initial index)", nodeID, name, repo, len(batch))
}
initial = false
} else if len(batch) > 0 {
} else if len(batch) > 0 && err == nil {
err = conn.IndexUpdate(repo, batch)
if debug && err == nil {
l.Debugf("sendIndexes for %s-%s/%q: %d files (last batch)", nodeID, name, repo, len(batch))
}
}
minLocalVer = maxLocalVer
}
return maxLocalVer, err
}
func (m *Model) updateLocal(repo string, f protocol.FileInfo) {