This commit is contained in:
wXwcoder 2025-03-13 18:00:32 +00:00 committed by GitHub
commit 402adefc64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 3 deletions

View File

@ -42,6 +42,10 @@ type Server interface {
// SaveState is impl by ServerImplBase, It saves the state cache of this actor instance to state store component by calling api of daprd.
// Save state is called at two places: 1. On invocation of this actor instance. 2. When new actor starts.
SaveState() error
// Activate called when actor created by actor manager
Activate() error
// Deactivate called before actor removed by actor manager
Deactivate() error
WithContext() ServerContext
}
@ -64,6 +68,10 @@ type ServerContext interface {
// SaveState is impl by ServerImplBase, It saves the state cache of this actor instance to state store component by calling api of daprd.
// Save state is called at two places: 1. On invocation of this actor instance. 2. When new actor starts.
SaveState(context.Context) error
// Activate called when actor created by actor manager
Activate() error
// Deactivate called before actor removed by actor manager
Deactivate() error
}
type ReminderCallee interface {
@ -131,6 +139,16 @@ func (b *ServerImplBase) SaveState() error {
return b.ctx.SaveState(context.Background())
}
// Activate when actor created by actor manager
func (b *ServerImplBase) Activate() error {
return nil
}
// Deactivate before actor removed by actor manager
func (b *ServerImplBase) Deactivate() error {
return nil
}
// Deprecated: Use ServerImplBaseCtx instead.
func (b *ServerImplBase) WithContext() *ServerImplBaseCtx {
b.ctx.lock.RLock()
@ -179,6 +197,16 @@ func (b *ServerImplBaseCtx) SaveState(ctx context.Context) error {
return nil
}
// Activate when actor created by actor manager
func (b *ServerImplBaseCtx) Activate() error {
return nil
}
// Deactivate before actor removed by actor manager
func (b *ServerImplBaseCtx) Deactivate() error {
return nil
}
// Deprecated: StateManager is deprecated in favour of StateManagerContext.
type StateManager interface {
// Add is to add new state store with @stateName and @value

View File

@ -30,6 +30,7 @@ type ActorContainer interface {
Invoke(methodName string, param []byte) ([]reflect.Value, actorErr.ActorErr)
//nolint:staticcheck // SA1019 Deprecated: use ActorContainerContext instead.
GetActor() actor.Server
Deactivate() error
}
type ActorContainerContext interface {
@ -80,8 +81,12 @@ func NewDefaultActorContainerContext(ctx context.Context, actorID string, impl a
daprClient, _ := dapr.NewClient()
// create state manager for this new actor
impl.SetStateManager(state.NewActorStateManagerContext(impl.Type(), actorID, state.NewDaprStateAsyncProvider(daprClient)))
err := impl.Activate()
if err != nil {
return nil, actorErr.ErrSaveStateFailed
}
// save state of this actor
err := impl.SaveState(ctx)
err = impl.SaveState(ctx)
if err != nil {
return nil, actorErr.ErrSaveStateFailed
}
@ -118,6 +123,10 @@ func (d *DefaultActorContainerContext) Invoke(ctx context.Context, methodName st
return returnValue, actorErr.Success
}
func (d *DefaultActorContainer) Deactivate() error {
return d.actor.Deactivate()
}
func (d *DefaultActorContainerContext) GetActor() actor.ServerContext {
return d.actor
}

View File

@ -181,12 +181,12 @@ func (m *DefaultActorManagerContext) InvokeMethod(ctx context.Context, actorID,
return rspData, actorErr.Success
}
// DeactivateActor removes actor from actor manager.
func (m *DefaultActorManagerContext) DeactivateActor(_ context.Context, actorID string) actorErr.ActorErr {
_, ok := m.activeActors.Load(actorID)
actor, ok := m.activeActors.Load(actorID)
if !ok {
return actorErr.ErrActorIDNotFound
}
actor.(ActorContainer).Deactivate()
m.activeActors.Delete(actorID)
return actorErr.Success
}

View File

@ -28,6 +28,14 @@ type ActorImpl struct {
actor.ServerImplBase
}
func (t *ActorImpl) Activate() error {
return nil
}
func (t *ActorImpl) Deactivate() error {
return nil
}
func (t *ActorImpl) Type() string {
return "testActorType"
}
@ -70,6 +78,14 @@ type NotReminderCalleeActor struct {
actor.ServerImplBaseCtx
}
func (t *NotReminderCalleeActor) Activate() error {
return nil
}
func (t *NotReminderCalleeActor) Deactivate() error {
return nil
}
func (t *NotReminderCalleeActor) Type() string {
return "testActorNotReminderCalleeType"
}

View File

@ -152,6 +152,14 @@ func (mr *MockServerMockRecorder) Type() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Type", reflect.TypeOf((*MockServer)(nil).Type))
}
func (m *MockServer) Activate() error {
return nil
}
func (m *MockServer) Deactivate() error {
return nil
}
// WithContext mocks base method.
func (m *MockServer) WithContext() actor.ServerContext {
m.ctrl.T.Helper()