From ec865633265b896538424aa21bb3d0da8f37d0a1 Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Sat, 20 Dec 2008 07:14:56 -0700
Subject: [PATCH] immutable: when downloading an immutable file, use primary
 shares if they are available Primary shares require no erasure decoding so
 the more primary shares you have, the less CPU is used.

---
 src/allmydata/immutable/download.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/allmydata/immutable/download.py b/src/allmydata/immutable/download.py
index da1d40c4..fcf5c422 100644
--- a/src/allmydata/immutable/download.py
+++ b/src/allmydata/immutable/download.py
@@ -875,8 +875,13 @@ class FileDownloader(log.PrefixingLogMixin):
             potential_shnums = list(available_shnums - handled_shnums)
             if not potential_shnums:
                 raise NotEnoughSharesError
-            # choose a random share
-            shnum = random.choice(potential_shnums)
+            # For the next share, choose a primary share if available, else a randomly chosen
+            # secondary share.
+            potential_shnums.sort()
+            if potential_shnums[0] < self._uri.needed_shares:
+                shnum = potential_shnums[0]
+            else:
+                shnum = random.choice(potential_shnums)
             # and a random bucket that will provide it
             validated_bucket = random.choice(list(self._share_vbuckets[shnum]))
             self.active_buckets[shnum] = validated_bucket
-- 
2.45.2