FFFF Make sending anything wait until downstream component is ready by kenhkan · Pull Request #40 · noflo/noflo · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
61 changes: 61 additions & 0 deletions src/lib/NoFlo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class NoFlo
connections: []
graph: null
startupDate: null
portBuffer: {}

constructor: (graph) ->
@processes = {}
Expand Down Expand Up @@ -145,6 +146,33 @@ class NoFlo
socket.on "data", (data) ->
logSocket "DATA"

# Release the IPs buffered because of an un-ready component
flushPortBuffer: (id) ->
buffer = @portBuffer[id]
inports = buffer.ins
outports = buffer.outs

# Notify outports FIRST so that connections are attached because the floodgate is opened
for port in outports
port.emit("ready")

for port in inports
port.emit("ready")

delete @portBuffer[id]

# Set up a buffer for an un-ready component's port
setupPortBuffer: (id) ->
if @portBuffer[id]?
@portBuffer[id].count++
else
@portBuffer[id] =
ins: [] # Inports of upstream components of an un-ready component that need to buffer
outs: [] # Outports of an un-ready component
count: 1 # A stack count to make sure all IPs are flushed at the same time with the right order of setup (i.e. outports of an un-ready component are set first)

@portBuffer[id]

addEdge: (edge) ->
return @addInitial(edge) unless edge.from.node
socket = internalSocket.createSocket()
Expand All @@ -156,17 +184,50 @@ class NoFlo
unless from.component
throw new Error "No component defined for outbound node #{edge.from.node}"
unless from.component.isReady()
buffer = @setupPortBuffer(from.id)

from.component.once "ready", =>
@addEdge edge

# When the "from" component isn't ready, it's an outgoing port
fromPort = from.component.outPorts[edge.from.port]
buffer.outs.push(fromPort)

# Decrement the count and flush the buffer on empty stack ON NEXT CYCLE
next = () =>
buffer.count--
if buffer.count is 0
@flushPortBuffer(from.id)

setTimeout(next, 0)

return

to = @getNode edge.to.node
unless to
throw new Error "No process defined for inbound node #{edge.to.node}"
unless to.component
throw new Error "No component defined for inbound node #{edge.to.node}"
unless to.component.isReady()
fromPort = from.component.outPorts[edge.from.port]
fromPort.downstreamIsGettingReady = true
buffer = @setupPortBuffer(from.id)

to.component.once "ready", =>
@addEdge edge

# When the "to" component isn't ready, it's an incoming port
fromPort = from.component.outPorts[edge.from.port]
buffer.ins.push(fromPort)

# Decrement the count and flush the buffer on empty stack ON NEXT CYCLE
next = () =>
buffer.count--
if buffer.count is 0
@flushPortBuffer(from.id)

setTimeout(next, 0)

return

@connectPort socket, to, edge.to.port, true
Expand Down
49 changes: 49 additions & 0 deletions src/lib/Port.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ class Port extends events.EventEmitter
@name = name
@socket = null
@from = null
@downstreamIsGettingReady = false
@groups = []
@data = []
@buffer = []

@on "ready", () =>
if @downstreamIsGettingReady
# Reset the flag
@downstreamIsGettingReady = false

# Each record in buffer is a separate connection
for conn in @buffer
for group in conn.groups
@beginGroup(group)

for datum in conn.data
@send(datum)

for group in conn.groups
@endGroup()

@disconnect()

@buffer = []

attach: (socket) ->
throw new Error "#{@name}: Socket already attached #{@socket.getId()} - #{socket.getId()}" if @socket
Expand All @@ -29,10 +53,17 @@ class Port extends events.EventEmitter
@emit "disconnect", socket

connect: ->
if @downstreamIsGettingReady
return

throw new Error "No connection available" unless @socket
do @socket.connect

beginGroup: (group) ->
if @downstreamIsGettingReady
@groups.push(group)
return

throw new Error "No connection available" unless @socket

return @socket.beginGroup group if @isConnected()
Expand All @@ -42,6 +73,10 @@ class Port extends events.EventEmitter
do @socket.connect

send: (data) ->
if @downstreamIsGettingReady
@data.push(data)
return

throw new Error "No connection available" unless @socket

return @socket.send data if @isConnected()
Expand All @@ -51,10 +86,24 @@ class Port extends events.EventEmitter
do @socket.connect

endGroup: ->
if @downstreamIsGettingReady
return

throw new Error "No connection available" unless @socket
do @socket.endGroup

disconnect: ->
if @downstreamIsGettingReady
buffer =
groups: @groups
data: @data
@buffer.push(buffer)

@groups = []
@data = []

return

return unless @socket
@socket.disconnect()

Expand Down
0