Skip to content

Instantly share code, notes, and snippets.

View ztellman's full-sized avatar
💭
boiling the ocean

Zach Tellman ztellman

💭
boiling the ocean
View GitHub Profile

Hey all,

In preparation for Clojure/West, I'm formally releasing the latest Aleph and the libraries that surround it. Aleph 0.4.0 has been running in production at Factual for half a year now, and across a variety of services is handling at peak 600k HTTP requests/sec (spread across 15-20 machines).

Since the landscape of Clojure HTTP servers is pretty crowded these days, it's worth taking some time to explain how Aleph differs. To be clear, most Clojure deployments likely use Jetty, and should continue to do so. However, Aleph has some unique properties:

  • It uses the Netty library, which is a high-performance and very battle-tested network layer for the JVM
  • It is the only HTTP server that has ubiquitous asynchronous streams wherever data can be received or sent (all other libraries can only represent streaming requests using InputStreams, or like http-kit don't support streaming HTTP requests at all)
  • It is the only server that has a WebSocket implementation with any support for per-connection
(headers :uint16-be
(fn [len]
(if (zero? len)
nil-frame
[(string "iso-8859-1" :length len)
(string "iso-8859-1" :prefix :uint16-be)]))
(fn [tuple]
(if tuple
0
(count (first tuple)))))
(require '[manifold.stream :as s])
(->> (s/periodically period sample-state)
s/stream->seq
(take max-tries)
(remove unsettled?)
first)
(require '[manifold.deferred :as d])
(require '[manifold.stream :as s])
(defn tcp-handler [s conn-info]
(d/let-flow [s (wrap-stream protocol s)
msg (s/take! s)
_ (s/put! s (response msg))]
(set-up-message-handling s)))
(defn split-spliced-stream [s]
(let [in (a/chan)
out (a/chan)]
(s/connect s in)
(s/connect out s)
[in out]))
(import
'[java.awt.geom
Ellipse2D$Double
Rectangle2D$Double])
(defn rectangle [x y w h]
(Rectangle2D$Double. x y w h))
(defn circle [x y r]
(Ellipse2D$Double. (- x r) (- y r) (* r 2) (* r 2)))
(ns com.x.aleph-test
(require [aleph.http :as http]
[manifold.deferred :as d]
[manifold.stream :as s]
[compojure.core :only [routes ANY GET PUT POST] :as compojure]
[compojure.handler :as handler]
[clj-http.client :as http-client]
))
(comment
(require
'[manifold.stream :as s])
(defn cancellable-connect [a b]
(let [bridge (s/stream)]
(s/connect a bridge)
(s/connect bridge b)
#(s/close! bridge)))
(require
'[manifold.deferred :as d]
'[manifold.stream :as s])
(defn take-first! [pred s]
(d/loop []
(d/chain (s/take! s ::none)
#(cond
(identical? ::none %)
nil
(require '[manifold.stream :as s])
(defn streaming-response [ch]
{:status 200
:headers {"content-type" "text/plain"}
:body (s/->stream ch)})