Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created September 1, 2017 12:32
Show Gist options
  • Save cgrand/eea3bf5afdfc5554cbd14fb85dee90f9 to your computer and use it in GitHub Desktop.
Save cgrand/eea3bf5afdfc5554cbd14fb85dee90f9 to your computer and use it in GitHub Desktop.
pseudo code for back pressure bridging
(a/go-loop [async-tcp-connections-to-chans ; "foreign" -> ch
buffered ; ch -> data
pressurized] ; ch -> foreign
(let [cnx (select (keys async-tcp-connections-to-chans))
ch (async-tcp-connections-to-chans cnx)
data (read-data cnx)
async-tcp-connections-to-chans (dissoc async-tcp-connections-to-chans cnx)
buffered (assoc buffered ch data)
pressurized (assoc pressurized ch cnx)]
(let [[_ c] (a/alts! (vec buffered)
:default :bp)]
(if (= :default c)
(recur async-tcp-connections-to-chans buffered pressurized)
(let [cnx (pressurized c)]
(recur (assoc async-tcp-connections-to-chans cnx c)
(dissoc buffered c)
(dissoc pressurized c)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment