"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "neutron/notifiers/batch_notifier.py" between
neutron-14.0.2.tar.gz and neutron-14.0.3.tar.gz

About: OpenStack Neutron (Core Service: Networking) enables network connectivity as a service for other OpenStack services, such as OpenStack Compute. Provides an API for users to define networks and the attachments into them. Has a pluggable architecture that supports many popular networking vendors and technologies.
The "Stein" series (maintained release).

batch_notifier.py  (neutron-14.0.2):batch_notifier.py  (neutron-14.0.3)
skipping to change at line 13 skipping to change at line 13
# a copy of the License at # a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import threading
import eventlet import eventlet
from neutron_lib.utils import runtime
from oslo_utils import uuidutils
class BatchNotifier(object): class BatchNotifier(object):
def __init__(self, batch_interval, callback): def __init__(self, batch_interval, callback):
self.pending_events = [] self._pending_events = eventlet.Queue()
self.callback = callback self.callback = callback
self.batch_interval = batch_interval self.batch_interval = batch_interval
self._lock_identifier = 'notifier-%s' % uuidutils.generate_uuid() self._mutex = threading.Lock()
def queue_event(self, event): def queue_event(self, event):
"""Called to queue sending an event with the next batch of events. """Called to queue sending an event with the next batch of events.
Sending events individually, as they occur, has been problematic as it Sending events individually, as they occur, has been problematic as it
can result in a flood of sends. Previously, there was a loopingcall can result in a flood of sends. Previously, there was a loopingcall
thread that would send batched events on a periodic interval. However, thread that would send batched events on a periodic interval. However,
maintaining a persistent thread in the loopingcall was also maintaining a persistent thread in the loopingcall was also
problematic. problematic.
This replaces the loopingcall with a mechanism that creates a This replaces the loopingcall with a mechanism that creates a
short-lived thread on demand whenever an event is queued. That thread short-lived thread on demand whenever an event is queued. That thread
will wait for a lock, send all queued events and then sleep for will check if the lock is released, send all queued events and then
'batch_interval' seconds to allow other events to queue up. sleep for 'batch_interval' seconds. If at the end of this sleep time,
other threads have added new events to the event queue, the same thread
This effectively acts as a rate limiter to only allow 1 batch per will process them.
'batch_interval' seconds.
At the same time, other threads will be able to add new events to the
queue and will spawn new "synced_send" threads to process them. But if
the mutex is locked, the spawned thread will end immediately.
:param event: the event that occurred. :param event: the event that occurred.
""" """
if not event: if not event:
return return
self.pending_events.append(event) self._pending_events.put(event)
@runtime.synchronized(self._lock_identifier)
def synced_send(): def synced_send():
self._notify() if not self._mutex.locked():
# sleeping after send while holding the lock allows subsequent with self._mutex:
# events to batch up while not self._pending_events.empty():
eventlet.sleep(self.batch_interval) self._notify()
# sleeping after send while holding the lock allows
# subsequent events to batch up
eventlet.sleep(self.batch_interval)
eventlet.spawn_n(synced_send) eventlet.spawn_n(synced_send)
def _notify(self): def _notify(self):
if not self.pending_events: batched_events = []
return while not self._pending_events.empty():
batched_events.append(self._pending_events.get())
batched_events = self.pending_events
self.pending_events = []
self.callback(batched_events) self.callback(batched_events)
 End of changes. 9 change blocks. 
20 lines changed or deleted 23 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)