Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
chore: actually store timeout in Redis and pop from cache once finished
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Dec 30, 2021
1 parent 5e5aa1f commit 40b2c70
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package pkg
import (
"context"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"strings"
Expand Down Expand Up @@ -78,16 +79,37 @@ func (c *Client) WriteMessage(msg Message) {

func (c *Client) HandleTimeout(t Timeout) {
logrus.Debugf("Told to handle timeout (type=%s; guild=%s; user=%s)", t.Type, t.GuildId, t.UserId)
key := fmt.Sprintf("%s:%s", t.GuildId, t.UserId)
bytes, err := json.Marshal(&t)
if err != nil {
logrus.Errorf("Unable to marshal timeout %v: %v", t, err)
}

if err = Redis.Connection.HMSet(context.TODO(), "nino:timeouts", key, string(bytes)).Err(); err != nil {
logrus.Errorf("Unable to store timeout %v into Redis: %v", t, err)
}

go func() {
select {
case <-time.After(time.Duration(t.ExpiresAt-t.IssuedAt) * time.Millisecond):
{
if !Server.HasClient() {
Server.QueueIn(t)
logrus.Warnf("Client has been disconnected, added pending timeout to replay soon.")

return
}

// pop from redis
_, err := Redis.Connection.HGet(context.TODO(), "nino:timeouts", fmt.Sprintf("%s:%s", t.GuildId, t.UserId)).Result()
if err != nil {
logrus.Warnf("Timeout doesn't exist anymore? %v", err)
}

if err = Redis.Connection.HDel(context.TODO(), "nino:timeouts", fmt.Sprintf("%s:%s", t.GuildId, t.UserId)).Err(); err != nil {
logrus.Errorf("Unable to delete timeout from cache: %v", err)
}

c.WriteMessage(Message{
OP: Apply,
Data: t,
Expand Down

0 comments on commit 40b2c70

Please sign in to comment.