Use `DateTime.UtcNow` in `TokenCleanupService`.

Resolve #10956
pull/10962/head
maliming 3 years ago
parent de7cae0e00
commit bc986f3f81
No known key found for this signature in database
GPG Key ID: 096224957E51C89E

@ -1,9 +1,9 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Devices;
using Volo.Abp.IdentityServer.Grants; using Volo.Abp.IdentityServer.Grants;
using Volo.Abp.Timing;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.IdentityServer.Tokens namespace Volo.Abp.IdentityServer.Tokens
@ -12,43 +12,32 @@ namespace Volo.Abp.IdentityServer.Tokens
{ {
protected IPersistentGrantRepository PersistentGrantRepository { get; } protected IPersistentGrantRepository PersistentGrantRepository { get; }
protected IDeviceFlowCodesRepository DeviceFlowCodesRepository { get; } protected IDeviceFlowCodesRepository DeviceFlowCodesRepository { get; }
protected IClock Clock { get; }
protected TokenCleanupOptions Options { get; } protected TokenCleanupOptions Options { get; }
public TokenCleanupService( public TokenCleanupService(
IPersistentGrantRepository persistentGrantRepository, IPersistentGrantRepository persistentGrantRepository,
IDeviceFlowCodesRepository deviceFlowCodesRepository, IDeviceFlowCodesRepository deviceFlowCodesRepository,
IClock clock,
IOptions<TokenCleanupOptions> options) IOptions<TokenCleanupOptions> options)
{ {
PersistentGrantRepository = persistentGrantRepository; PersistentGrantRepository = persistentGrantRepository;
DeviceFlowCodesRepository = deviceFlowCodesRepository; DeviceFlowCodesRepository = deviceFlowCodesRepository;
Clock = clock;
Options = options.Value; Options = options.Value;
} }
public virtual async Task CleanAsync() public virtual async Task CleanAsync()
{ {
await RemoveGrantsAsync() await RemoveGrantsAsync();
;
await RemoveDeviceCodesAsync(); await RemoveDeviceCodesAsync();
} }
[UnitOfWork] [UnitOfWork]
protected virtual async Task RemoveGrantsAsync() protected virtual async Task RemoveGrantsAsync()
{ {
for (int i = 0; i < Options.CleanupLoopCount; i++) for (var i = 0; i < Options.CleanupLoopCount; i++)
{ {
var persistentGrants = await PersistentGrantRepository var persistentGrants = await PersistentGrantRepository.GetListByExpirationAsync(DateTime.UtcNow, Options.CleanupBatchSize);
.GetListByExpirationAsync(Clock.Now, Options.CleanupBatchSize);
//TODO: Can be optimized if the repository implements the batch deletion await PersistentGrantRepository.DeleteManyAsync(persistentGrants);
foreach (var persistentGrant in persistentGrants)
{
await PersistentGrantRepository
.DeleteAsync(persistentGrant);
}
//No need to continue to query if it gets more than max items. //No need to continue to query if it gets more than max items.
if (persistentGrants.Count < Options.CleanupBatchSize) if (persistentGrants.Count < Options.CleanupBatchSize)
@ -60,17 +49,11 @@ namespace Volo.Abp.IdentityServer.Tokens
protected virtual async Task RemoveDeviceCodesAsync() protected virtual async Task RemoveDeviceCodesAsync()
{ {
for (int i = 0; i < Options.CleanupLoopCount; i++) for (var i = 0; i < Options.CleanupLoopCount; i++)
{ {
var deviceFlowCodeses = await DeviceFlowCodesRepository var deviceFlowCodeses = await DeviceFlowCodesRepository.GetListByExpirationAsync(DateTime.UtcNow, Options.CleanupBatchSize);
.GetListByExpirationAsync(Clock.Now, Options.CleanupBatchSize);
//TODO: Can be optimized if the repository implements the batch deletion await DeviceFlowCodesRepository.DeleteManyAsync(deviceFlowCodeses);
foreach (var deviceFlowCodes in deviceFlowCodeses)
{
await DeviceFlowCodesRepository
.DeleteAsync(deviceFlowCodes);
}
//No need to continue to query if it gets more than max items. //No need to continue to query if it gets more than max items.
if (deviceFlowCodeses.Count < Options.CleanupBatchSize) if (deviceFlowCodeses.Count < Options.CleanupBatchSize)
@ -80,4 +63,4 @@ namespace Volo.Abp.IdentityServer.Tokens
} }
} }
} }
} }

Loading…
Cancel
Save