challenge-sync-airbus: Avoid concurrent map write

This commit is contained in:
nemunaire 2024-03-27 21:28:59 +01:00
parent 59cf98ead2
commit c53140b88e
1 changed files with 8 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"math"
"os"
"path/filepath"
"sync"
"time"
"srs.epita.fr/fic-server/libfic"
@ -18,6 +19,7 @@ var (
type Walker struct {
LastSync map[string]*TSValue
LastSyncLock sync.RWMutex
Exercices AirbusExercicesBindings
Teams map[string]fic.ExportedTeam
RevTeams map[string]string
@ -158,7 +160,9 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
maxts := TSValue{
Time: time.Time{},
}
w.LastSyncLock.RLock()
ts, ok := w.LastSync[airbusTeam.Name]
w.LastSyncLock.RUnlock()
if ok {
maxts = *ts
} else {
@ -198,7 +202,9 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
maxts.Score = int64(math.Trunc(expected_score))
}
w.LastSyncLock.Lock()
w.LastSync[airbusTeam.Name] = &maxts
w.LastSyncLock.Unlock()
return nil
}
@ -221,12 +227,14 @@ func (w *Walker) BalanceScores() error {
return fmt.Errorf("Unable to award team %s: %w", myteam.Name, err)
}
w.LastSyncLock.Lock()
if _, ok := w.LastSync[airbusTeam.Name]; !ok {
w.LastSync[airbusTeam.Name] = &TSValue{}
}
w.LastSync[airbusTeam.Name].Score = expected_score
w.LastSync[airbusTeam.Name].Time = time.Now()
w.LastSyncLock.Unlock()
}
}