File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -138,4 +138,32 @@ public interface IActor : IWorker
138138 /// 通常在Actor被回收或重置时调用此方法。
139139 /// </remarks>
140140 void ClearData ( ) ;
141+
142+ /// <summary>
143+ /// Actor 回收时的处理方法,该方法在Actor被回收时自动调用,该函数不能由外部调用。只能由ActorManager内部调用
144+ /// </summary>
145+ /// <remarks>
146+ /// 当 Actor 被系统回收时调用此方法。
147+ /// 用于执行必要的清理工作,如:
148+ /// - 释放占用的资源
149+ /// - 清理组件状态
150+ /// - 保存需要持久化的数据
151+ /// - 取消订阅的事件
152+ /// - 断开网络连接等
153+ /// </remarks>
154+ /// <returns>表示异步操作的任务</returns>
155+ internal Task OnRecycle ( ) ;
156+
157+ /// <summary>
158+ /// 添加一个在Actor回收时执行一次的回调事件
159+ /// </summary>
160+ /// <param name="action">要执行的回调方法</param>
161+ /// <remarks>
162+ /// 该回调事件只会在Actor被回收时触发一次,之后会自动移除。
163+ /// 通常用于:
164+ /// - 执行一次性的清理操作
165+ /// - 触发状态变更通知
166+ /// - 记录回收日志等场景
167+ /// </remarks>
168+ void AddOnceRecycleCallback ( Action action ) ;
141169}
Original file line number Diff line number Diff line change @@ -229,6 +229,7 @@ public async Task CrossDay(int openServerDay)
229229
230230 private readonly ConcurrentDictionary < string , object > _data = new ConcurrentDictionary < string , object > ( ) ;
231231
232+
232233 /// <summary>
233234 /// 设置Actor的数据
234235 /// </summary>
@@ -292,6 +293,69 @@ public void ClearData()
292293 _data . Clear ( ) ;
293294 }
294295
296+ /// <summary>
297+ /// Actor 回收时的处理方法
298+ /// </summary>
299+ /// <remarks>
300+ /// 当 Actor 被系统回收时调用此方法。
301+ /// 用于执行必要的清理工作,如:
302+ /// - 释放占用的资源
303+ /// - 清理组件状态
304+ /// - 保存需要持久化的数据
305+ /// - 取消订阅的事件
306+ /// - 断开网络连接等
307+ /// </remarks>
308+ /// <returns>表示异步操作的任务</returns>
309+ public Task OnRecycle ( )
310+ {
311+ try
312+ {
313+ while ( _onceRecycleCallbacks . First != null )
314+ {
315+ var first = _onceRecycleCallbacks . First ;
316+ _onceRecycleCallbacks . RemoveFirst ( ) ;
317+ try
318+ {
319+ first . Value ? . Invoke ( ) ;
320+ }
321+ catch ( Exception ex )
322+ {
323+ // 记录回调执行异常但继续执行其他回调
324+ LogHelper . Error ( $ "Actor回收回调执行异常 actorId:{ Id } actorType:{ Type } 异常:\n { ex } ") ;
325+ }
326+ }
327+ }
328+ catch ( Exception ex )
329+ {
330+ // 记录整体执行异常
331+ LogHelper . Error ( $ "Actor回收过程异常 actorId:{ Id } actorType:{ Type } 异常:\n { ex } ") ;
332+ }
333+ finally
334+ {
335+ _onceRecycleCallbacks . Clear ( ) ;
336+ }
337+
338+ return Task . CompletedTask ;
339+ }
340+
341+ private readonly LinkedList < Action > _onceRecycleCallbacks = new LinkedList < Action > ( ) ;
342+
343+ /// <summary>
344+ /// 添加一个在Actor回收时执行一次的回调事件
345+ /// </summary>
346+ /// <param name="action">要执行的回调方法</param>
347+ /// <remarks>
348+ /// 该回调事件只会在Actor被回收时触发一次,之后会自动移除。
349+ /// 通常用于:
350+ /// - 执行一次性的清理操作
351+ /// - 触发状态变更通知
352+ /// - 记录回收日志等场景
353+ /// </remarks>
354+ public void AddOnceRecycleCallback ( Action action )
355+ {
356+ _onceRecycleCallbacks . AddLast ( action ) ;
357+ }
358+
295359 /// <summary>
296360 /// 反激活所有组件,使其进入非活动状态
297361 /// </summary>
Original file line number Diff line number Diff line change @@ -252,6 +252,7 @@ async Task<bool> Work()
252252 if ( actor . ReadyToDeActive )
253253 {
254254 await actor . Inactive ( ) ;
255+ await actor . OnRecycle ( ) ;
255256 ActorMap . TryRemove ( actor . Id , out var _ ) ;
256257 LogHelper . Debug ( $ "actor回收 id:{ actor . Id } type:{ actor . Type } ") ;
257258 }
You can’t perform that action at this time.
0 commit comments