mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-10 22:17:44 +00:00
reduce account updates by half
This commit is contained in:
parent
b92e3fc374
commit
6f8f93f3ab
2 changed files with 10 additions and 25 deletions
|
@ -115,13 +115,13 @@ func evalScript(ctx context.Context, redis *redis.Client) (string, error) {
|
||||||
for i=1, #ids do
|
for i=1, #ids do
|
||||||
local key = KEYS[1] .. ":" .. ids[i]
|
local key = KEYS[1] .. ":" .. ids[i]
|
||||||
if redis.call("exists", key) == 0 then
|
if redis.call("exists", key) == 0 then
|
||||||
redis.call("setex", key, %d, 1)
|
redis.call("setex", key, %.0f, 1)
|
||||||
retv[#retv + 1] = ids[i]
|
retv[#retv + 1] = ids[i]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return retv
|
return retv
|
||||||
`, int64(domain.NotificationCheckTimeout.Seconds()))
|
`, domain.NotificationCheckTimeout.Seconds())
|
||||||
|
|
||||||
return redis.ScriptLoad(ctx, lua).Result()
|
return redis.ScriptLoad(ctx, lua).Result()
|
||||||
}
|
}
|
||||||
|
|
|
@ -182,13 +182,14 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
|
||||||
account.CheckCount++
|
account.CheckCount++
|
||||||
account.NextNotificationCheckAt = time.Now().Add(domain.NotificationCheckInterval)
|
account.NextNotificationCheckAt = time.Now().Add(domain.NotificationCheckInterval)
|
||||||
|
|
||||||
if err = nc.accountRepo.Update(nc, &account); err != nil {
|
defer func(acc *domain.Account) {
|
||||||
nc.logger.WithFields(logrus.Fields{
|
if err = nc.accountRepo.Update(nc, acc); err != nil {
|
||||||
"account#username": account.NormalizedUsername(),
|
nc.logger.WithFields(logrus.Fields{
|
||||||
"err": err,
|
"account#username": account.NormalizedUsername(),
|
||||||
}).Error("failed to update next_notification_check_at for account")
|
"err": err,
|
||||||
return
|
}).Error("failed to update account")
|
||||||
}
|
}
|
||||||
|
}(&account)
|
||||||
|
|
||||||
rac := nc.reddit.NewAuthenticatedClient(account.AccountID, account.RefreshToken, account.AccessToken)
|
rac := nc.reddit.NewAuthenticatedClient(account.AccountID, account.RefreshToken, account.AccessToken)
|
||||||
if account.TokenExpiresAt.Before(now) {
|
if account.TokenExpiresAt.Before(now) {
|
||||||
|
@ -224,14 +225,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
|
||||||
|
|
||||||
// Refresh client
|
// Refresh client
|
||||||
rac = nc.reddit.NewAuthenticatedClient(account.AccountID, tokens.RefreshToken, tokens.AccessToken)
|
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
|
// 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
|
// Let's populate this with the latest message so we don't flood users with stuff
|
||||||
if newAccount {
|
if newAccount {
|
||||||
nc.logger.WithFields(logrus.Fields{
|
nc.logger.WithFields(logrus.Fields{
|
||||||
|
|
Loading…
Reference in a new issue