Files
gns3-server/gns3server/controller/notification.py
T
YueGuobin 8c98142d26 feat: report Docker pull progress and add an idle timeout
A pull now reports docker.image.pull notifications on the controller
stream: started, throttled downloading events with the layer counts
and the latest daemon message, then completed or failed. An optional
progress_id is echoed in every event so a client can correlate them
with its own request. Image operations belong to a compute, not to a
project, so the events go to the controller stream and reach clients
that have no project open.

The pull also has an idle socket read timeout of 60 seconds. Without
it an unreachable registry left the pull, the compute request and the
client request hanging forever with no feedback at all. The timeout
is idle time, not total time: the daemon streams progress during the
whole pull, so a minute without data means the registry is gone. When
it fires, the daemon connection closes and Docker cancels the pull.

Docker.http_query now accepts an aiohttp ClientTimeout object so a
caller can ask for a policy finer than a total timeout.
2026-11-06 15:03:34 +08:00

199 lines
6.8 KiB
Python

#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
from contextlib import contextmanager
from gns3server.utils.notification_queue import NotificationQueue
from .controller_error import ControllerError
class Notification:
"""
Manage notification for the controller
"""
def __init__(self, controller):
self._controller = controller
self._project_listeners = {}
self._project_marker_listeners = {}
self._controller_listeners = set()
@contextmanager
def project_queue(self, project_id):
"""
Get a queue of notifications
Use it with Python with
"""
queue = NotificationQueue()
self._project_listeners.setdefault(project_id, set())
self._project_listeners[project_id].add(queue)
try:
yield queue
finally:
self._project_listeners[project_id].remove(queue)
@contextmanager
def project_marker_queue(self, project_id):
"""
Get a queue of marker notifications (marker.match etc.) for a project.
Marker events are delivered on this dedicated channel instead of the
main project queue, so high-frequency marker.matches do not cause
head-of-line blocking for topology events (node.*/link.*).
Use it with Python with
"""
queue = NotificationQueue()
self._project_marker_listeners.setdefault(project_id, set())
self._project_marker_listeners[project_id].add(queue)
try:
yield queue
finally:
self._project_marker_listeners[project_id].remove(queue)
@contextmanager
def controller_queue(self):
"""
Get a queue of notifications
Use it with Python with
"""
queue = NotificationQueue()
self._controller_listeners.add(queue)
try:
yield queue
finally:
self._controller_listeners.remove(queue)
def controller_emit(self, action, event):
"""
Send a notification to clients connected to the controller stream
:param action: Action name
:param event: Event to send
"""
for controller_listener in self._controller_listeners:
asyncio.get_running_loop().call_soon_threadsafe(controller_listener.put_nowait, (action, event, {}))
def project_has_listeners(self, project_id):
"""
:param project_id: Project object
:returns: True if client listen this project
"""
return project_id in self._project_listeners and len(self._project_listeners[project_id]) > 0
async def dispatch(self, action, event, project_id, compute_id):
"""
Notification received from compute node. Send it directly
to clients or process it
:param action: Action name
:param event: Event to send
:param compute_id: Compute id of the sender
"""
if action == "node.updated":
try:
# Update controller node data and send the event node.updated
project = self._controller.get_project(event["project_id"])
node = project.get_node(event["node_id"])
await node.parse_node_response(event)
self.project_emit("node.updated", node.asdict())
except ControllerError: # Project closing
return
elif action == "ping":
event["compute_id"] = compute_id
self.project_emit(action, event)
elif action.startswith("docker.image."):
# Image operations are scoped to a compute, not to a project, and
# clients that are not in a project still need them: emit on the
# controller stream, like compute.updated.
event["compute_id"] = compute_id
self.controller_emit(action, event)
elif action.startswith("marker."):
self.marker_emit(action, event, project_id)
else:
self.project_emit(action, event, project_id)
def project_emit(self, action, event, project_id=None):
"""
Send a notification to clients scoped by projects
:param action: Action name
:param event: Event to send
"""
if "project_id" in event or project_id:
self._send_event_to_project(event.get("project_id", project_id), action, event)
else:
self._send_event_to_all_projects(action, event)
def marker_emit(self, action, event, project_id):
"""
Send a marker notification (e.g. marker.match) to clients listening on
the dedicated marker channel for this project. Marker events are kept
off the main project queue on purpose, to avoid head-of-line blocking
from high-frequency matches.
:param action: Action name
:param event: Event to send
:param project_id: Project id the marker belongs to
"""
try:
marker_listeners = self._project_marker_listeners[project_id]
except KeyError:
return
for listener in marker_listeners:
asyncio.get_running_loop().call_soon_threadsafe(listener.put_nowait, (action, event, {}))
def _send_event_to_project(self, project_id, action, event):
"""
Send an event to all the client listening for notifications for
this project
:param project: Project where we need to send the event
:param action: Action name
:param event: Event to send
"""
try:
project_listeners = self._project_listeners[project_id]
except KeyError:
return
for listener in project_listeners:
asyncio.get_running_loop().call_soon_threadsafe(listener.put_nowait, (action, event, {}))
def _send_event_to_all_projects(self, action, event):
"""
Send an event to all the client listening for notifications on all
projects
:param action: Action name
:param event: Event to send
"""
for project_listeners in self._project_listeners.values():
for listener in project_listeners:
asyncio.get_running_loop().call_soon_threadsafe(listener.put_nowait, (action, event, {}))