Skip to content

Commit

Permalink
Fixes race condition with token rate limit INCR ?
Browse files Browse the repository at this point in the history
  • Loading branch information
denverquane committed Nov 29, 2020
1 parent 35a9399 commit 69503d2
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions galactus/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,11 @@ func (tokenProvider *TokenProvider) getAnySession(guildID string, limit int) (*d
}

func (tokenProvider *TokenProvider) IncrGuildTokenComboLock(guildID, hashToken string) {
v := int64(0)
vStr, err := tokenProvider.client.Get(context.Background(), guildTokenLock(guildID, hashToken)).Result()
if err == redis.Nil {
v = 0
} else {
v, _ = strconv.ParseInt(vStr, 10, 64)
}
v++

//5 second TTL
err = tokenProvider.client.Set(context.Background(), guildTokenLock(guildID, hashToken), v, time.Second*5).Err()
err := tokenProvider.client.Incr(context.Background(), guildTokenLock(guildID, hashToken)).Err()
if err != nil {
log.Println(err)
log.Println()
}
tokenProvider.client.Expire(context.Background(), guildTokenLock(guildID, hashToken), time.Second*5)
}

func (tokenProvider *TokenProvider) CanUseGuildTokenCombo(guildID, hashToken string) bool {
Expand Down

0 comments on commit 69503d2

Please sign in to comment.