Skip to content

Commit 7d45c64

Browse files
committed
支持按策略对Actor进行统一配置
1 parent 6abfd84 commit 7d45c64

4 files changed

Lines changed: 58 additions & 3 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Vertex.Runtime.Actor.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
6+
public class FollowPolicyAttribute : Attribute
7+
{
8+
public FollowPolicyAttribute(string optionPolicy)
9+
{
10+
this.OptionPolicy = optionPolicy;
11+
}
12+
13+
public string OptionPolicy { get; set; }
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace Vertex.Runtime.Actor.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
6+
public class VertexPolicyAttribute : Attribute
7+
{
8+
public VertexPolicyAttribute(string optionPolicy, string archiveOptionPolicy)
9+
{
10+
this.OptionPolicy = optionPolicy;
11+
this.ArchiveOptionPolicy = archiveOptionPolicy;
12+
}
13+
14+
public string OptionPolicy { get; set; }
15+
16+
public string ArchiveOptionPolicy { get; set; }
17+
}
18+
}

src/Vertex.Runtime/Actor/FlowActor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Data;
55
using System.Linq;
66
using System.Linq.Expressions;
7+
using System.Reflection;
78
using System.Reflection.Emit;
89
using System.Runtime.CompilerServices;
910
using System.Threading.Tasks;
@@ -340,7 +341,16 @@ static void GetInheritor(SwitchMethodEmit from, List<SwitchMethodEmit> list, Lis
340341
/// <returns></returns>
341342
protected virtual async ValueTask DependencyInjection()
342343
{
343-
this.FlowOptions = this.ServiceProvider.GetService<IOptionsMonitor<FlowActorOptions>>().Get(this.ActorType.FullName);
344+
var optionPolicy = this.ActorType.GetCustomAttribute<FollowPolicyAttribute>(true);
345+
if (optionPolicy != default)
346+
{
347+
this.FlowOptions = this.ServiceProvider.GetService<IOptionsMonitor<FlowActorOptions>>().Get(optionPolicy.OptionPolicy);
348+
}
349+
else
350+
{
351+
this.FlowOptions = this.ServiceProvider.GetService<IOptionsMonitor<FlowActorOptions>>().CurrentValue;
352+
}
353+
344354
this.Serializer = this.ServiceProvider.GetService<ISerializer>();
345355
this.EventTypeContainer = this.ServiceProvider.GetService<IEventTypeContainer>();
346356
this.Logger = (ILogger)this.ServiceProvider.GetService(typeof(ILogger<>).MakeGenericType(this.ActorType));

src/Vertex.Runtime/Actor/VertexActor.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Runtime.CompilerServices;
67
using System.Text;
78
using System.Threading.Tasks;
@@ -16,6 +17,7 @@
1617
using Vertex.Abstractions.Snapshot;
1718
using Vertex.Abstractions.Storage;
1819
using Vertex.Protocol;
20+
using Vertex.Runtime.Actor.Attributes;
1921
using Vertex.Runtime.Event;
2022
using Vertex.Runtime.Exceptions;
2123
using Vertex.Runtime.Options;
@@ -67,8 +69,18 @@ public class VertexActor<TPrimaryKey, T> : ActorBase<TPrimaryKey>, IVertexActor
6769
/// <returns></returns>
6870
protected virtual async ValueTask DependencyInjection()
6971
{
70-
this.VertexOptions = this.ServiceProvider.GetService<IOptionsMonitor<ActorOptions>>().Get(this.ActorType.FullName);
71-
this.ArchiveOptions = this.ServiceProvider.GetService<IOptionsMonitor<ArchiveOptions>>().Get(this.ActorType.FullName);
72+
var optionPolicy = this.ActorType.GetCustomAttribute<VertexPolicyAttribute>(true);
73+
if (optionPolicy != default)
74+
{
75+
this.VertexOptions = this.ServiceProvider.GetService<IOptionsMonitor<ActorOptions>>().Get(optionPolicy.OptionPolicy);
76+
this.ArchiveOptions = this.ServiceProvider.GetService<IOptionsMonitor<ArchiveOptions>>().Get(optionPolicy.ArchiveOptionPolicy);
77+
}
78+
else
79+
{
80+
this.VertexOptions = this.ServiceProvider.GetService<IOptionsMonitor<ActorOptions>>().CurrentValue;
81+
this.ArchiveOptions = this.ServiceProvider.GetService<IOptionsMonitor<ArchiveOptions>>().CurrentValue;
82+
}
83+
7284
this.Logger = (ILogger)this.ServiceProvider.GetService(typeof(ILogger<>).MakeGenericType(this.ActorType));
7385
this.Serializer = this.ServiceProvider.GetService<ISerializer>();
7486
this.EventTypeContainer = this.ServiceProvider.GetService<IEventTypeContainer>();

0 commit comments

Comments
 (0)