Skip to content

Commit 49edb14

Browse files
Merge pull request #55 from algolia/random-hosts
Randomize hosts in client initialization
2 parents da01b0e + 94860fe commit 49edb14

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

algoliasearch/client.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import hmac
2828
import hashlib
2929
import base64
30+
import random
3031
import sys
3132

3233
APPENGINE = 'APPENGINE_RUNTIME' in os.environ
@@ -75,15 +76,18 @@ def __init__(self, app_id, api_key, hosts_array=None):
7576
@param hosts_array the list of hosts that you have received for the service
7677
"""
7778
if not hosts_array:
78-
self.read_hosts = ['%s-dsn.algolia.net' % app_id,
79-
'%s-1.algolianet.com' % app_id,
80-
'%s-2.algolianet.com' % app_id,
81-
'%s-3.algolianet.com' % app_id]
82-
self.write_hosts = ['%s.algolia.net' % app_id,
83-
'%s-1.algolianet.com' % app_id,
84-
'%s-2.algolianet.com' % app_id,
85-
'%s-3.algolianet.com' % app_id
86-
]
79+
fallbacks = [
80+
'%s-1.algolianet.com' % app_id,
81+
'%s-2.algolianet.com' % app_id,
82+
'%s-3.algolianet.com' % app_id,
83+
]
84+
random.shuffle(fallbacks)
85+
86+
self.read_hosts = ['%s-dsn.algolia.net' % app_id]
87+
self.read_hosts.extend(fallbacks)
88+
self.write_hosts = ['%s.algolia.net' % app_id]
89+
self.write_hosts.extend(fallbacks)
90+
8791
else:
8892
self.read_hosts = hosts_array
8993
self.write_hosts = hosts_array

0 commit comments

Comments
 (0)