不通过IPv4接收与Aleph的异步响应,而是使用IPv6

时间:2014-04-15 13:07:13

标签: networking clojure ip compojure aleph

我正在尝试使用ClojureAleph中设置服务器发送的事件,但它只是在IPv4上工作。如果我通过IPv6连接,一切都很好。这在Linux和MacOS上都会发生。我已经有了一个关于我所说的on GitHub的完整例子。

我认为我没有做任何特别喜欢的事情。整个代码在GitHub上,但基本上我的程序是:

(def my-channel (permanent-channel))

(defroutes app-routes
  (GET "/events" []
    {:headers {"Content-Type" "text/event-stream"}
     :body my-channel}))

(def app
  (handler/site app-routes))

(start-server (wrap-ring-handler app) {:port 3000}))

但是,当我连接到127.0.0.1:3000时,我可以看到curl发送请求标头,但它只是挂起,从不打印响应标头:

$ curl -vvv http://127.0.0.1:3000/events

* About to connect() to 127.0.0.1 port 3000 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x7f920a004400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f920a004400) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET /events HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 127.0.0.1:3000
> Accept: */*

如果我通过IPv6连接,响应会立即生效,并且我在通道中enqueue的事件会被正确发送:

$ curl -vvv http://localhost:3000/events

* Adding handle: conn: 0x7f943c001a00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f943c001a00) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 3000 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 3000 (#0)
> GET /events HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
* Server aleph/0.3.0 is not blacklisted
< Server: aleph/0.3.0
< Date: Tue, 15 Apr 2014 12:27:05 GMT
< Connection: keep-alive
< Content-Type: text/event-stream
< Transfer-Encoding: chunked

我还在Chrome中重现了这种行为。在IPv4和IPv6情况下,tcpdump显示响应标头正在通过网络传输。

lein run和uberjar都会出现此问题。如果我用-Djava.net.preferIPv4Stack=true执行uberjar,也会发生这种情况。

如何使我的应用程序在IPv4上的行为与IPv6相同?

0 个答案:

没有答案