Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use threads to speed up connectivity check #576

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Hide CDPR Goodie Pack Content
- Add notifications on successful download and installation of games (thanks to orende)
- Add category filtering dialog for game library (thanks to orende)
- Parallelize api.can_connect function with threads, futures (thanks to orende)

**1.2.2**
- Fix progress bar not showing up for downloads
Expand Down
20 changes: 17 additions & 3 deletions minigalaxy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urlencode
import requests
import xml.etree.ElementTree as ET
from concurrent.futures import ThreadPoolExecutor, as_completed

from requests import Session

Expand All @@ -29,6 +30,7 @@ def __init__(self, config: Config, session: Session):
self.debug = os.environ.get("MG_DEBUG")
self.active_token = False
self.active_token_expiration_time = time.time()
self.conn_check_thpool = ThreadPoolExecutor(max_workers=2)

# use a method to authenticate, based on the information we have
# Returns an empty string if no information was entered
Expand Down Expand Up @@ -248,12 +250,24 @@ def can_connect(self) -> bool:
"https://embed.gog.com",
"https://auth.gog.com",
]
for url in urls:
threads = []

def make_request(url_to_check: str):
try:
self.session.get(url, timeout=5)
self.session.get(url_to_check, timeout=5)
return True
except requests.exceptions.ConnectionError:
return False
return True

for url in urls:
threads.append(self.conn_check_thpool.submit(make_request, url))

results = []
for thread in as_completed(threads):
if thread.cancelled():
return False
results += [thread.result()]
return all(results)

# Make a request with the active token
def __request(self, url: str = None, params: dict = None) -> dict:
Expand Down
4 changes: 4 additions & 0 deletions minigalaxy/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ def __init__(self, config: Config, api: 'Api', download_manager: DownloadManager
self.make_directories()

# Interact with the API
logger.debug("Checking API connectivity...")
self.offline = not self.api.can_connect()
logger.debug("Done checking API connectivity, status: %s", "offline" if self.offline else "online")
if not self.offline:
try:
logger.debug("Authenticating...")
self.__authenticate()
logger.debug("Authenticated as: %s", self.api.get_user_info())
self.HeaderBar.set_subtitle(self.api.get_user_info())
except Exception:
logger.warn("Starting in offline mode after receiving exception", exc_info=1)
Expand Down