例如:
(: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”。但是在回调函数内部,如何查看是否由于超时而被调用。请告诉我是否有办法这样做。谢谢。
最佳答案
这是您轻松重现超时的方法:
(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 的实例
因此您必须更改回调以接受 map 。如果发生错误,此映射中的 :error 字段不为零,如果发生超时,它将包含 TimeoutException。顺便说一句,这只是 the client documentation 中的示例稍作修改。 - 我认为那里解释得很好。
所以尝试将回调更改为:
(defn post-callback
[{:keys [status headers body error]}]
;; and check the error same way as I do above
)
关于clojure - 如何判断http-kit客户端回调超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241494/