Skip to content

Commit 6c25af6

Browse files
authored
Merge pull request #1420 from nico97118/fix/bcrypt_threading
fix(auth): restore threading for bcrypt work from #1182
2 parents 7e6da0a + 02c3418 commit 6c25af6

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

custom_components/alarmo/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import base64
55
import logging
6+
import concurrent.futures
67

78
import bcrypt
89
from homeassistant.core import (
@@ -48,6 +49,8 @@
4849

4950
_LOGGER = logging.getLogger(__name__)
5051

52+
# Max number of threads to start when checking user codes.
53+
MAX_WORKERS = 4
5154
# Number of rounds of hashing when computing user hashes.
5255
BCRYPT_NUM_ROUNDS = 10
5356

@@ -369,11 +372,17 @@ def check_user_code(user, code):
369372
return check_user_code(self.store.async_get_user(user_id), code)
370373

371374
users = self.store.async_get_users()
372-
for user in users.values():
373-
result = check_user_code(user, code)
374-
if result:
375-
return result
376-
return None
375+
with concurrent.futures.ThreadPoolExecutor(
376+
max_workers=MAX_WORKERS
377+
) as executor:
378+
futures = [
379+
executor.submit(check_user_code, user, code)
380+
for user in users.values()
381+
]
382+
for future in concurrent.futures.as_completed(futures):
383+
if future.result():
384+
executor.shutdown(wait=False, cancel_futures=True)
385+
return future.result()
377386

378387
return await self.hass.async_add_executor_job(_authenticate_user_sync)
379388

0 commit comments

Comments
 (0)