Index broadcast pacing

This commit is contained in:
Jakob Borg 2013-12-24 15:21:03 -05:00
parent 3dc199d8df
commit 4151972d3e

View File

@ -33,11 +33,20 @@ type Model struct {
remote map[string]map[string]File remote map[string]map[string]File
need map[string]bool // the files we need to update need map[string]bool // the files we need to update
nodes map[string]*protocol.Connection nodes map[string]*protocol.Connection
lastIdxBcast time.Time
lastIdxBcastRequest time.Time
} }
const ( const (
RemoteFetchers = 4 RemoteFetchers = 4
FlagDeleted = 1 << 12 FlagDeleted = 1 << 12
// Index is broadcasted when a broadcast has been requested and the index
// has been quiscent for idxBcastHoldtime, or it was at least
// idxBcastMaxDelay since we last sent an index.
idxBcastHoldtime = 15 * time.Second
idxBcastMaxDelay = 120 * time.Second
) )
func NewModel(dir string) *Model { func NewModel(dir string) *Model {
@ -51,6 +60,7 @@ func NewModel(dir string) *Model {
} }
go m.printStats() go m.printStats()
go m.broadcastIndexLoop()
return m return m
} }
@ -227,8 +237,19 @@ func (m *Model) ReplaceLocal(fs []File) {
} }
} }
// Must be called with the read lock held.
func (m *Model) broadcastIndex() { func (m *Model) broadcastIndex() {
m.Lock()
defer m.Unlock()
m.lastIdxBcastRequest = time.Now()
}
func (m *Model) broadcastIndexLoop() {
for {
m.RLock()
bcastRequested := m.lastIdxBcastRequest.After(m.lastIdxBcast)
holdtimeExceeded := time.Since(m.lastIdxBcastRequest) > idxBcastHoldtime
maxDelayExceeded := time.Since(m.lastIdxBcast) > idxBcastMaxDelay
if bcastRequested && (holdtimeExceeded || maxDelayExceeded) {
idx := m.protocolIndex() idx := m.protocolIndex()
for _, node := range m.nodes { for _, node := range m.nodes {
node := node node := node
@ -237,6 +258,13 @@ func (m *Model) broadcastIndex() {
} }
go node.Index(idx) go node.Index(idx)
} }
// We write here without the write lock because we are the only
// goroutine that accesses lastIdxBcast
m.lastIdxBcast = time.Now()
}
m.RUnlock()
time.Sleep(idxBcastHoldtime)
}
} }
// markDeletedLocals sets the deleted flag on files that have gone missing locally. // markDeletedLocals sets the deleted flag on files that have gone missing locally.