From 9cce9640131c5b5fd933f03cc3aa9b5500315379 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 14 Apr 2016 15:20:57 -0700 Subject: [PATCH] use nextPacketTimestamp only to catch up, not stay ahead --- libraries/networking/src/udt/SendQueue.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libraries/networking/src/udt/SendQueue.cpp b/libraries/networking/src/udt/SendQueue.cpp index d9cd329bdd..ba43076b31 100644 --- a/libraries/networking/src/udt/SendQueue.cpp +++ b/libraries/networking/src/udt/SendQueue.cpp @@ -329,10 +329,21 @@ void SendQueue::run() { auto nextPacketDelta = (newPacketCount == 2 ? 2 : 1) * _packetSendPeriod; nextPacketTimestamp += std::chrono::microseconds(nextPacketDelta); - // sleep as long as we need until next packet send, if we can + // sleep as long as we need for next packet send, if we can auto now = p_high_resolution_clock::now(); + auto timeToSleep = duration_cast(nextPacketTimestamp - now); + // we use nextPacketTimestamp so that we don't fall behind, not to force long sleeps + // we'll never allow nextPacketTimestamp to force us to sleep for more than nextPacketDelta + // so cap it to that value + if (timeToSleep > std::chrono::microseconds(nextPacketDelta)) { + // reset the nextPacketTimestamp so that it is correct next time we come around + nextPacketTimestamp = now + std::chrono::microseconds(nextPacketDelta); + + timeToSleep = std::chrono::microseconds(nextPacketDelta); + } + // we're seeing SendQueues sleep for a long period of time here, // which can lock the NodeList if it's attempting to clear connections // for now we guard this by capping the time this thread and sleep for