|
1
|
|
|
"""Utilities for composing KytosEventBuffers""" |
|
2
|
|
|
|
|
3
|
|
|
from janus import PriorityQueue, Queue |
|
4
|
|
|
|
|
5
|
|
|
from kytos.core.helpers import get_thread_pool_max_workers |
|
6
|
|
|
|
|
7
|
|
|
from .buffers import KytosEventBuffer |
|
8
|
|
|
|
|
9
|
|
|
queue_classes = { |
|
10
|
|
|
'queue': Queue, |
|
11
|
|
|
'priority': PriorityQueue, |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def process_queue(config: dict) -> Queue: |
|
16
|
|
|
""" |
|
17
|
|
|
Create a janus queue from a given config dict |
|
18
|
|
|
""" |
|
19
|
|
|
queue_type = queue_classes[config.get('type', 'queue')] |
|
20
|
|
|
queue_size = config.get('maxsize', 0) |
|
21
|
|
|
if isinstance(queue_size, str): |
|
22
|
|
|
if queue_size.startswith('threadpool_'): |
|
23
|
|
|
threadpool = queue_size[len('threadpool_'):] |
|
24
|
|
|
queue_size = get_thread_pool_max_workers().get(threadpool, 0) |
|
25
|
|
|
else: |
|
26
|
|
|
raise TypeError( |
|
27
|
|
|
'Expected int or str formatted ' |
|
28
|
|
|
'as "threadpool_{threadpool_name}"' |
|
29
|
|
|
) |
|
30
|
|
|
return queue_type(maxsize=queue_size) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
extension_processors = {} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def buffer_from_config(name: str, config: dict) -> KytosEventBuffer: |
|
37
|
|
|
""" |
|
38
|
|
|
Create a KytosEventBuffer from a given config dict |
|
39
|
|
|
""" |
|
40
|
|
|
buffer_cls = KytosEventBuffer |
|
41
|
|
|
args = {} |
|
42
|
|
|
# Process Queue Config |
|
43
|
|
|
args['queue'] = process_queue(config.get('queue', {})) |
|
44
|
|
|
|
|
45
|
|
|
buffer = buffer_cls(name, **args) |
|
46
|
|
|
|
|
47
|
|
|
# Process Mixins |
|
48
|
|
|
extensions: dict = config.get('extensions', []) |
|
49
|
|
|
for extension in extensions: |
|
50
|
|
|
extension_type = extension['type'] |
|
51
|
|
|
extension_args = extension.get('args', {}) |
|
52
|
|
|
buffer = extension_processors[extension_type](buffer, extension_args) |
|
53
|
|
|
|
|
54
|
|
|
return buffer |
|
55
|
|
|
|