Просмотр исходного кода

1.优化协程锁的实现
2.没有mailbox则认为是NotFoundActor,这样Unit在传送前可以移除MailBox来让Actor消息失效重发,逻辑上也更精确,没有挂载Mailbox本身就不是一个actor
3.Lock Location默认改成60秒超时,之前是1秒钟太短了,很多人忘记指定时间,默认1秒,结果容易超时

tanghai 4 лет назад
Родитель
Сommit
8eacccca2e

+ 3 - 2
Server/Hotfix/Demo/Session/InnerMessageDispatcherHelper.cs

@@ -22,7 +22,8 @@ namespace ET
             MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
             if (mailBoxComponent == null)
             {
-                FailResponse(iActorRequest, ErrorCode.ERR_ActorNoMailBoxComponent, reply);
+                Log.Warning($"actor not found mailbox: {entity.GetType().Name} {actorId} {iActorRequest}");
+                FailResponse(iActorRequest, ErrorCode.ERR_NotFoundActor, reply);
                 return;
             }
 
@@ -77,7 +78,7 @@ namespace ET
             MailBoxComponent mailBoxComponent = entity.GetComponent<MailBoxComponent>();
             if (mailBoxComponent == null)
             {
-                Log.Error($"actor not add MailBoxComponent: {entity.GetType().Name} {iActorMessage}");
+                Log.Error($"actor not found mailbox: {entity.GetType().Name} {actorId} {iActorMessage}");
                 return;
             }
 

+ 0 - 1
Server/Hotfix/Module/ActorLocation/ActorLocationSenderComponentSystem.cs

@@ -174,7 +174,6 @@ namespace ET
                         actorLocationSender.ActorId = 0;
                         continue;
                     }
-                    case ErrorCode.ERR_ActorNoMailBoxComponent:
                     case ErrorCode.ERR_ActorTimeout:
                     {
                         throw new RpcException(response.Error, $"{memoryStream.ToActorMessage()}");

+ 1 - 1
Server/Hotfix/Module/ActorLocation/LocationProxyComponentSystem.cs

@@ -34,7 +34,7 @@ namespace ET
                 new ObjectAddRequest() { Key = key, InstanceId = instanceId });
         }
 
-        public static async ETTask Lock(this LocationProxyComponent self, long key, long instanceId, int time = 1000)
+        public static async ETTask Lock(this LocationProxyComponent self, long key, long instanceId, int time = 60000)
         {
             Log.Info($"location proxy lock {key}, {instanceId} {TimeHelper.ServerNow()}");
             await MessageHelper.CallActor(GetLocationSceneId(key),

+ 5 - 5
Server/Model/Module/ActorLocation/LocationComponent.cs

@@ -43,7 +43,7 @@ namespace ET
             using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
             {
                 this.locations[key] = instanceId;
-                Log.Debug($"location add key: {key} instanceId: {instanceId}");
+                Log.Info($"location add key: {key} instanceId: {instanceId}");
             }
         }
 
@@ -52,7 +52,7 @@ namespace ET
             using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
             {
                 this.locations.Remove(key);
-                Log.Debug($"location remove key: {key}");
+                Log.Info($"location remove key: {key}");
             }
         }
 
@@ -63,7 +63,7 @@ namespace ET
             LockInfo lockInfo = this.AddChild<LockInfo, long, CoroutineLock>(instanceId, coroutineLock);
             this.lockInfos.Add(key, lockInfo);
 
-            Log.Debug($"location lock key: {key} instanceId: {instanceId}");
+            Log.Info($"location lock key: {key} instanceId: {instanceId}");
 
             if (time > 0)
             {
@@ -92,7 +92,7 @@ namespace ET
                 return;
             }
 
-            Log.Debug($"location unlock key: {key} instanceId: {oldInstanceId} newInstanceId: {newInstanceId}");
+            Log.Info($"location unlock key: {key} instanceId: {oldInstanceId} newInstanceId: {newInstanceId}");
 
             this.locations[key] = newInstanceId;
 
