Browse Source

Improve inventory actor timers

pull/207/head
gdlcf88 4 years ago
parent
commit
8dc2e8a642
  1. 21
      plugins/Inventories/DaprActors/src/EasyAbp.EShop.Plugins.Inventories.DaprActors/EasyAbp/EShop/Plugins/Inventories/DaprActors/InventoryActor.cs
  2. 21
      plugins/Inventories/OrleansGrains/src/EasyAbp.EShop.Plugins.Inventories.OrleansGrains/EasyAbp/EShop/Plugins/Inventories/OrleansGrains/InventoryGrain.cs

21
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)
{

21
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<InventoryStateModel>, IInventoryGrain
protected bool FlashSalesInventoryUpdated { get; set; }
[CanBeNull]
protected IDisposable FlashSalesPersistInventoryTimer { get; set; }
public virtual Task<InventoryStateModel> GetInventoryStateAsync() => Task.FromResult(State);
public virtual async Task IncreaseInventoryAsync(int quantity, bool decreaseSold, bool isFlashSale)
@ -29,8 +25,6 @@ public class InventoryGrain : Grain<InventoryStateModel>, IInventoryGrain
}
else
{
FlashSalesInventoryUpdated = true;
TryRegisterFlashSalesPersistInventoryTimer();
}
}
@ -45,19 +39,24 @@ public class InventoryGrain : Grain<InventoryStateModel>, 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)
{

Loading…
Cancel
Save