4

例如:

(:require [org.httpkit.client :as http])

(defn post-callback
 []
 ;; how to know if it is due to timeout?
 )

(def options {:body "abc" :timeout 1000})
(http/post "some-url" options post-callback)

如果“some-url”关闭,则在超时时,将调用“post-callback”。但是在回调函数内部,如何查看是否因为超时而被调用。请让我知道是否有办法这样做。谢谢。

4

1 回答 1

3

这是您可以轻松重现 timeout 的方法:

(http/get "http://google.com" {:timeout 1}
         (fn [{:keys [status headers body error]}] ;; asynchronous response handling
           (if error
             (do
               (if (instance? org.httpkit.client.TimeoutException error)
                 (println "There was timeout")
                 (println "There wasn't timeout"))
               (println "Failed, exception is " error))
             (println "Async HTTP GET: " status))))

它将打印错误,这是 org.httpkit.client.TimeoutException 的一个实例

因此,您必须更改回调以接受地图。如果出现错误,此映射中的 :error 字段不为零,如果超时,它将包含 TimeoutException。顺便说一句,这只是客户端文档中稍作修改的示例- 我认为它在那里得到了很好的解释。

因此,尝试将您的回调更改为:

(defn post-callback
  [{:keys [status headers body error]}]
  ;; and check the error same way as I do above
)
于 2015-08-27T07:30:18.873 回答