Move remote backoff sleep to before we pop a frame from the queue.
authorCarl Hetherington <cth@carlh.net>
Wed, 19 Mar 2025 23:31:22 +0000 (00:31 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 22 Mar 2025 12:29:58 +0000 (13:29 +0100)
Otherwise in the case of failure it can take a while for the frame to
become available again for a functioning thread to encode.

src/lib/j2k_sync_encoder_thread.cc
src/lib/j2k_sync_encoder_thread.h
src/lib/remote_j2k_encoder_thread.cc
src/lib/remote_j2k_encoder_thread.h

index ef6834f60e0ecf8a92dc9b9eb9b1f311dc5ebf3d..158c12b11bdc04cc00b000ff5e3ab4f83df7a336 100644 (file)
@@ -25,6 +25,8 @@
 #include "j2k_sync_encoder_thread.h"
 #include <dcp/scope_guard.h>
 
+#include "i18n.h"
+
 
 J2KSyncEncoderThread::J2KSyncEncoderThread(J2KEncoder& encoder)
        : J2KEncoderThread(encoder)
@@ -40,6 +42,11 @@ try
        log_thread_start();
 
        while (true) {
+               if (auto wait = backoff()) {
+                       LOG_ERROR(N_("Encoder thread sleeping (due to backoff) for %1s"), wait);
+                       boost::this_thread::sleep(boost::posix_time::seconds(wait));
+               }
+
                LOG_TIMING("encoder-sleep thread=%1", thread_id());
                auto frame = _encoder.pop();
 
index 45222279e3568e34a29ca0bb9da827c1ba351fa2..06bb1fcefb64bbad7684c443a695a78f5ebaa86c 100644 (file)
@@ -26,6 +26,11 @@ public:
 
        virtual void log_thread_start() const = 0;
        virtual std::shared_ptr<dcp::ArrayData> encode(DCPVideo const& frame) = 0;
+       /** @return number of seconds we should wait between attempts to use this thread
+        *  for encoding. Used to avoid flooding non-responsive network servers with
+        *  requests.
+        */
+       virtual int backoff() const { return 0; }
 };
 
 
index 49d80953dad520260c462e72a473af805c0dc2f6..2abaf94baaae1a0512b9852dcaaab9bbee8c2cc7 100644 (file)
@@ -61,22 +61,18 @@ RemoteJ2KEncoderThread::encode(DCPVideo const& frame)
                }
        } catch (std::exception& e) {
                LOG_ERROR(
-                       N_("Remote encode of %1 on %2 failed (%3); thread sleeping for %4s"),
+                       N_("Remote encode of %1 on %2 failed (%3)"),
                        frame.index(), _server.host_name(), e.what(), _remote_backoff
                );
        } catch (...) {
                LOG_ERROR(
-                       N_("Remote encode of %1 on %2 failed; thread sleeping for %4s"),
+                       N_("Remote encode of %1 on %2 failed"),
                        frame.index(), _server.host_name(), _remote_backoff
                );
        }
 
-       if (!encoded) {
-               if (_remote_backoff < 60) {
-                       /* back off more */
-                       _remote_backoff += 10;
-               }
-               boost::this_thread::sleep(boost::posix_time::seconds(_remote_backoff));
+       if (!encoded && _remote_backoff < 60) {
+               _remote_backoff += 10;
        }
 
        return encoded;
index f3fe7f94aafcbd1db5c2be52cf40443228c46dc2..5b203946c8416d6b3b8045d8cd8b3af103bd4314 100644 (file)
@@ -14,6 +14,10 @@ public:
                return _server;
        }
 
+       int backoff() const override {
+               return _remote_backoff;
+       }
+
 private:
        EncodeServerDescription _server;
        /** Number of seconds that we currently wait between attempts to connect to the server */