Skip to content

Instantly share code, notes, and snippets.

View halgari's full-sized avatar

Timothy Baldridge halgari

View GitHub Profile
@halgari
halgari / gist:6251731
Last active December 21, 2015 04:48
(defn throttle-chan [input]
(let [throttle-chan (chan)
output-chan (chan)]
(go
(loop [throttle-msec nil]
(let [t-chan (when-not (= :disabled throttle-msec)
(timeout throttle-msec))
[val c] (alt! [input throttle-chan])]
(if (= c throttle-chan)
(recur val)
(def a (chan))
(def b (chan))
(go
(>! b (<! a)))
(go
(>! a (<! b)))
@halgari
halgari / gist:6309500
Created August 22, 2013 16:24
Load balancer using core.async
(ns async-examples.load-balancer
(:require [clojure.core.async :refer :all]))
;; Let's assume we have a DB, and it holds the following pairs of name/ages
(def db-store
{:john 42
:amy 33
:jill 3
(ns cljs.core.async.impl.dispatch
(:require [cljs.core.async.impl.buffers :as buffers]))
(def message-channel nil)
(def tasks (buffers/ring-buffer 32))
(def ^:boolean running? false)
(def ^:boolean queued? false)
(def TASK_BATCH_SIZE 1024)
(defn print-fn [x]
(.log js/console x))
(set! *print-fn* print-fn)
(def c (chan))
(def MILLION 1000000)
(def TEN-THOUSAND 10)
(def AMOUNT MILLION)
(def c (chan))
(defn drain [c]
(go
(loop [col []]
(if-let [v (<! c)]
(recur (conj col v))
col))))
(defrecord Jmp [value block]
IInstruction
(reads-from [this] [value])
(writes-to [this] [])
(block-references [this] [block])
ITerminator
(terminate-block [this state-sym _]
`(do (aset-all! ~state-sym ~VALUE-IDX ~value ~STATE-IDX ~block)
:recur)))
(go (->> urls
(map async-http-request)
doall
(apply map< vector)
<!))
(ns dataflow-prototype.core
(:require [clojure.core.async :refer :all]
[clojure.edn :refer [read-string]]))
(defprotocol Lifecycle
(start [service])
(stop [service]))
(defprotocol DataFlowNode
(ns rabbit-mq-test.core
(:require [clojure.core.async :refer :all])
(:import [com.rabbitmq.client ConnectionFactory Connection Channel QueueingConsumer Consumer DefaultConsumer]))
(set! *warn-on-reflection* true)
(defprotocol Lifecycle
(start [service])
(stop [service]))