Skip to content

Commit 6615edd

Browse files
authored
Merge pull request #601 from arnaudgeiser/511
Ensure IdleStateHandler is the first handler
2 parents a267786 + ea2b4f9 commit 6615edd

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/aleph/http/server.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@
505505
ssl?))]
506506

507507
(doto pipeline
508+
(http/attach-idle-handlers idle-timeout)
508509
(.addLast "http-server"
509510
(HttpServerCodec.
510511
max-initial-line-length
@@ -517,7 +518,6 @@
517518
(let [compressor (HttpContentCompressor. (or compression-level 6))]
518519
(.addAfter ^ChannelPipeline %1 "http-server" "deflater" compressor))
519520
(.addAfter ^ChannelPipeline %1 "deflater" "streamer" (ChunkedWriteHandler.))))
520-
(http/attach-idle-handlers idle-timeout)
521521
pipeline-transform))))
522522

523523
;;;

test/aleph/http_test.clj

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
[flow :as flow]
1010
[ssl :as ssl]
1111
[tcp :as tcp]]
12-
[aleph.http.core :as core]
1312
[clj-commons.byte-streams :as bs]
1413
[manifold.deferred :as d]
15-
[manifold.stream :as s])
14+
[manifold.stream :as s]
15+
[manifold.time :as t])
1616
(:import
1717
(java.io
1818
File)
@@ -490,6 +490,45 @@
490490
(set-prop initial-threads-property)
491491
(clear-prop))))
492492

493+
(defn- slow-stream
494+
"Produces a `manifold.stream` which yield a value
495+
every 50 milliseconds six times."
496+
[]
497+
(let [body (s/stream 10)]
498+
(-> (d/loop [cnt 0]
499+
(t/in 50
500+
(fn []
501+
(d/chain' (s/put! body (str cnt))
502+
(fn [_]
503+
(when (< cnt 5)
504+
(d/recur (inc cnt))))))))
505+
(d/chain' (fn [_] (s/close! body))))
506+
body))
507+
508+
(deftest test-idle-timeout
509+
(let [url (str "http://localhost:" port)
510+
echo-handler (fn [{:keys [body]}] {:body (bs/to-string body)})
511+
slow-handler (fn [_] {:body (slow-stream)})]
512+
(testing "Server is slow to write"
513+
(with-handler-options slow-handler {:idle-timeout 200
514+
:port port}
515+
(is (= "012345" (bs/to-string (:body @(http/get url)))))))
516+
(testing "Server is too slow to write"
517+
(with-handler-options slow-handler {:idle-timeout 30
518+
:port port}
519+
(is (= ""
520+
(bs/to-string (:body @(http/get url)))))))
521+
(testing "Client is slow to write"
522+
(with-handler-options echo-handler {:idle-timeout 200
523+
:port port
524+
:raw-stream? true}
525+
(is (= "012345" (bs/to-string (:body @(http/put url {:body (slow-stream)})))))))
526+
(testing "Client is too slow to write"
527+
(with-handler-options echo-handler {:idle-timeout 30
528+
:port port
529+
:raw-stream? true}
530+
(is (thrown-with-msg? Exception #"connection was close"
531+
(bs/to-string (:body @(http/put url {:body (slow-stream)})))))))))
493532
;;;
494533

495534
(deftest test-large-responses

0 commit comments

Comments
 (0)