Skip to content

Commit

Permalink
Bumped req/token/5sec, fixed token fallback, using Redis INCR properly
Browse files Browse the repository at this point in the history
  • Loading branch information
denverquane committed Nov 30, 2020
1 parent 0571ed3 commit 6ec30ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ storage of temporary tokens, and, crucially, communication between the Capture c
## **Do not provide unless you know what you're doing**:
* `NUM_SHARDS`: Should match whatever automuteus is using
* `SHARD_ID`: Probably just use 0
* `MAX_REQ_5_SEC`: How many Discord API mute/deafens should be issued per token per 5 second window. Defaults to 6 (ratelimits
returned by Discord are [5-10]/5sec, so 6 is a conservative heuristic)
* `MAX_REQ_5_SEC`: How many Discord API mute/deafens should be issued per token per 5 second window. Defaults to 7 (ratelimits
returned by Discord are anywhere from [5-10]/5sec, so 7 is a decent heuristic)
50 changes: 16 additions & 34 deletions galactus/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (tokenProvider *TokenProvider) getAnySession(guildID string, limit int) (*d
return nil, ""
}
//if this token isn't potentially rate-limited
if tokenProvider.CanUseGuildTokenCombo(guildID, hToken) {
if tokenProvider.IncrAndTestGuildTokenComboLock(guildID, hToken) {
if sess, ok := tokenProvider.activeSessions[hToken]; ok {
return sess, hToken
} else {
Expand All @@ -170,33 +170,21 @@ func (tokenProvider *TokenProvider) getAnySession(guildID string, limit int) (*d
return nil, ""
}

func (tokenProvider *TokenProvider) IncrGuildTokenComboLock(guildID, hashToken string) {
err := tokenProvider.client.Incr(context.Background(), guildTokenLock(guildID, hashToken)).Err()
if err != nil {
log.Println()
}
err = tokenProvider.client.Expire(context.Background(), guildTokenLock(guildID, hashToken), time.Second*5).Err()
func (tokenProvider *TokenProvider) IncrAndTestGuildTokenComboLock(guildID, hashToken string) bool {
i, err := tokenProvider.client.Incr(context.Background(), guildTokenLock(guildID, hashToken)).Result()
if err != nil {
log.Println(err)
}
}

func (tokenProvider *TokenProvider) CanUseGuildTokenCombo(guildID, hashToken string) bool {
res, err := tokenProvider.client.Get(context.Background(), guildTokenLock(guildID, hashToken)).Result()
if err == redis.Nil {
return true
} else if err != nil {
log.Println(err)
return true
}
i, err := strconv.ParseInt(res, 10, 64)
err = tokenProvider.client.Expire(context.Background(), guildTokenLock(guildID, hashToken), time.Second*5).Err()
if err != nil {
log.Println(err)
return true
}
log.Printf("Request count on this guild/token: %d\n", i)

return i < tokenProvider.maxRequests5Seconds
usable := i < tokenProvider.maxRequests5Seconds
log.Printf("Token %s on guild %s is at count %d. Skipping: %v", hashToken, guildID, i, usable)

return usable
}

func (tokenProvider *TokenProvider) BlacklistTokenForDuration(guildID, hashToken string, duration time.Duration) error {
Expand Down Expand Up @@ -245,8 +233,6 @@ func (tokenProvider *TokenProvider) Run(port string) {
}
mdscLock := sync.Mutex{}

sessLock := sync.Mutex{}

for _, modifyReq := range userModifications.Users {
wg.Add(1)

Expand All @@ -256,30 +242,27 @@ func (tokenProvider *TokenProvider) Run(port string) {

userIdStr := strconv.FormatUint(request.UserID, 10)
if limit > 0 {
sessLock.Lock()
sess, hToken := tokenProvider.getAnySession(guildID, limit)
if sess != nil {
tokenProvider.IncrGuildTokenComboLock(guildID, hToken)
sessLock.Unlock()

err := discord.ApplyMuteDeaf(sess, guildID, userIdStr, request.Mute, request.Deaf)
if err != nil {
log.Println("Failed to apply mute to player with error:")
log.Println(err)
} else {
log.Printf("Successfully applied mute=%v, deaf=%v to User %d using secondary bot: %s\n", request.Mute, request.Deaf, request.UserID, hToken)
mdscLock.Lock()
mdsc.Worker++
mdscLock.Unlock()
return
}
log.Printf("Successfully applied mute=%v, deaf=%v to User %d using secondary bot: %s\n", request.Mute, request.Deaf, request.UserID, hToken)
mdscLock.Lock()
mdsc.Worker++
mdscLock.Unlock()
return
} else {
sessLock.Unlock()
log.Println("No secondary bot tokens found. Trying other methods")
}
} else {
log.Println("Guild has no access to secondary bot tokens; skipping")
}
//this is cheeky, but use the connect code as part of the lock; don't issue too many requests on the capture client w/ this code
if tokenProvider.CanUseGuildTokenCombo(guildID, connectCode) {
if tokenProvider.IncrAndTestGuildTokenComboLock(guildID, connectCode) {
//if the secondary token didn't work, then next we try the client-side capture request
task := discord.NewModifyTask(gid, request.UserID, discord.NoNickPatchParams{
Deaf: request.Deaf,
Expand Down Expand Up @@ -313,7 +296,6 @@ func (tokenProvider *TokenProvider) Run(port string) {
res := <-acked
if res {
log.Println("Successful mute/deafen using client capture bot!")
tokenProvider.IncrGuildTokenComboLock(guildID, connectCode)
mdscLock.Lock()
mdsc.Capture++
mdscLock.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const DefaultGalactusPort = "5858"
const DefaultBrokerPort = "8123"
const DefaultMaxRequests5Sec int64 = 6
const DefaultMaxRequests5Sec int64 = 7

func main() {
botToken := os.Getenv("DISCORD_BOT_TOKEN")
Expand Down

0 comments on commit 6ec30ae

Please sign in to comment.