"Fossies" - the Fresh Open Source Software Archive

Member "pyzor-1.0.0/pyzor/forwarder.py" (10 Dec 2014, 2568 Bytes) of package /linux/privat/pyzor-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "forwarder.py" see the Fossies "Dox" file reference documentation.

    1 """Manage the forwarder process."""
    2 
    3 import logging
    4 import threading
    5 
    6 try:
    7     import Queue
    8 except ImportError:
    9     import queue as Queue
   10 
   11 
   12 class Forwarder(object):
   13     """Forwards digest to remote pyzor servers"""
   14 
   15     def __init__(self, forwarding_client, remote_servers,
   16                  max_queue_size=10000):
   17         """
   18         forward_client: a pyzor.client.Client instance to use as
   19                         forwarding client
   20         remote_servers: a list of (hostname,port) tuples where digests should
   21                         be forwarded to
   22         max_queue_size: max amount of queued digests
   23         """
   24         self.log = logging.getLogger("pyzord")
   25         self.forwarding_client = forwarding_client
   26         self.forward_queue = Queue.Queue(max_queue_size)
   27         self.remote_servers = remote_servers
   28 
   29     def _forward_loop(self):
   30         """read forwarding requests from the queue"""
   31         while True:
   32             try:
   33                 digest, whitelist = self.forward_queue.get(block=True,
   34                                                            timeout=2)
   35             except Queue.Empty:
   36                 # If the forwarding client has been deleted we should
   37                 # end the thread
   38                 if self.forwarding_client is None:
   39                     return
   40                 else:
   41                     continue
   42 
   43             for server in self.remote_servers:
   44                 try:
   45                     if whitelist:
   46                         self.forwarding_client.whitelist(digest, server)
   47                     else:
   48                         self.forwarding_client.report(digest, server)
   49                 except Exception as ex:
   50                     self.log.warn('Forwarding digest %s to %s failed: %s',
   51                                   digest, server, ex)
   52 
   53     def queue_forward_request(self, digest, whitelist=False):
   54         """If forwarding is enabled, insert a digest into the forwarding queue
   55         if whitelist is True, the digest will be forwarded as whitelist request
   56         if the queue is full, the digest is dropped
   57         """
   58         if self.forwarding_client is None:  # forwarding has been disabled
   59             return
   60 
   61         try:
   62             self.forward_queue.put_nowait((digest, whitelist),)
   63         except Queue.Full:
   64             pass
   65 
   66     def start_forwarding(self):
   67         """start the forwarding thread"""
   68         threading.Thread(target=self._forward_loop).start()
   69 
   70     def stop_forwarding(self):
   71         """disable forwarding and tell the forwarding thread to end itself"""
   72         self.forwarding_client = None