From 40b2c7051bb0c2639b3aed0bd818fed61b6cb77b Mon Sep 17 00:00:00 2001 From: Noel Date: Thu, 30 Dec 2021 06:56:41 -0700 Subject: [PATCH] chore: actually store timeout in Redis and pop from cache once finished --- pkg/client.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/client.go b/pkg/client.go index 37e8dca..41037c3 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -23,6 +23,7 @@ package pkg import ( "context" "encoding/json" + "fmt" "github.com/gorilla/websocket" "github.com/sirupsen/logrus" "strings" @@ -78,6 +79,16 @@ 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): @@ -85,9 +96,20 @@ func (c *Client) HandleTimeout(t Timeout) { 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,