Phill Wolf
2016-01-05 18:15:15 UTC
Sometimes, the Ring handler observes a nil :remote-addr in the request.
The REPL exercise below can demonstrate the phenomenon. Start the server;
go to a web browser, visit the page, and lean on the Refresh button for a
while; then return to the REPL and look at the atom that counts nil
:remote-addr.
I expected :remote-addr to be non-nil (after all, the request came from
somewhere), so I am not sure what this means.
I used [aleph "0.4.1-beta3"] and Firefox.
Here is the REPL exercise:
(ns simplest-server
(:require [aleph.http :as http]))
(def nils (atom 0))
(defn handler-slow [req]
(Thread/sleep 1000)
(when (nil? (:remote-addr req))
(swap! nils inc))
{:status 200
:headers {"content-type" "text/plain"}
:body "hello!"})
(comment
@nils
;; Serves slowly.
;; Auto-repeat the browser Refresh for a while, then check @nils.
(def listening
(http/start-server handler-slow {:port 8080}))
(.close listening)
@nils
)
As an alternative, I tried testing :remote-addr before the Thread/sleep. I
did not observe any nils. I infer that :remote-addr is mutable or subject
to some kind of race condition. The request map appears to be a facade on
Netty's HttpRequest and Channel, defined by (p/def-derived-map
NettyRequest...), so perhaps other members are mutable too?
The REPL exercise below can demonstrate the phenomenon. Start the server;
go to a web browser, visit the page, and lean on the Refresh button for a
while; then return to the REPL and look at the atom that counts nil
:remote-addr.
I expected :remote-addr to be non-nil (after all, the request came from
somewhere), so I am not sure what this means.
I used [aleph "0.4.1-beta3"] and Firefox.
Here is the REPL exercise:
(ns simplest-server
(:require [aleph.http :as http]))
(def nils (atom 0))
(defn handler-slow [req]
(Thread/sleep 1000)
(when (nil? (:remote-addr req))
(swap! nils inc))
{:status 200
:headers {"content-type" "text/plain"}
:body "hello!"})
(comment
@nils
;; Serves slowly.
;; Auto-repeat the browser Refresh for a while, then check @nils.
(def listening
(http/start-server handler-slow {:port 8080}))
(.close listening)
@nils
)
As an alternative, I tried testing :remote-addr before the Thread/sleep. I
did not observe any nils. I infer that :remote-addr is mutable or subject
to some kind of race condition. The request map appears to be a facade on
Netty's HttpRequest and Channel, defined by (p/def-derived-map
NettyRequest...), so perhaps other members are mutable too?
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.