From 8dc2e8a6427f12da29d2a382e77f6fa53f5156fc Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Mon, 8 Aug 2022 16:55:40 +0800 Subject: [PATCH] Improve inventory actor timers --- .../Inventories/DaprActors/InventoryActor.cs | 21 +++++++++---------- .../OrleansGrains/InventoryGrain.cs | 21 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/plugins/Inventories/DaprActors/src/EasyAbp.EShop.Plugins.Inventories.DaprActors/EasyAbp/EShop/Plugins/Inventories/DaprActors/InventoryActor.cs b/plugins/Inventories/DaprActors/src/EasyAbp.EShop.Plugins.Inventories.DaprActors/EasyAbp/EShop/Plugins/Inventories/DaprActors/InventoryActor.cs index 29f5555e..ce3ca9b0 100644 --- a/plugins/Inventories/DaprActors/src/EasyAbp.EShop.Plugins.Inventories.DaprActors/EasyAbp/EShop/Plugins/Inventories/DaprActors/InventoryActor.cs +++ b/plugins/Inventories/DaprActors/src/EasyAbp.EShop.Plugins.Inventories.DaprActors/EasyAbp/EShop/Plugins/Inventories/DaprActors/InventoryActor.cs @@ -2,7 +2,6 @@ using System; using System.Threading.Tasks; using Dapr; using Dapr.Actors.Runtime; -using JetBrains.Annotations; namespace EasyAbp.EShop.Plugins.Inventories.DaprActors; @@ -12,9 +11,6 @@ public class InventoryActor : Actor, IInventoryActor protected bool FlashSalesInventoryUpdated { get; set; } - [CanBeNull] - protected ActorTimer FlashSalesPersistInventoryTimer { get; set; } - public InventoryActor(ActorHost host) : base(host) { } @@ -39,8 +35,6 @@ public class InventoryActor : Actor, IInventoryActor } else { - FlashSalesInventoryUpdated = true; - await TryRegisterFlashSalesPersistInventoryTimerAsync(); } } @@ -57,19 +51,24 @@ public class InventoryActor : Actor, IInventoryActor } else { - FlashSalesInventoryUpdated = true; - await TryRegisterFlashSalesPersistInventoryTimerAsync(); } } protected virtual async Task TryRegisterFlashSalesPersistInventoryTimerAsync() { - FlashSalesPersistInventoryTimer ??= await RegisterTimerAsync(null, nameof(PeriodicSetInventoryStateAsync), - null, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3)); + if (FlashSalesInventoryUpdated) + { + return; + } + + FlashSalesInventoryUpdated = true; + + await RegisterTimerAsync(null, nameof(TimerSetInventoryStateAsync), null, TimeSpan.FromSeconds(5), + TimeSpan.FromMilliseconds(-1)); } - protected virtual async Task PeriodicSetInventoryStateAsync() + protected virtual async Task TimerSetInventoryStateAsync() { if (!FlashSalesInventoryUpdated) { diff --git a/plugins/Inventories/OrleansGrains/src/EasyAbp.EShop.Plugins.Inventories.OrleansGrains/EasyAbp/EShop/Plugins/Inventories/OrleansGrains/InventoryGrain.cs b/plugins/Inventories/OrleansGrains/src/EasyAbp.EShop.Plugins.Inventories.OrleansGrains/EasyAbp/EShop/Plugins/Inventories/OrleansGrains/InventoryGrain.cs index 1815bcb3..cb4aa189 100644 --- a/plugins/Inventories/OrleansGrains/src/EasyAbp.EShop.Plugins.Inventories.OrleansGrains/EasyAbp/EShop/Plugins/Inventories/OrleansGrains/InventoryGrain.cs +++ b/plugins/Inventories/OrleansGrains/src/EasyAbp.EShop.Plugins.Inventories.OrleansGrains/EasyAbp/EShop/Plugins/Inventories/OrleansGrains/InventoryGrain.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using JetBrains.Annotations; using Orleans; using Orleans.Providers; using Orleans.Runtime; @@ -14,9 +13,6 @@ public class InventoryGrain : Grain, IInventoryGrain protected bool FlashSalesInventoryUpdated { get; set; } - [CanBeNull] - protected IDisposable FlashSalesPersistInventoryTimer { get; set; } - public virtual Task GetInventoryStateAsync() => Task.FromResult(State); public virtual async Task IncreaseInventoryAsync(int quantity, bool decreaseSold, bool isFlashSale) @@ -29,8 +25,6 @@ public class InventoryGrain : Grain, IInventoryGrain } else { - FlashSalesInventoryUpdated = true; - TryRegisterFlashSalesPersistInventoryTimer(); } } @@ -45,19 +39,24 @@ public class InventoryGrain : Grain, IInventoryGrain } else { - FlashSalesInventoryUpdated = true; - TryRegisterFlashSalesPersistInventoryTimer(); } } protected virtual void TryRegisterFlashSalesPersistInventoryTimer() { - FlashSalesPersistInventoryTimer ??= RegisterTimer(PeriodicWriteInventoryStateAsync, - new object(), TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3)); + if (FlashSalesInventoryUpdated) + { + return; + } + + FlashSalesInventoryUpdated = true; + + RegisterTimer(TimerWriteInventoryStateAsync, new object(), TimeSpan.FromSeconds(5), + TimeSpan.FromMilliseconds(-1)); } - protected virtual async Task PeriodicWriteInventoryStateAsync(object obj) + protected virtual async Task TimerWriteInventoryStateAsync(object obj) { if (!FlashSalesInventoryUpdated) {