perf(network): cache dbus proxies, reuse rfkill state
This commit is contained in:
1 file changed
+38
-20
+38
-20
@@ -1,11 +1,11 @@
|
|||||||
import subprocess
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import List, Literal
|
from typing import List, Literal
|
||||||
|
|
||||||
from fabric.core.service import Property, Service, Signal
|
from fabric.core.service import Property, Service, Signal
|
||||||
from fabric.utils import Gio, GLib, logger
|
from fabric.utils import Gio, GLib, logger
|
||||||
|
|
||||||
from services.bluetooth import _rfkill_soft_blocked
|
from services.bluetooth import rfkill_soft_blocked
|
||||||
|
from utils.functions import run_command
|
||||||
|
|
||||||
NM_SERVICE = "org.freedesktop.NetworkManager"
|
NM_SERVICE = "org.freedesktop.NetworkManager"
|
||||||
NM_PATH = "/org/freedesktop/NetworkManager"
|
NM_PATH = "/org/freedesktop/NetworkManager"
|
||||||
@@ -84,16 +84,30 @@ _CONNECTIVITY_NAMES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_PROXY_CACHE: dict[tuple[str, str], Gio.DBusProxy] = {}
|
||||||
|
|
||||||
|
|
||||||
def _make_proxy(bus: Gio.DBusConnection, path: str, iface: str) -> Gio.DBusProxy:
|
def _make_proxy(bus: Gio.DBusConnection, path: str, iface: str) -> Gio.DBusProxy:
|
||||||
return Gio.DBusProxy.new_sync(
|
key = (path, iface)
|
||||||
bus,
|
proxy = _PROXY_CACHE.get(key)
|
||||||
Gio.DBusProxyFlags.NONE,
|
if proxy is None:
|
||||||
None,
|
proxy = Gio.DBusProxy.new_sync(
|
||||||
NM_SERVICE,
|
bus,
|
||||||
path,
|
Gio.DBusProxyFlags.NONE,
|
||||||
iface,
|
None,
|
||||||
None,
|
NM_SERVICE,
|
||||||
)
|
path,
|
||||||
|
iface,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
_PROXY_CACHE[key] = proxy
|
||||||
|
return proxy
|
||||||
|
|
||||||
|
|
||||||
|
def _invalidate_proxy(path: str) -> None:
|
||||||
|
"""Drop cached proxies for an object path (e.g. a removed device)."""
|
||||||
|
for key in [k for k in _PROXY_CACHE if k[0] == path]:
|
||||||
|
_PROXY_CACHE.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
def _get(proxy: Gio.DBusProxy, prop: str):
|
def _get(proxy: Gio.DBusProxy, prop: str):
|
||||||
@@ -844,7 +858,7 @@ class NetworkClient(Service):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Initial airplane mode
|
# Initial airplane mode
|
||||||
bluetooth_blocked = _rfkill_soft_blocked()
|
bluetooth_blocked = rfkill_soft_blocked()
|
||||||
wifi_enabled = self._nm.wireless_enabled
|
wifi_enabled = self._nm.wireless_enabled
|
||||||
self._airplane_mode = bluetooth_blocked and not wifi_enabled
|
self._airplane_mode = bluetooth_blocked and not wifi_enabled
|
||||||
|
|
||||||
@@ -914,6 +928,10 @@ class NetworkClient(Service):
|
|||||||
self.ethernet_device = None
|
self.ethernet_device = None
|
||||||
self.emit("device-removed", iface)
|
self.emit("device-removed", iface)
|
||||||
|
|
||||||
|
# Drop cached proxies now that signal handlers are disconnected and the
|
||||||
|
# removed device is no longer referenced.
|
||||||
|
_invalidate_proxy(dev_path)
|
||||||
|
|
||||||
def _get_primary_device(self) -> Literal["wifi", "wired"] | None:
|
def _get_primary_device(self) -> Literal["wifi", "wired"] | None:
|
||||||
if not self._nm:
|
if not self._nm:
|
||||||
return None
|
return None
|
||||||
@@ -1075,7 +1093,7 @@ class NetworkClient(Service):
|
|||||||
@Property(bool, "read-write", default_value=False)
|
@Property(bool, "read-write", default_value=False)
|
||||||
def airplane_mode(self) -> bool:
|
def airplane_mode(self) -> bool:
|
||||||
if self._nm:
|
if self._nm:
|
||||||
bluetooth_blocked = _rfkill_soft_blocked()
|
bluetooth_blocked = rfkill_soft_blocked()
|
||||||
wifi_enabled = self._nm.wireless_enabled
|
wifi_enabled = self._nm.wireless_enabled
|
||||||
self._airplane_mode = bluetooth_blocked and not wifi_enabled
|
self._airplane_mode = bluetooth_blocked and not wifi_enabled
|
||||||
return self._airplane_mode
|
return self._airplane_mode
|
||||||
@@ -1091,13 +1109,13 @@ class NetworkClient(Service):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
action = "block" if enable else "unblock"
|
action = "block" if enable else "unblock"
|
||||||
subprocess.run(
|
result = run_command(["rfkill", action, "bluetooth"], timeout=5)
|
||||||
["rfkill", action, "bluetooth"],
|
if result.returncode == 127:
|
||||||
capture_output=True,
|
logger.warning("[Network] rfkill not found")
|
||||||
timeout=5,
|
elif result.returncode == -1:
|
||||||
)
|
logger.warning(f"[Network] rfkill {action} timed out")
|
||||||
except FileNotFoundError:
|
elif result.returncode != 0:
|
||||||
logger.warning("[Network] rfkill not found")
|
logger.warning(f"[Network] rfkill {action} failed: {result.stderr}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"[Network] rfkill {action} failed: {e}")
|
logger.warning(f"[Network] rfkill {action} failed: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user