Skip to content

Commit

Permalink
Simplify PAR cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersAbel committed Jan 4, 2024
1 parent 8aec575 commit 91b94db
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/EntityFramework.Storage/TokenCleanup/TokenCleanupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,36 +272,19 @@ protected virtual async Task RemovePushedAuthorizationRequestsAsync(Cancellation
{
var found = Int32.MaxValue;

var query = _persistedGrantDbContext.PushedAuthorizationRequests
.Where(par => par.ExpiresAtUtc < DateTime.UtcNow)
.OrderBy(par => par.ExpiresAtUtc);

while (found >= _options.TokenCleanupBatchSize)
{
var query = _persistedGrantDbContext.PushedAuthorizationRequests
.Where(par => par.ExpiresAtUtc < DateTime.UtcNow)
.OrderBy(par => par.ExpiresAtUtc);

var expiredPars = await query
found = await query
.Take(_options.TokenCleanupBatchSize)
.AsNoTracking()
.ToArrayAsync(cancellationToken);

found = expiredPars.Length;
.ExecuteDeleteAsync(cancellationToken);

if (found > 0)
{
_logger.LogInformation("Removing {parCount} stale pushed authorization requests", found);

var foundIds = expiredPars.Select(par => par.Id).ToArray();

var deleteCount = await query
.Where(par => par.ExpiresAtUtc >= expiredPars.First().ExpiresAtUtc && par.ExpiresAtUtc <= expiredPars.Last().ExpiresAtUtc)
.Where(par => foundIds.Contains(par.Id))
.ExecuteDeleteAsync();

if (deleteCount != found)
{
_logger.LogDebug("Tried to remove {grantCount} expired device codes, but only {deleteCount} " +
"was deleted. This indicates that another process has already removed the items.",
found, deleteCount);
}
_logger.LogInformation("Removed {parCount} stale pushed authorization requests", found);
}
}
}
Expand Down

0 comments on commit 91b94db

Please sign in to comment.