From 6f8f93f3abf0db4dfa1b808d69c9bec06d758faf Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sun, 22 May 2022 19:57:29 -0400 Subject: [PATCH] reduce account updates by half --- internal/cmd/scheduler.go | 4 ++-- internal/worker/notifications.go | 31 ++++++++----------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/internal/cmd/scheduler.go b/internal/cmd/scheduler.go index 931cfbb..60344f7 100644 --- a/internal/cmd/scheduler.go +++ b/internal/cmd/scheduler.go @@ -115,13 +115,13 @@ func evalScript(ctx context.Context, redis *redis.Client) (string, error) { for i=1, #ids do local key = KEYS[1] .. ":" .. ids[i] if redis.call("exists", key) == 0 then - redis.call("setex", key, %d, 1) + redis.call("setex", key, %.0f, 1) retv[#retv + 1] = ids[i] end end return retv - `, int64(domain.NotificationCheckTimeout.Seconds())) + `, domain.NotificationCheckTimeout.Seconds()) return redis.ScriptLoad(ctx, lua).Result() } diff --git a/internal/worker/notifications.go b/internal/worker/notifications.go index a1bca6f..123b9c9 100644 --- a/internal/worker/notifications.go +++ b/internal/worker/notifications.go @@ -182,13 +182,14 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { account.CheckCount++ account.NextNotificationCheckAt = time.Now().Add(domain.NotificationCheckInterval) - if err = nc.accountRepo.Update(nc, &account); err != nil { - nc.logger.WithFields(logrus.Fields{ - "account#username": account.NormalizedUsername(), - "err": err, - }).Error("failed to update next_notification_check_at for account") - return - } + defer func(acc *domain.Account) { + if err = nc.accountRepo.Update(nc, acc); err != nil { + nc.logger.WithFields(logrus.Fields{ + "account#username": account.NormalizedUsername(), + "err": err, + }).Error("failed to update account") + } + }(&account) rac := nc.reddit.NewAuthenticatedClient(account.AccountID, account.RefreshToken, account.AccessToken) if account.TokenExpiresAt.Before(now) { @@ -224,14 +225,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { // Refresh client rac = nc.reddit.NewAuthenticatedClient(account.AccountID, tokens.RefreshToken, tokens.AccessToken) - - if err = nc.accountRepo.Update(nc, &account); err != nil { - nc.logger.WithFields(logrus.Fields{ - "account#username": account.NormalizedUsername(), - "err": err, - }).Error("failed to update reddit tokens for account") - return - } } // Only update delay on accounts we can actually check, otherwise it skews @@ -296,14 +289,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { } } - if err = nc.accountRepo.Update(nc, &account); err != nil { - nc.logger.WithFields(logrus.Fields{ - "account#username": account.NormalizedUsername(), - "err": err, - }).Error("failed to update last_message_id for account") - return - } - // Let's populate this with the latest message so we don't flood users with stuff if newAccount { nc.logger.WithFields(logrus.Fields{