|
3 | 3 | import logging |
4 | 4 | import os |
5 | 5 | from asyncio import CancelledError |
| 6 | +from asyncio import Task |
6 | 7 | from asyncio import create_task |
7 | 8 | from asyncio import sleep |
8 | 9 | from time import time |
9 | 10 | from typing import TYPE_CHECKING |
10 | 11 | from typing import Any |
11 | 12 | from typing import Dict |
12 | 13 | from typing import Optional |
| 14 | +from typing import Set |
| 15 | +from urllib.parse import urlsplit |
13 | 16 |
|
14 | 17 | import aiohttp |
15 | 18 |
|
@@ -49,14 +52,35 @@ class WebhookNotifier: |
49 | 52 | def __init__(self, url: str): |
50 | 53 | """Initialize a WebhookNotifier posting to ``url``.""" |
51 | 54 | self.url: str = url |
| 55 | + #: Strong references to in-flight delivery tasks. ``asyncio`` only |
| 56 | + #: holds a *weak* reference to tasks created via |
| 57 | + #: :func:`asyncio.create_task`, so without this a long-running |
| 58 | + #: :meth:`_deliver` (up to ~27 s of backoff + timeouts) could be |
| 59 | + #: garbage-collected mid-flight. Each task removes itself via a |
| 60 | + #: done-callback (the pattern from the asyncio docs). |
| 61 | + self._tasks: Set["Task[None]"] = set() |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def _safe_url(url: str) -> str: |
| 65 | + """Return ``scheme://host[:port]`` of ``url`` for safe logging. |
| 66 | +
|
| 67 | + Strips any userinfo, path, and query string so credentials embedded |
| 68 | + in the URL (userinfo or query params) are never written to logs. |
| 69 | + """ |
| 70 | + parts = urlsplit(url) |
| 71 | + host: str = parts.hostname or "" |
| 72 | + if parts.port is not None: |
| 73 | + host = f"{host}:{parts.port}" |
| 74 | + return f"{parts.scheme}://{host}" if parts.scheme else host |
52 | 75 |
|
53 | 76 | @classmethod |
54 | 77 | def from_env(cls) -> Optional["WebhookNotifier"]: |
55 | 78 | """Build a notifier from ``STATUS_WEBHOOK_URL``, or None if unset.""" |
56 | 79 | url: str = os.environ.get("STATUS_WEBHOOK_URL", "").strip() |
57 | 80 | if not url: |
58 | 81 | return None |
59 | | - logger.info("Status-change webhook enabled; posting to %s", url) |
| 82 | + # Log only scheme+host: the URL may contain secrets (userinfo/query). |
| 83 | + logger.info("Status-change webhook enabled; posting to %s", cls._safe_url(url)) |
60 | 84 | return cls(url) |
61 | 85 |
|
62 | 86 | def build_payload( |
@@ -90,33 +114,39 @@ def notify( |
90 | 114 | ) -> None: |
91 | 115 | """Build the payload and spawn a fire-and-forget delivery task.""" |
92 | 116 | payload: Dict[str, Any] = self.build_payload(machine, event, user=user) |
93 | | - create_task(self._deliver(payload)) |
| 117 | + task: "Task[None]" = create_task(self._deliver(payload)) |
| 118 | + # Hold a strong reference until the task completes so the event loop's |
| 119 | + # weak reference cannot let it be garbage-collected mid-delivery. |
| 120 | + self._tasks.add(task) |
| 121 | + task.add_done_callback(self._tasks.discard) |
94 | 122 |
|
95 | 123 | async def _deliver(self, payload: Dict[str, Any]) -> None: |
96 | 124 | """Deliver one webhook with retries and exponential backoff. |
97 | 125 |
|
98 | 126 | Returns as soon as an attempt gets a non-error (< 400) response. On |
99 | 127 | network errors or 4xx/5xx responses it retries up to |
100 | 128 | :data:`MAX_ATTEMPTS` times, backing off exponentially between attempts, |
101 | | - then logs a single error and gives up. |
| 129 | + then logs a single error and gives up. A single |
| 130 | + :class:`aiohttp.ClientSession` is reused across attempts so retries can |
| 131 | + benefit from connection pooling. |
102 | 132 | """ |
103 | 133 | timeout: aiohttp.ClientTimeout = aiohttp.ClientTimeout( |
104 | 134 | total=self.REQUEST_TIMEOUT_SEC |
105 | 135 | ) |
106 | 136 | last_err: Optional[str] = None |
107 | | - for attempt in range(1, self.MAX_ATTEMPTS + 1): |
108 | | - if attempt > 1: |
109 | | - await sleep(self.BACKOFF_BASE_SEC * (2 ** (attempt - 2))) |
110 | | - try: |
111 | | - async with aiohttp.ClientSession(timeout=timeout) as session: |
| 137 | + async with aiohttp.ClientSession(timeout=timeout) as session: |
| 138 | + for attempt in range(1, self.MAX_ATTEMPTS + 1): |
| 139 | + if attempt > 1: |
| 140 | + await sleep(self.BACKOFF_BASE_SEC * (2 ** (attempt - 2))) |
| 141 | + try: |
112 | 142 | async with session.post(self.url, json=payload) as resp: |
113 | 143 | if resp.status < 400: |
114 | 144 | return |
115 | 145 | last_err = f"HTTP {resp.status}" |
116 | | - except CancelledError: # pragma: no cover |
117 | | - raise |
118 | | - except Exception as ex: |
119 | | - last_err = repr(ex) |
| 146 | + except CancelledError: # pragma: no cover |
| 147 | + raise |
| 148 | + except Exception as ex: |
| 149 | + last_err = repr(ex) |
120 | 150 | logger.error( |
121 | 151 | "Failed to deliver status webhook for machine %s event %s after " |
122 | 152 | "%d attempts: %s", |
|
0 commit comments