Skip to content

Commit 1d7da97

Browse files
committed
Forgor synchronization
1 parent db228fb commit 1d7da97

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

codes/codes.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"go.minekube.com/gate/pkg/util/uuid"
66
"math/rand"
7+
"sync"
78
"time"
89
)
910

@@ -13,6 +14,7 @@ type codeData struct {
1314
}
1415

1516
var codes []codeData
17+
var mutex sync.Mutex
1618

1719
var ErrInvalidCode = errors.New("invalid code")
1820

@@ -21,6 +23,9 @@ func init() {
2123
}
2224

2325
func New(id uuid.UUID) int {
26+
mutex.Lock()
27+
defer mutex.Unlock()
28+
2429
start := rand.Intn(len(codes))
2530
i := start
2631

@@ -55,6 +60,9 @@ func New(id uuid.UUID) int {
5560
}
5661

5762
func Retrieve(code int) (uuid.UUID, error) {
63+
mutex.Lock()
64+
defer mutex.Unlock()
65+
5866
data := codes[code]
5967

6068
if data.time.Add(time.Minute * 5).Before(time.Now()) {

0 commit comments

Comments
 (0)