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