From c9def7697757bdae20804a058493710fcff0cab3 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Sun, 31 Jul 2011 21:39:32 -0700
Subject: [PATCH] Coalesce multiple Share.loop() calls, make downloads faster.
 Closes #1268.

---
 src/allmydata/immutable/downloader/share.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/allmydata/immutable/downloader/share.py b/src/allmydata/immutable/downloader/share.py
index 95173ae6..1918d902 100644
--- a/src/allmydata/immutable/downloader/share.py
+++ b/src/allmydata/immutable/downloader/share.py
@@ -52,6 +52,7 @@ class Share:
         self._dyhb_rtt = dyhb_rtt
         # self._alive becomes False upon fatal corruption or server error
         self._alive = True
+        self._loop_scheduled = False
         self._lp = log.msg(format="%(share)s created", share=repr(self),
                            level=log.NOISY, parent=logparent, umid="P7hv2w")
 
@@ -164,7 +165,7 @@ class Share:
                 break
         else:
             self._requested_blocks.append( (segnum, set([o])) )
-        eventually(self.loop)
+        self.schedule_loop()
         return o
 
     def _cancel_block_request(self, o):
@@ -184,7 +185,14 @@ class Share:
             return self._requested_blocks[0]
         return None, []
 
+    def schedule_loop(self):
+        if self._loop_scheduled:
+            return
+        self._loop_scheduled = True
+        eventually(self.loop)
+
     def loop(self):
+        self._loop_scheduled = False
         if not self._alive:
             return
         try:
@@ -797,7 +805,7 @@ class Share:
 
     def _trigger_loop(self, res):
         if self._alive:
-            eventually(self.loop)
+            self.schedule_loop()
         return res
 
     def _fail(self, f, level=log.WEIRD):
-- 
2.45.2