Skip to content

Instantly share code, notes, and snippets.

@dotemacs
Forked from cgrand/bp.clj
Created September 3, 2017 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotemacs/28279b24354e7d3cc3fb04ace0f90558 to your computer and use it in GitHub Desktop.
Save dotemacs/28279b24354e7d3cc3fb04ace0f90558 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