From bc986f3f81ebb190577f94744c291ce85ee57bbb Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 16 Dec 2021 14:24:05 +0800 Subject: [PATCH] Use `DateTime.UtcNow` in `TokenCleanupService`. Resolve #10956 --- .../Tokens/TokenCleanupService.cs | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs index dfa66b9e88..6c78071d8b 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Tokens/TokenCleanupService.cs @@ -1,9 +1,9 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.IdentityServer.Devices; using Volo.Abp.IdentityServer.Grants; -using Volo.Abp.Timing; using Volo.Abp.Uow; namespace Volo.Abp.IdentityServer.Tokens @@ -12,43 +12,32 @@ namespace Volo.Abp.IdentityServer.Tokens { protected IPersistentGrantRepository PersistentGrantRepository { get; } protected IDeviceFlowCodesRepository DeviceFlowCodesRepository { get; } - protected IClock Clock { get; } protected TokenCleanupOptions Options { get; } public TokenCleanupService( IPersistentGrantRepository persistentGrantRepository, IDeviceFlowCodesRepository deviceFlowCodesRepository, - IClock clock, IOptions options) { PersistentGrantRepository = persistentGrantRepository; DeviceFlowCodesRepository = deviceFlowCodesRepository; - Clock = clock; Options = options.Value; } public virtual async Task CleanAsync() { - await RemoveGrantsAsync() - ; - + await RemoveGrantsAsync(); await RemoveDeviceCodesAsync(); } [UnitOfWork] 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 - .GetListByExpirationAsync(Clock.Now, Options.CleanupBatchSize); + var persistentGrants = await PersistentGrantRepository.GetListByExpirationAsync(DateTime.UtcNow, Options.CleanupBatchSize); - //TODO: Can be optimized if the repository implements the batch deletion - foreach (var persistentGrant in persistentGrants) - { - await PersistentGrantRepository - .DeleteAsync(persistentGrant); - } + await PersistentGrantRepository.DeleteManyAsync(persistentGrants); //No need to continue to query if it gets more than max items. if (persistentGrants.Count < Options.CleanupBatchSize) @@ -60,17 +49,11 @@ namespace Volo.Abp.IdentityServer.Tokens 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 - .GetListByExpirationAsync(Clock.Now, Options.CleanupBatchSize); + var deviceFlowCodeses = await DeviceFlowCodesRepository.GetListByExpirationAsync(DateTime.UtcNow, Options.CleanupBatchSize); - //TODO: Can be optimized if the repository implements the batch deletion - foreach (var deviceFlowCodes in deviceFlowCodeses) - { - await DeviceFlowCodesRepository - .DeleteAsync(deviceFlowCodes); - } + await DeviceFlowCodesRepository.DeleteManyAsync(deviceFlowCodeses); //No need to continue to query if it gets more than max items. if (deviceFlowCodeses.Count < Options.CleanupBatchSize) @@ -80,4 +63,4 @@ namespace Volo.Abp.IdentityServer.Tokens } } } -} \ No newline at end of file +}