Petr Horáček has uploaded a new change for review.

Change subject: netlink: event monitor with timeout
......................................................................

netlink: event monitor with timeout

Change-Id: If5a00814d8b7d9c4c7cba52c81b6e56f50dcc584
Signed-off-by: Petr Horáček <[email protected]>
---
M lib/vdsm/netlink/monitor.py
1 file changed, 31 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/29/37129/1

diff --git a/lib/vdsm/netlink/monitor.py b/lib/vdsm/netlink/monitor.py
index 307df48..6b938e8 100644
--- a/lib/vdsm/netlink/monitor.py
+++ b/lib/vdsm/netlink/monitor.py
@@ -23,6 +23,7 @@
 import os
 import select
 import threading
+import time
 
 from vdsm.utils import NoIntrCall, NoIntrPoll
 
@@ -38,8 +39,10 @@
 # If monitoring thread is running, queue waiting for new value and we call
 # stop(), we have to stop queue by passing special code.
 _STOP_FLAG = 31
+_TIMEOUT_FLAG = 32
 
 E_NOT_RUNNING = 1
+E_TIMEOUTED = 2
 
 
 class MonitorError(Exception):
@@ -58,24 +61,34 @@
         handle event
 
     Monitoring events synchronously:
+    mon = Monitor()
     mon.start()
     for event in mon:
         if foo:
             mon.stop()
         handle event
 
-    Monitor defined groups (monitor everything if not set):
+    Monitoring events with defined timeout. It there is no time left,
+    MonitorError: E_TIMEOUTED is raised by iteration:
+    mon = Monitor(timeout=2)
     mon.start()
-    for event in Monitor(groups=['link', 'ipv4-route']):
+    for event in mon:
+        handle event
+
+    Monitor defined groups (monitor everything if not set):
+    mon = Monitor()
+    mon.start()
+    for event in mon:
         if foo:
             mon.stop()
-        handle event
 
     Possible groups: link, notify, neigh, tc, ipv4-ifaddr, ipv4-mroute,
     ipv4-route ipv6-ifaddr, ipv6-mroute, ipv6-route, ipv6-ifinfo,
     decnet-ifaddr, decnet-route, ipv6-prefix
     """
-    def __init__(self, groups=frozenset()):
+    def __init__(self, groups=frozenset(), timeout=None):
+        self._time_start = None
+        self._timeout = timeout
         self._stopped = False
         if groups:
             self._groups = groups
@@ -88,11 +101,15 @@
 
     def __iter__(self):
         for event in iter(self._queue.get, None):
-            if event == _STOP_FLAG:
+            if event == _TIMEOUT_FLAG:
+                raise MonitorError(E_TIMEOUTED)
+            elif event == _STOP_FLAG:
                 break
             yield event
 
     def start(self):
+        if self._timeout:
+            self._time_start = time.time()
         self._scan_thread.start()
         self._scanning_started.wait()
 
@@ -102,11 +119,18 @@
                 with _pipetrick(epoll) as self._pipetrick:
                     self._scanning_started.set()
                     while True:
-                        events = NoIntrPoll(epoll.poll)
-                        if (self._pipetrick[0], select.POLLIN) in events:
+                        timeout = ((self._time_start + self._timeout)
+                                   - time.time()) if self._timeout else -1
+                        events = NoIntrPoll(epoll.poll, timeout=timeout)
+
+                        if events == []:
+                            self._queue.put(_TIMEOUT_FLAG)
+                            break
+                        elif (self._pipetrick[0], select.POLLIN) in events:
                             NoIntrCall(os.read, self._pipetrick[0], 1)
                             self._queue.put(_STOP_FLAG)
                             break
+
                         _nl_recvmsgs_default(sock)
 
     def stop(self):


-- 
To view, visit http://gerrit.ovirt.org/37129
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If5a00814d8b7d9c4c7cba52c81b6e56f50dcc584
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Petr Horáček <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to