From d706057ed8ce6897c925633c97bc9bff6820f628 Mon Sep 17 00:00:00 2001
From: Tim Abbott <tabbott@zulip.com>
Date: Wed, 25 Sep 2013 13:23:11 -0400
Subject: [PATCH] zephyr_mirror: Clear all notices from the queue before
 selecting.

We were having problems where we were suspiciously processing notices
at a rate of 1 notice per 15s, which suggests that we the select was
timing out even though there were notices to be fetched immediately.
We fix this by clearing the queue each time our select loop ends.

(imported from commit 7e7bfbb2126d1f4170d65d1483a0b799dcab80b9)
---
 bots/zephyr_mirror_backend.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/bots/zephyr_mirror_backend.py b/bots/zephyr_mirror_backend.py
index d263a48a..140d52ba 100755
--- a/bots/zephyr_mirror_backend.py
+++ b/bots/zephyr_mirror_backend.py
@@ -267,17 +267,20 @@ def process_loop(log):
     while True:
         select.select([zephyr._z.getFD()], [], [], 15)
         try:
-            notice = zephyr.receive(block=False)
+            # Fetch notices from the queue until its empty
+            while True:
+                notice = zephyr.receive(block=False)
+                if notice is None:
+                    break
+                try:
+                    process_notice(notice, log)
+                except Exception:
+                    logger.exception("Error relaying zephyr:")
+                    time.sleep(2)
         except Exception:
             logger.exception("Error checking for new zephyrs:")
             time.sleep(1)
             continue
-        if notice is not None:
-            try:
-                process_notice(notice, log)
-            except Exception:
-                logger.exception("Error relaying zephyr:")
-                time.sleep(2)
 
         if time.time() - last_check_time > 15:
             last_check_time = time.time()