reduce account updates by half

This commit is contained in:
Andre Medeiros 2022-05-22 19:57:29 -04:00
parent b92e3fc374
commit 6f8f93f3ab
2 changed files with 10 additions and 25 deletions

View file

@ -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()
}

View file

@ -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 {
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 next_notification_check_at for account")
return
}).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{