-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from freifunkMUC/multithread_worker
Add queues for netlink messages
- Loading branch information
Showing
5 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python3 | ||
import threading | ||
from queue import Queue | ||
from time import sleep | ||
from wgkex.common import logger | ||
from wgkex.worker.netlink import link_handler | ||
from wgkex.worker.netlink import WireGuardClient | ||
|
||
|
||
class UniqueQueue(Queue): | ||
def put(self, item, block=True, timeout=None): | ||
if item not in self.queue: | ||
Queue.put(self, item, block, timeout) | ||
|
||
def _init(self, maxsize): | ||
self.queue = set() | ||
|
||
def _put(self, item): | ||
self.queue.add(item) | ||
|
||
def _get(self): | ||
return self.queue.pop() | ||
|
||
|
||
q = UniqueQueue() | ||
|
||
|
||
def watch_queue() -> None: | ||
"""Watches the queue for new messages.""" | ||
logger.debug("Starting queue watcher") | ||
threading.Thread(target=pick_from_queue, daemon=True).start() | ||
|
||
|
||
def pick_from_queue() -> None: | ||
"""Picks a message from the queue and processes it.""" | ||
logger.debug("Starting queue processor") | ||
while True: | ||
if not q.empty(): | ||
logger.debug("Queue is not empty current size is %i", q.qsize()) | ||
domain, message = q.get() | ||
logger.debug("Processing queue item %s for domain %s", message, domain) | ||
client = WireGuardClient( | ||
public_key=message, | ||
domain=domain, | ||
remove=False, | ||
) | ||
logger.info( | ||
f"Processing queue for key {client.public_key} on domain {domain} with lladdr {client.lladdr}" | ||
) | ||
logger.debug(link_handler(client)) | ||
q.task_done() | ||
else: | ||
logger.debug("Queue is empty") | ||
sleep(1) |