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