Skip to content

Commit

Permalink
Update authorization with main (#20)
Browse files Browse the repository at this point in the history
* Made gh action trigger on PRs also to get around stuck status checks (#19)

* Made gh action trigger on PRs also to get around stuck status checks

* Modified to trigger only on PRs

* Renamed check

* Add Token to Queue (#18)

* implemented add token for queue store

* implemented create token route

* Fix code style issues with gofmt

* read tokens in the queue in readqueue

* Fix code style issues with gofmt

* implemented token number

* Fix code style issues with gofmt

* added read token route

* Fix code style issues with gofmt

* separated name length constants

* log insert error for add token

* Fix code style issues with gofmt

* refactoring mongo store

* Fix code style issues with gofmt

Co-authored-by: Lint Action <[email protected]>

Co-authored-by: Nithin <[email protected]>
Co-authored-by: Lint Action <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2022
1 parent 6b9bfb4 commit de112cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/vet_and_format.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Post-push-checks
name: PR-checks

on: [push]
on: [pull_request]

jobs:
build-check:
Expand Down
13 changes: 8 additions & 5 deletions internal/datastore/mongo_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,21 @@ func (mongodb MongoDB) AddTokenToQueue(id db.QueueId, token db.Token) (db.TokenI

// Lock queue to ensure 2 concurrent add tokens don't happen
queueMutex.Lock()
// Unlock the mutex before returning
defer queueMutex.Unlock()

// if there were 2 concurrent calls to this function, 2 tokens might get the same number
max, err := mongodb.GetMaxToken(id)

if err != nil {
log.Fatal(err)
queueMutex.Unlock()
return token.Id, err
}

token.TokenNumber = max + 1

result, err := mongodb.Token.InsertOne(context.TODO(), token)

queueMutex.Unlock()

if err != nil {
log.Println(err)
return token.Id, err
Expand All @@ -177,17 +176,19 @@ func (mongodb MongoDB) AddTokenToQueue(id db.QueueId, token db.Token) (db.TokenI
}

func (mongodb MongoDB) GetMaxToken(id db.QueueId) (uint32, error) {
// select all tokens that belong to this queue
filter := bson.D{
// queueid is a string this time
{Key: "queueid", Value: id},
}

sort := bson.D{
// sort tokens by descending order of token number
sortFilter := bson.D{
{Key: "tokennumber", Value: -1},
}

findOptions := options.Find()
findOptions.SetSort(sort)
findOptions.SetSort(sortFilter)
findOptions.SetLimit(1)

cursor, err := mongodb.Token.Find(context.TODO(), filter, findOptions)
Expand All @@ -207,6 +208,8 @@ func (mongodb MongoDB) GetMaxToken(id db.QueueId) (uint32, error) {
return 0, nil
}

// return the token number of the first token
// first token is highest as the list is in descending order
return tokens[0].TokenNumber, nil
}

Expand Down

0 comments on commit de112cf

Please sign in to comment.