From 3bdd348d50768b6174d0d1f7aa88488ffbe15e72 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 13 Jul 2022 17:28:46 -0400 Subject: [PATCH] reduce account updates --- internal/worker/notifications.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/worker/notifications.go b/internal/worker/notifications.go index db7b8dc..c6373e9 100644 --- a/internal/worker/notifications.go +++ b/internal/worker/notifications.go @@ -165,9 +165,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { return } - account.CheckCount++ - newAccount := account.CheckCount == 1 - defer func(acc *domain.Account) { if err = nc.accountRepo.Update(nc, acc); err != nil { nc.logger.Error("failed to update account", @@ -212,6 +209,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { account.AccessToken = tokens.AccessToken account.RefreshToken = tokens.RefreshToken account.TokenExpiresAt = now.Add(tokens.Expiry) + _ = nc.accountRepo.Update(nc, &account) // Refresh client rac = nc.reddit.NewAuthenticatedClient(account.AccountID, tokens.RefreshToken, tokens.AccessToken) @@ -270,16 +268,20 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { for _, msg := range msgs.Children { if !msg.IsDeleted() { account.LastMessageID = msg.FullName() + _ = nc.accountRepo.Update(nc, &account) break } } // Let's populate this with the latest message so we don't flood users with stuff - if newAccount { + if account.CheckCount == 0 { nc.logger.Debug("populating first message id to prevent spamming", zap.String("account#reddit_account_id", id), zap.String("account#username", account.NormalizedUsername()), ) + + account.CheckCount = 1 + _ = nc.accountRepo.Update(nc, &account) return }