From 1d7da975d35ca89b3f4f89c22def0c653d61fee8 Mon Sep 17 00:00:00 2001 From: MineGame159 Date: Sat, 12 Oct 2024 18:15:31 +0200 Subject: [PATCH] Forgor synchronization --- codes/codes.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/codes/codes.go b/codes/codes.go index 5a73175..29b4fe0 100644 --- a/codes/codes.go +++ b/codes/codes.go @@ -4,6 +4,7 @@ import ( "errors" "go.minekube.com/gate/pkg/util/uuid" "math/rand" + "sync" "time" ) @@ -13,6 +14,7 @@ type codeData struct { } var codes []codeData +var mutex sync.Mutex var ErrInvalidCode = errors.New("invalid code") @@ -21,6 +23,9 @@ func init() { } func New(id uuid.UUID) int { + mutex.Lock() + defer mutex.Unlock() + start := rand.Intn(len(codes)) i := start @@ -55,6 +60,9 @@ func New(id uuid.UUID) int { } func Retrieve(code int) (uuid.UUID, error) { + mutex.Lock() + defer mutex.Unlock() + data := codes[code] if data.time.Add(time.Minute * 5).Before(time.Now()) {