Skip to content

Instantly share code, notes, and snippets.

@OlegTheCat
Last active November 11, 2016 17:23
Show Gist options
  • Save OlegTheCat/1c89365ed0a61fbaff5cb3a22eff1676 to your computer and use it in GitHub Desktop.
Save OlegTheCat/1c89365ed0a61fbaff5cb3a22eff1676 to your computer and use it in GitHub Desktop.
Aleph deadlock
(ns aleph-deadlock.core
(:require [aleph.http :as http]
[manifold.deferred :as d]
[taoensso.timbre :as log])
(:import (java.net InetAddress InetSocketAddress)))
(defn handler [_]
{:status 200
:body "Hello, world"})
(defn start-server []
(let [addr (InetSocketAddress.
(InetAddress/getByName "localhost")
9992)]
(http/start-server #'handler {:socket-address addr})))
(def server (start-server))
(def client-pool (http/connection-pool {:keep-alive? true
:connections-per-host 5}))
(defn make-request []
(http/post "http://localhost:9992" {:headers {"Connection" "Close"} ;; this is important
:pool client-pool}))
(defn make-requests [num-reqs]
(let [req-count (atom 0)]
(->> (repeatedly num-reqs #(make-request))
(map #(d/chain % (fn [_] (log/infof "---> Request done: %s"
(swap! req-count inc))
:ok)))
doall
(apply d/zip))))
;; for :connections-per-host = 5, the below will almost certainly block forever
;; to increase the probability of a deadlock just increase number of requests to make
@(make-requests 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment