Fixing some locking issues

This commit is contained in:
John Donaldson 2022-05-17 13:14:09 -07:00
parent 9793de315a
commit 9a8ec19908
1 changed files with 41 additions and 34 deletions

View File

@ -283,7 +283,6 @@ func (s *State) SetTeamName(teamID, teamName string) error {
return fmt.Errorf("team ID not found in list of valid team IDs") return fmt.Errorf("team ID not found in list of valid team IDs")
} }
s.teamNameLock.RLock() s.teamNameLock.RLock()
_, ok := s.teamNames[teamID] _, ok := s.teamNames[teamID]
s.teamNameLock.RUnlock() s.teamNameLock.RUnlock()
@ -452,7 +451,7 @@ func (s *State) flushPointsLog(newPoints award.List) error {
defer logf.Close() defer logf.Close()
if err != nil { if err != nil {
return fmt.Errorf("Can't write to points log: ", err) return fmt.Errorf("Can't write to points log: %s", err)
} }
for _, pointEntry := range newPoints { for _, pointEntry := range newPoints {
fmt.Fprintln(logf, pointEntry.String()) fmt.Fprintln(logf, pointEntry.String())
@ -569,21 +568,24 @@ func (s *State) collectPoints() {
} else { } else {
log.Print("Award: ", awd.String()) log.Print("Award: ", awd.String())
{
s.pointsLogFileLock.Lock() s.pointsLogFileLock.Lock()
defer s.pointsLogFileLock.Unlock()
logf, err := s.OpenFile("points.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) logf, err := s.OpenFile("points.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
log.Print("Can't append to points log: ", err) log.Print("Can't append to points log: ", err)
s.pointsLogFileLock.Unlock()
return return
} }
fmt.Fprintln(logf, awd.String()) fmt.Fprintln(logf, awd.String())
logf.Close() logf.Close()
s.pointsLogFileLock.Unlock()
}
// Stick this on the cache too // Stick this on the cache too
s.pointsLock.Lock() s.pointsLock.Lock()
defer s.pointsLock.Unlock()
s.pointsLog = append(s.pointsLog, awd) s.pointsLog = append(s.pointsLog, awd)
s.pointsLock.Unlock()
} }
if err := s.Remove(filename); err != nil { if err := s.Remove(filename); err != nil {
@ -746,6 +748,8 @@ func (s *State) updateCaches() {
s.pointsLock.RLock() s.pointsLock.RLock()
defer s.pointsLock.RUnlock() defer s.pointsLock.RUnlock()
// Re-read the points log
{
s.pointsLogFileLock.RLock() s.pointsLogFileLock.RLock()
defer s.pointsLogFileLock.RUnlock() defer s.pointsLogFileLock.RUnlock()
@ -766,9 +770,9 @@ func (s *State) updateCaches() {
pointsLog = append(pointsLog, cur) pointsLog = append(pointsLog, cur)
} }
s.pointsLog = pointsLog s.pointsLog = pointsLog
} }
}
// Only do this if the teams directory has a newer mtime; directories with // Only do this if the teams directory has a newer mtime; directories with
// hundreds of team names can cause NFS I/O storms // hundreds of team names can cause NFS I/O storms
@ -806,12 +810,15 @@ func (s *State) updateCaches() {
} }
} }
// Re-read the messages file
{
s.messageFileLock.RLock() s.messageFileLock.RLock()
defer s.messageFileLock.RUnlock() defer s.messageFileLock.RUnlock()
if bMessages, err := afero.ReadFile(s, "messages.html"); err == nil { if bMessages, err := afero.ReadFile(s, "messages.html"); err == nil {
s.messages = string(bMessages) s.messages = string(bMessages)
} }
}
} }
func (s *State) refresh() { func (s *State) refresh() {