mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-03 12:40:47 +08:00
FSM for Feed
This commit is contained in:
@@ -3,13 +3,27 @@ from collections import Counter
|
||||
|
||||
from zipline.core.component import Component
|
||||
from zipline.components.aggregator import Aggregate
|
||||
from zipline.utils.protocol_utils import Enum
|
||||
import zipline.protocol as zp
|
||||
from zipline.transitions import WorkflowMeta
|
||||
|
||||
from zipline.protocol import CONTROL_PROTOCOL, COMPONENT_TYPE, \
|
||||
CONTROL_FRAME, CONTROL_UNFRAME
|
||||
|
||||
LOGGER = logging.getLogger('ZiplineLogger')
|
||||
|
||||
# FSM
|
||||
# ---
|
||||
|
||||
INIT, READY, DRAINING = FEED_STATES = \
|
||||
Enum( 'INIT', 'READY', 'DRAINING')
|
||||
|
||||
state_transitions = dict(
|
||||
do_start = (-1 , INIT) ,
|
||||
do_run = (INIT , READY) ,
|
||||
do_drain = (READY , DRAINING) ,
|
||||
)
|
||||
|
||||
class Feed(Aggregate):
|
||||
"""
|
||||
Connects to N PULL sockets, publishing all messages received to a
|
||||
@@ -18,20 +32,26 @@ class Feed(Aggregate):
|
||||
one execution context (thread, process, etc) and run in another.
|
||||
"""
|
||||
|
||||
__metaclass__ = WorkflowMeta
|
||||
|
||||
states = list(FEED_STATES)
|
||||
transitions = state_transitions
|
||||
initial_state = -1
|
||||
|
||||
def init(self):
|
||||
self.sent_count = 0
|
||||
self.received_count = 0
|
||||
self.draining = False
|
||||
self.ds_finished_counter = 0
|
||||
|
||||
# Depending on the size of this, might want to use a data
|
||||
# structure with better asymptotics.
|
||||
self.data_buffer = {}
|
||||
|
||||
# source_id -> integer count
|
||||
self.sent_counters = Counter()
|
||||
self.recv_counters = Counter()
|
||||
|
||||
self.state = INIT
|
||||
|
||||
@property
|
||||
def get_id(self):
|
||||
return "FEED"
|
||||
@@ -71,6 +91,8 @@ class Feed(Aggregate):
|
||||
"""
|
||||
Get the next message in chronological order.
|
||||
"""
|
||||
|
||||
# is_full and draining defined in aggregator
|
||||
if not(self.is_full() or self.draining):
|
||||
return
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ class PassthroughTransform(BaseTransform):
|
||||
"""
|
||||
|
||||
def init(self):
|
||||
self.state = { 'name': 'PASSTHROUGH' }
|
||||
self.props = { 'name': 'PASSTHROUGH' }
|
||||
|
||||
#TODO, could save some cycles by skipping the _UNFRAME call
|
||||
# and just setting value to original msg string.
|
||||
|
||||
Reference in New Issue
Block a user