Browse Source

Merge pull request #207 from EasyAbp/improve-timers

Improve inventory actor timers
pull/208/head
Super 4 years ago
committed by GitHub
parent
commit
7d5eb34bed
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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 System.Threading.Tasks;
using Dapr; using Dapr;
using Dapr.Actors.Runtime; using Dapr.Actors.Runtime;
using JetBrains.Annotations;
namespace EasyAbp.EShop.Plugins.Inventories.DaprActors; namespace EasyAbp.EShop.Plugins.Inventories.DaprActors;
@ -12,9 +11,6 @@ public class InventoryActor : Actor, IInventoryActor
protected bool FlashSalesInventoryUpdated { get; set; } protected bool FlashSalesInventoryUpdated { get; set; }
[CanBeNull]
protected ActorTimer FlashSalesPersistInventoryTimer { get; set; }
public InventoryActor(ActorHost host) : base(host) public InventoryActor(ActorHost host) : base(host)
{ {
} }
@ -39,8 +35,6 @@ public class InventoryActor : Actor, IInventoryActor
} }
else else
{ {
FlashSalesInventoryUpdated = true;
await TryRegisterFlashSalesPersistInventoryTimerAsync(); await TryRegisterFlashSalesPersistInventoryTimerAsync();
} }
} }
@ -57,19 +51,24 @@ public class InventoryActor : Actor, IInventoryActor
} }
else else
{ {
FlashSalesInventoryUpdated = true;
await TryRegisterFlashSalesPersistInventoryTimerAsync(); await TryRegisterFlashSalesPersistInventoryTimerAsync();
} }
} }
protected virtual async Task TryRegisterFlashSalesPersistInventoryTimerAsync() protected virtual async Task TryRegisterFlashSalesPersistInventoryTimerAsync()
{ {
FlashSalesPersistInventoryTimer ??= await RegisterTimerAsync(null, nameof(PeriodicSetInventoryStateAsync), if (FlashSalesInventoryUpdated)
null, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3)); {
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) 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;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using Orleans; using Orleans;
using Orleans.Providers; using Orleans.Providers;
using Orleans.Runtime; using Orleans.Runtime;
@ -14,9 +13,6 @@ public class InventoryGrain : Grain<InventoryStateModel>, IInventoryGrain
protected bool FlashSalesInventoryUpdated { get; set; } protected bool FlashSalesInventoryUpdated { get; set; }
[CanBeNull]
protected IDisposable FlashSalesPersistInventoryTimer { get; set; }
public virtual Task<InventoryStateModel> GetInventoryStateAsync() => Task.FromResult(State); public virtual Task<InventoryStateModel> GetInventoryStateAsync() => Task.FromResult(State);
public virtual async Task IncreaseInventoryAsync(int quantity, bool decreaseSold, bool isFlashSale) public virtual async Task IncreaseInventoryAsync(int quantity, bool decreaseSold, bool isFlashSale)
@ -29,8 +25,6 @@ public class InventoryGrain : Grain<InventoryStateModel>, IInventoryGrain
} }
else else
{ {
FlashSalesInventoryUpdated = true;
TryRegisterFlashSalesPersistInventoryTimer(); TryRegisterFlashSalesPersistInventoryTimer();
} }
} }
@ -45,19 +39,24 @@ public class InventoryGrain : Grain<InventoryStateModel>, IInventoryGrain
} }
else else
{ {
FlashSalesInventoryUpdated = true;
TryRegisterFlashSalesPersistInventoryTimer(); TryRegisterFlashSalesPersistInventoryTimer();
} }
} }
protected virtual void TryRegisterFlashSalesPersistInventoryTimer() protected virtual void TryRegisterFlashSalesPersistInventoryTimer()
{ {
FlashSalesPersistInventoryTimer ??= RegisterTimer(PeriodicWriteInventoryStateAsync, if (FlashSalesInventoryUpdated)
new object(), TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(3)); {
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) if (!FlashSalesInventoryUpdated)
{ {

Loading…
Cancel
Save