Skip to content
Snippets Groups Projects
Commit fc389b6a authored by Edward Welch's avatar Edward Welch Committed by Ed
Browse files

don't delete elements from a map while iterating over it

improve locking of positions
parent 85a9e826
No related branches found
No related tags found
No related merge requests found
......@@ -84,8 +84,12 @@ func (p *Positions) Get(path string) int64 {
// Remove removes the position tracking for a filepath
func (p *Positions) Remove(path string) {
p.mtx.Lock()
defer p.mtx.Unlock()
p.remove(path)
}
func (p *Positions) remove(path string) {
delete(p.positions, path)
p.mtx.Unlock()
}
// SyncPeriod returns how often the positions file gets resynced
......@@ -126,11 +130,14 @@ func (p *Positions) save() {
}
func (p *Positions) cleanup() {
p.mtx.Lock()
defer p.mtx.Unlock()
toRemove := []string{}
for k := range p.positions {
if _, err := os.Stat(k); err != nil {
if os.IsNotExist(err) {
// File no longer exists.
p.Remove(k)
toRemove = append(toRemove, k)
return
}
// Can't determine if file exists or not, some other error.
......@@ -139,6 +146,9 @@ func (p *Positions) cleanup() {
}
}
for _, tr := range toRemove {
p.remove(tr)
}
}
func readPositionsFile(filename string) (map[string]int64, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment