我有一些长时间运行的进程,当进程完成时,它会返回一个带有结果的 core.async 通道。
现在我想使用带有 HTTP-kit 的长轮询返回该结果。不幸的是,我有点困惑正确的做法是什么。
目前我有一个处理程序(连接到一个路由),它启动处理调用并在完成后发送结果:
(defn handler
[request]
(let [c (process request)] ;; long running process that returns a channel
(http/with-channel request channel
(http/send! channel {:status 200
:body (<!! (go (<! c)))))
(http/on-close channel (fn [_] (async/close! c))))))
它有效,但我不确定这是否是正确的方法。
编辑因为<!!
是阻塞我现在在 go-loop 中尝试一个非阻塞变体
(defn handler
[request]
(let [c (process request)]
(http/with-channel request channel
(async/go-loop [v (<! c)]
(http/send! channel {:status 200
:body v}))
(http/on-close channel (fn [_] (async/close! c))))))