public void LabelOutgoingMessage()

in src/DurableTask.Core/Entities/StateFormat/MessageSorter.cs [54:109]


        public void LabelOutgoingMessage(RequestMessage message, string destination, DateTime now, TimeSpan reorderWindow)
        {
            if (reorderWindow.Ticks == 0)
            {
                return; // we are not doing any message sorting.
            }

            DateTime timestamp = now;

            // whenever (SendHorizon + reorderWindow < now) it is possible to advance the send horizon to (now - reorderWindow)
            // and we can then clean out all the no-longer-needed entries of LastSentToInstance.
            // However, to reduce the overhead of doing this collection, we don't update the send horizon immediately when possible.
            // Instead, we make sure at least MinIntervalBetweenCollections passes between collections.
            if (SendHorizon + reorderWindow + MinIntervalBetweenCollections < now)
            {
                SendHorizon = now - reorderWindow;

                // clean out send clocks that are past the reorder window

                if (LastSentToInstance != null)
                {
                    List<string> expired = new List<string>();

                    foreach (var kvp in LastSentToInstance)
                    {
                        if (kvp.Value < SendHorizon)
                        {
                            expired.Add(kvp.Key);
                        }
                    }

                    foreach (var t in expired)
                    {
                        LastSentToInstance.Remove(t);
                    }
                }
            }

            if (LastSentToInstance == null)
            {
                LastSentToInstance = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);
            }
            else if (LastSentToInstance.TryGetValue(destination, out var last))
            {
                message.Predecessor = last;

                // ensure timestamps are monotonic even if system clock is not
                if (timestamp <= last)
                {
                    timestamp = new DateTime(last.Ticks + 1);
                }
            }

            message.Timestamp = timestamp;
            LastSentToInstance[destination] = timestamp;
        }