Exemplo n.º 1
0
        private void AssignMessageToHandler(ISubscription subs, IMessage message)
        {
            HandlerBundle bundle = ActiveHandlers.FirstOrDefault(b => b.Handler.CanAccept(subs, message).Success);
            bool          stateAcceptable = bundle.IsNull() || bundle.HostingTask.Status == TaskStatus.WaitingForActivation, reanimated = false;

            // There is a handler that can accept the current subscription, but its task has ceased
            // Reuse it to ensure no messages are lost
            if (bundle.IsNotNull() && !stateAcceptable)
            {
                ActiveHandlers.Remove(bundle);
                bundle.HostingTask.Wait();
                // If the handler is blocked, we still allow this flow to proceed, as this places a message in the handler and the task immediately exits - but the message is preserved
                bundle = CreateBundle(bundle.Handler, subs, message);
                ActiveHandlers.Add(bundle);
                reanimated = true;
                Logger.LogInfo("Reanimated handler for (" + subs.Topic.Name + "," + subs.ChannelMonicker + ")");
            }
            bundle
            .IsNull()
            .IfTrue(() => CreateNewHandler(subs, message))
            .IfFalse(() => { if (!reanimated)
                             {
                                 bundle.Handler.Accept(subs, message);
                             }
                     });
        }