From 3774c946aee711e6f0b73daa84f06d1949d8e411 Mon Sep 17 00:00:00 2001 From: wangxw <996268132@qq.com> Date: Mon, 14 Nov 2022 11:28:40 +0800 Subject: [PATCH] add support for actor runtime config, to make idle timeout scanning and gc of runtime actor in effective #334 Signed-off-by: wangxw <996268132@qq.com> --- actor/config/config.go | 34 +++++++++++++++++++++++++++++++++- actor/config/config_test.go | 8 ++++++++ actor/runtime/actor_runtime.go | 4 ++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/actor/config/config.go b/actor/config/config.go index 656cc81a..0eed3d58 100644 --- a/actor/config/config.go +++ b/actor/config/config.go @@ -17,7 +17,11 @@ import "github.com/dapr/go-sdk/actor/codec/constant" // ActorConfig is Actor's configuration struct. type ActorConfig struct { - SerializerType string + SerializerType string + ActorIdleTimeout string + ActorScanInterval string + DrainOngingCallTimeout string + DrainBalancedActors bool } // Option is option function of ActorConfig. @@ -30,6 +34,34 @@ func WithSerializerName(serializerType string) Option { } } +// WithActorIdleTimeout set actorIdleTimeout type of the actor as @actorIdleTimeout. +func WithActorIdleTimeout(actorIdleTimeout string) Option { + return func(config *ActorConfig) { + config.ActorIdleTimeout = actorIdleTimeout + } +} + +// WithActorScanInterval set actorScanInterval type of the actor as @actorScanInterval. +func WithActorScanInterval(actorScanInterval string) Option { + return func(config *ActorConfig) { + config.ActorScanInterval = actorScanInterval + } +} + +// WithDrainOngingCallTimeout set drainOngingCallTimeout type of the actor as @drainOngingCallTimeout. +func WithDrainOngingCallTimeout(drainOngingCallTimeout string) Option { + return func(config *ActorConfig) { + config.DrainOngingCallTimeout = drainOngingCallTimeout + } +} + +// WithDrainBalancedActors set drainBalancedActors type of the actor as @drainBalancedActors. +func WithDrainBalancedActors(drainBalancedActors bool) Option { + return func(config *ActorConfig) { + config.DrainBalancedActors = drainBalancedActors + } +} + // GetConfigFromOptions get final ActorConfig set by @opts. func GetConfigFromOptions(opts ...Option) *ActorConfig { conf := &ActorConfig{ diff --git a/actor/config/config_test.go b/actor/config/config_test.go index 961cc3b9..83c822f8 100644 --- a/actor/config/config_test.go +++ b/actor/config/config_test.go @@ -31,8 +31,16 @@ func TestRegisterActorTimer(t *testing.T) { t.Run("get config with option", func(t *testing.T) { config := GetConfigFromOptions( WithSerializerName("mockSerializerType"), + WithActorIdleTimeout("1m"), + WithActorScanInterval("10s"), + WithDrainOngingCallTimeout("10s"), + WithDrainBalancedActors(true), ) assert.NotNil(t, config) assert.Equal(t, "mockSerializerType", config.SerializerType) + assert.Equal(t, "1m", config.ActorIdleTimeout) + assert.Equal(t, "10s", config.ActorScanInterval) + assert.Equal(t, "10s", config.DrainOngingCallTimeout) + assert.Equal(t, true, config.DrainBalancedActors) }) } diff --git a/actor/runtime/actor_runtime.go b/actor/runtime/actor_runtime.go index 410e7679..4dbbac76 100644 --- a/actor/runtime/actor_runtime.go +++ b/actor/runtime/actor_runtime.go @@ -49,6 +49,10 @@ func (r *ActorRunTime) RegisterActorFactory(f actor.Factory, opt ...config.Optio conf := config.GetConfigFromOptions(opt...) actType := f().Type() r.config.RegisteredActorTypes = append(r.config.RegisteredActorTypes, actType) + r.config.ActorIdleTimeout = conf.ActorIdleTimeout + r.config.ActorScanInterval = conf.ActorScanInterval + r.config.DrainOngingCallTimeout = conf.DrainOngingCallTimeout + r.config.DrainBalancedActors = conf.DrainBalancedActors mng, ok := r.actorManagers.Load(actType) if !ok { newMng, err := manager.NewDefaultActorManager(conf.SerializerType)