summaryrefslogtreecommitdiff
path: root/kvmd
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-05-27 02:30:49 +0300
committerDevaev Maxim <[email protected]>2020-05-27 02:30:49 +0300
commit3a2bf5304733913851adce6fb73dd8aa14623c79 (patch)
tree0516d2a95ac89411cf2bd5a5470510f61613a975 /kvmd
parent49aca90c2eaffd2350411be48fae056ba535b427 (diff)
refactoring
Diffstat (limited to 'kvmd')
-rw-r--r--kvmd/apps/kvmd/server.py8
-rw-r--r--kvmd/apps/kvmd/streamer.py23
2 files changed, 17 insertions, 14 deletions
diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py
index 893f95c3..6561ca86 100644
--- a/kvmd/apps/kvmd/server.py
+++ b/kvmd/apps/kvmd/server.py
@@ -408,12 +408,12 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
# ===== SYSTEM TASKS
async def __stream_controller(self) -> None:
- prev = 0
+ prev = False
while True:
- cur = len(self.__sockets)
- if prev == 0 and cur > 0:
+ cur = bool(self.__sockets)
+ if not prev and cur:
await self.__streamer.ensure_start(init_restart=True)
- elif prev > 0 and cur == 0:
+ elif prev and not cur:
await self.__streamer.ensure_stop(immediately=False)
if self.__reset_streamer or self.__new_streamer_params:
diff --git a/kvmd/apps/kvmd/streamer.py b/kvmd/apps/kvmd/streamer.py
index 5f2d2b42..cc8b9d29 100644
--- a/kvmd/apps/kvmd/streamer.py
+++ b/kvmd/apps/kvmd/streamer.py
@@ -179,13 +179,9 @@ class Streamer: # pylint: disable=too-many-instance-attributes
async def get_state(self) -> Dict:
state = None
if self.__streamer_task:
- session = self.__ensure_session()
+ session = self.__ensure_http_session()
try:
- async with session.get(
- url=f"http://{self.__host}:{self.__port}/state",
- headers={"User-Agent": make_user_agent("KVMD")},
- timeout=self.__timeout,
- ) as response:
+ async with session.get(self.__make_url("state")) as response:
aiotools.raise_not_200(response)
state = (await response.json())["result"]
except (aiohttp.ClientConnectionError, aiohttp.ServerConnectionError):
@@ -246,14 +242,21 @@ class Streamer: # pylint: disable=too-many-instance-attributes
# =====
- def __ensure_session(self) -> aiohttp.ClientSession:
+ def __ensure_http_session(self) -> aiohttp.ClientSession:
if not self.__http_session:
+ kwargs: Dict = {
+ "headers": {"User-Agent": make_user_agent("KVMD")},
+ "timeout": aiohttp.ClientTimeout(total=self.__timeout),
+ }
if self.__unix_path:
- self.__http_session = aiohttp.ClientSession(connector=aiohttp.UnixConnector(path=self.__unix_path))
- else:
- self.__http_session = aiohttp.ClientSession()
+ kwargs["connector"] = aiohttp.UnixConnector(path=self.__unix_path)
+ self.__http_session = aiohttp.ClientSession(**kwargs)
return self.__http_session
+ def __make_url(self, handle: str) -> str:
+ assert not handle.startswith("/"), handle
+ return f"http://{self.__host}:{self.__port}/{handle}"
+
# =====
@aiotools.atomic