Skip to content

Commit

Permalink
Forgor synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Oct 12, 2024
1 parent db228fb commit 1d7da97
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions codes/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"go.minekube.com/gate/pkg/util/uuid"
"math/rand"
"sync"
"time"
)

Expand All @@ -13,6 +14,7 @@ type codeData struct {
}

var codes []codeData
var mutex sync.Mutex

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

Expand All @@ -21,6 +23,9 @@ func init() {
}

func New(id uuid.UUID) int {
mutex.Lock()
defer mutex.Unlock()

start := rand.Intn(len(codes))
i := start

Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 1d7da97

Please sign in to comment.