this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

update account status when accountLimit is increased

+31 -5
+7 -2
cmd/relay/handlers_admin.go
··· 437 437 438 438 type RateLimitChangeRequest struct { 439 439 Hostname string `json:"host"` 440 - RepoLimit int64 `json:"repo_limit,omitempty"` 440 + RepoLimit *int64 `json:"repo_limit"` 441 441 } 442 442 443 443 func (s *Service) handleAdminChangeHostRateLimits(c echo.Context) error { ··· 453 453 return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid hostname: %s", err)) 454 454 } 455 455 456 + // catch empty/nil body 457 + if body.RepoLimit == nil { 458 + return echo.NewHTTPError(http.StatusBadRequest, "missing repo_limit parameter") 459 + } 460 + 456 461 host, err := s.relay.GetHost(ctx, hostname) 457 462 if err != nil { 458 463 // TODO: technically, there could be a database error here or something 459 464 return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("unknown hostname: %s", err)) 460 465 } 461 466 462 - if err := s.relay.UpdateHostAccountLimit(ctx, host.ID, body.RepoLimit); err != nil { 467 + if err := s.relay.UpdateHostAccountLimit(ctx, host.ID, *body.RepoLimit); err != nil { 463 468 return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to update limits: %s", err)) 464 469 } 465 470
+24 -3
cmd/relay/relay/host.go
··· 78 78 return err 79 79 } 80 80 81 - // TODO: manage accounts marked as "host-throttled" when host-level account limits change (all in a transaction) 82 - // If limit increased: potentially mark some "host-throttled" accounts as "active" (ordered by UID ascending) 83 - // If limit decreased: potentially mark some "active" accounts as "host-throttled" (ordered by UID descending?) 81 + delta := accountLimit - host.AccountLimit 82 + r.Logger.Info("updating host account limit", "host", host.Hostname, "accountLimit", accountLimit, "previousAccountLimit", host.AccountLimit) 83 + 84 84 if err := r.db.Model(models.Host{}).Where("id = ?", hostID).Update("account_limit", accountLimit).Error; err != nil { 85 85 return err 86 86 } 87 + 88 + // manage accounts marked as "host-throttled" when host-level account limits change. Note that this isn't in a transaction: there is a small chance of race-conditions. 89 + if delta > 0 { 90 + // if limit increased: potentially mark some "host-throttled" accounts as "active" (ordered by UID ascending) 91 + // fetch accounts and update individually. this ensures that account cache is cleared for each (as well as any future code around account status changes) 92 + var accounts []models.Account 93 + if err := r.db.Model(models.Account{}).Where("status = ? AND upstream_status = ? AND host_id = ?", models.AccountStatusHostThrottled, models.AccountStatusActive, host.ID).Order("uid ASC").Limit(int(delta)).Find(&accounts).Error; err != nil { 94 + return err 95 + } 96 + r.Logger.Info("marking host-throttled accounts as active", "count", len(accounts), "delta", delta, "accountLimit", accountLimit, "host", host.Hostname) 97 + for _, acc := range accounts { 98 + // defensive double-check 99 + if acc.Status != models.AccountStatusHostThrottled || acc.HostID != host.ID { 100 + continue 101 + } 102 + if err := r.UpdateAccountLocalStatus(ctx, syntax.DID(acc.DID), models.AccountStatusActive, true); err != nil { 103 + return err 104 + } 105 + } 106 + } 107 + // TODO: If limit decreased: potentially mark some "active" accounts as "host-throttled" (ordered by UID descending?) 87 108 88 109 if r.Slurper.CheckIfSubscribed(host.Hostname) { 89 110 return r.Slurper.UpdateLimiters(host.Hostname, accountLimit, host.Trusted)