@@ -107,7 +107,7 @@ namespace ET
             using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
             {
                 this.locations.TryGetValue(key, out long instanceId);
-                Log.Debug($"location get key: {key} instanceId: {instanceId}");
+                Log.Info($"location get key: {key} instanceId: {instanceId}");
                 return instanceId;
             }
         }

+ 5 - 5
Unity/Assets/Model/Module/CoroutineLock/CoroutineLock.cs

@@ -9,7 +9,7 @@ namespace ET
         {
             self.coroutineLockType = type;
             self.key = k;
-            self.count = count;
+            self.level = count;
         }
     }
 
@@ -20,16 +20,16 @@ namespace ET
         {
             if (self.coroutineLockType != CoroutineLockType.None)
             {
-                CoroutineLockComponent.Instance.Notify(self.coroutineLockType, self.key, self.count + 1);
+                CoroutineLockComponent.Instance.RunNextCoroutine(self.coroutineLockType, self.key, self.level + 1);
             }
             else
             {
                 // CoroutineLockType.None说明协程锁超时了
-                Log.Error($"coroutine lock timeout: {self.coroutineLockType} {self.key} {self.count}");
+                Log.Error($"coroutine lock timeout: {self.coroutineLockType} {self.key} {self.level}");
             }
             self.coroutineLockType = CoroutineLockType.None;
             self.key = 0;
-            self.count = 0;
+            self.level = 0;
         }
     }
     
@@ -37,6 +37,6 @@ namespace ET
     {
         public CoroutineLockType coroutineLockType;
         public long key;
-        public int count;
+        public int level;
     }
 }

+ 20 - 28
Unity/Assets/Model/Module/CoroutineLock/CoroutineLockComponent.cs

@@ -37,16 +37,15 @@ namespace ET
             // 检测超时的CoroutineLock
             TimeoutCheck(self);
             
-            int count = self.nextFrameRun.Count;
-            // 注意这里不能将this.nextFrameRun.Count 放到for循环中,因为循环过程中会有对象继续加入队列
-            for (int i = 0; i < count; ++i)
+            // 循环过程中会有对象继续加入队列
+            while(self.nextFrameRun.Count > 0)
             {
-                (CoroutineLockType coroutineLockType, long key) = self.nextFrameRun.Dequeue();
-                self.Notify(coroutineLockType, key, 0);
+                (CoroutineLockType coroutineLockType, long key, int count) = self.nextFrameRun.Dequeue();
+                self.Notify(coroutineLockType, key, count);
             }
         }
-        
-        public void TimeoutCheck(CoroutineLockComponent self)
+
+        private void TimeoutCheck(CoroutineLockComponent self)
         {
             // 超时的锁
             if (self.timers.Count == 0)
@@ -95,7 +94,7 @@ namespace ET
 
                 CoroutineLock coroutineLock = coroutineLockTimer.CoroutineLock;
                 // 超时直接调用下一个锁
-                self.NextFrameRun(coroutineLock.coroutineLockType, coroutineLock.key);
+                self.RunNextCoroutine(coroutineLock.coroutineLockType, coroutineLock.key, coroutineLock.level + 1);
                 coroutineLock.coroutineLockType = CoroutineLockType.None; // 上面调用了下一个, dispose不再调用
             }
         }
