Closed nilsv-abb closed 1 month ago
@nilsv-abb Good catch. The issue you describe stems from the usage of the System clock in time calculations. Such code is always prone to time drifts. A better solution would be not to use DateTime.UtcNow at all.
@mrsuciu could you use a PeriodicTimer
instead?
Type of issue
Current Behavior
We have implemented a simple publisher using the PubSub NuGet package. A WriterGroup is configured with a Publishing Interval of 1000ms, but the actual (runtime) interval will be slightly larger so the publishing time will start to drift.
This log, with entries from Opc.Ua.PubSub.UaPublisher_PublishMessages(), shows that the millisecond part of the publishing time has drifted from 531ms to 737ms (13:36:05.531 vs 13:36:25.737) after 20s:
[2024-08-29 13:36:05.531 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:06.543 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:07.557 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:08.557 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:09.585 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:10.589 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:11.602 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:12.602 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:13.614 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:14.626 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:15.640 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:16.654 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:17.669 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:18.679 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:19.681 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:20.694 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:21.715 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:22.727 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:23.733 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:24.734 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua] [2024-08-29 13:36:25.737 INF] UaPublisher - PublishNetworkMessage, WriterGroupId:1; success = True [Opc.Ua]
Expected Behavior
The millisecond part of each publishing time should not be drifting, keeping the interval between publishing times constant.
Steps To Reproduce
No response
Environment
Anything else?
Changing how m_nextPublishTime is calculated fixes the problem, see "Old code" and "New code" comments below :
IntervalRunner.cs