@@ -103,12 +102,17 @@ namespace ET
 
     public static class CoroutineLockComponentSystem
     {
-        public static void NextFrameRun(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key)
+        public static void RunNextCoroutine(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key, int level)
         {
-            self.nextFrameRun.Enqueue((coroutineLockType, key));
+            // 一个协程队列一帧处理超过100个,说明比较多了,打个warning,检查一下是否够正常
+            if (level == 100)
+            {
+                Log.Warning($"too much coroutine level: {coroutineLockType} {key}");
+            }
+            self.nextFrameRun.Enqueue((coroutineLockType, key, level));
         }
 
-        public static void AddTimer(this CoroutineLockComponent self, long tillTime, CoroutineLock coroutineLock)
+        private static void AddTimer(this CoroutineLockComponent self, long tillTime, CoroutineLock coroutineLock)
         {
             self.timers.Add(tillTime, new CoroutineLockTimer(coroutineLock));
             if (tillTime < self.minTime)
@@ -133,9 +137,9 @@ namespace ET
             return await tcs;
         }
 
-        public static CoroutineLock CreateCoroutineLock(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key, int time, int count)
+        private static CoroutineLock CreateCoroutineLock(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key, int time, int level)
         {
-            CoroutineLock coroutineLock = self.AddChildWithId<CoroutineLock, CoroutineLockType, long, int>(++self.idGenerator, coroutineLockType, key, count, true);
+            CoroutineLock coroutineLock = self.AddChildWithId<CoroutineLock, CoroutineLockType, long, int>(++self.idGenerator, coroutineLockType, key, level, true);
             if (time > 0)
             {
                 self.AddTimer(TimeHelper.ClientFrameTime() + time, coroutineLock);
@@ -143,7 +147,7 @@ namespace ET
             return coroutineLock;
         }
 
-        public static void Notify(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key, int count)
+        public static void Notify(this CoroutineLockComponent self, CoroutineLockType coroutineLockType, long key, int level)
         {
             CoroutineLockQueueType coroutineLockQueueType = self.list[(int) coroutineLockType];
             if (!coroutineLockQueueType.TryGetValue(key, out CoroutineLockQueue queue))
@@ -156,21 +160,9 @@ namespace ET
                 coroutineLockQueueType.Remove(key);
                 return;
             }
-            
-#if NOT_UNITY
-            const int frameCoroutineCount = 5;
-#else
-            const int frameCoroutineCount = 10;
-#endif
 
-            if (count > frameCoroutineCount)
-            {
-                self.NextFrameRun(coroutineLockType, key);
-                return;
-            }
-            
             CoroutineLockInfo coroutineLockInfo = queue.Dequeue();
-            coroutineLockInfo.Tcs.SetResult(self.CreateCoroutineLock(coroutineLockType, key, coroutineLockInfo.Time, count));
+            coroutineLockInfo.Tcs.SetResult(self.CreateCoroutineLock(coroutineLockType, key, coroutineLockInfo.Time, level));
         }
     }
 
@@ -179,7 +171,7 @@ namespace ET
         public static CoroutineLockComponent Instance;
         
         public List<CoroutineLockQueueType> list = new List<CoroutineLockQueueType>((int) CoroutineLockType.Max);
-        public Queue<(CoroutineLockType, long)> nextFrameRun = new Queue<(CoroutineLockType, long)>();
+        public Queue<(CoroutineLockType, long, int)> nextFrameRun = new Queue<(CoroutineLockType, long, int)>();
         public MultiMap<long, CoroutineLockTimer> timers = new MultiMap<long, CoroutineLockTimer>();
         public Queue<long> timeOutIds = new Queue<long>();
         public Queue<CoroutineLockTimer> timerOutTimer = new Queue<CoroutineLockTimer>();

+ 0 - 1
Unity/Assets/Model/Module/Message/ErrorCode.cs

@@ -30,7 +30,6 @@ namespace ET
         public const int ERR_KcpSplitError = 100231;
         public const int ERR_KcpSplitCountError = 100232;
 
-        public const int ERR_ActorNoMailBoxComponent = 110003;
         public const int ERR_ActorLocationSenderTimeout = 110004;
         public const int ERR_PacketParserError = 110005;
         public const int ERR_KcpChannelAcceptTimeout = 110206;

+ 0 - 3
Unity/Assets/Model/Module/Message/IMessageDispatcher.cs.meta

@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: 69481855785b4b59834c766e4e2798e1
-timeCreated: 1503568535