[英]Clojure -way of building live-stream server — with promises, but is it the right way
我正在尋找構建實時流媒體服務器的方法。 我正在努力解決的特殊問題是如何將值從單個提供程序(網絡攝像頭)發送到未定義數量的線程(連接的客戶端)。 顯然,當客戶端連接它對網絡攝像頭的完整視頻文件不感興趣時,基本上需要發送一個標題,然后在那個確切的時刻發送來自網絡攝像頭的任何軟件包。
在直接的java中,我認為這很容易。 每當客戶端連接時,將連接添加到陣列,當它斷開連接時從陣列中刪除連接,並且每當網絡攝像頭的新包到達時,將其發送到陣列中的每個條目。 鎖定數組,以便我們添加/刪除條目,或循環通過它來發送數據包。 當然,我們可以在clojure中建立同樣的東西,但這聽起來真的很邪惡。
在傳遞多線程架構的消息中,這聽起來同樣容易。
我在clojure中可以想到的唯一解決方案是使用懶惰的承諾序列。 確實它有效,但我想知道是否有另一種方式可以導致更清晰的代碼和更多的clojure-zen :)
只是為了說明:一個簡化的問題,有承諾和原子:
一個提供程序函數生成數據,一個線程讀取此數據。 后來創建了一些其他線程,希望從第一個線程獲取數據,但無法獲取。
(defn provider []
(lazy-seq
(do
(Thread/sleep 100)
(cons (rand) (provider)))))
(def printer (agent nil))
(defn log [& line]
(send-off printer (fn [x] (apply println line))))
(def promises (atom (repeatedly promise)))
(defn client-connected-thread [x input]
(log "Client connection " x " is connected with the provider and just received" @(first input))
(recur x (rest input)))
(.start (Thread. (fn []
(loop [stream (provider)]
(when-let [item (first stream)]
(log "I received " item", will share now")
(deliver (first @promises) item)
(swap! promises rest))
(recur (rest stream))))))
(Thread/sleep 300)
(.start (Thread. #(client-connected-thread 1 @promises)))
(Thread/sleep 100)
(.start (Thread. #(client-connected-thread 2 @promises)))
(Thread/sleep 50)
(.start (Thread. #(client-connected-thread 3 @promises)))
所以,基本上問題是:這是解決這個問題的正確方法嗎?
此外,我們在這里討論流媒體服務器,因此提供商功能每秒將提供數萬個項目,並且可能有10個客戶端連接。 承諾系統是否意味着如此大量使用?
Clojure具有代理,用於需要異步發送信息的情況,這似乎與您的用例非常匹配。
你確實非常接近,只是將工作代理卡在幾個地方完成了。
“在直接的Clojure中 ,我認為這很容易。每當客戶端連接時,將連接添加到代理中的代理向量 ,當它斷開連接時從代理的代理中刪除連接,並且每當來自網絡攝像頭的新包到達時將其發送給代理商中的每個代理商 。“
確保使用send-off
而不是send
來防止清空線程池。
這比“鎖定數組”方法有許多優點:
粗略的輪廓看起來像:
user> (def connections-stub (range))
user> (def connections (agent []))
#'user/connections
user> (defn accept-connection [connection]
(send connections conj (agent connection)))
#'user/accept-connection
user> (map accept-connection (take 10 connections-stub))
(#<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4> #<Agent@4c260132: 5>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4> #<Agent@4c260132: 5> #<Agent@5318a0ac: 6>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4> #<Agent@4c260132: 5> #<Agent@5318a0ac: 6> #<Agent@75dca6d2: 7>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4> #<Agent@4c260132: 5> #<Agent@5318a0ac: 6> #<Agent@75dca6d2: 7> #<Agent@694c6171: 8>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4> #<Agent@4c260132: 5> #<Agent@5318a0ac: 6> #<Agent@75dca6d2: 7> #<Agent@694c6171: 8> #<Agent@159177b9: 9>]> #<Agent@2a81df82: [#<Agent@3478e59b: 0> #<Agent@6d2a3e06: 1> #<Agent@704e33e7: 2> #<Agent@1e31bc4b: 3> #<Agent@5340ef69: 4> #<Agent@4c260132: 5> #<Agent@5318a0ac: 6> #<Agent@75dca6d2: 7> #<Agent@694c6171: 8> #<Agent@159177b9: 9>]>)
user> (defn send-frame [con-agent frame]
(send con-agent
(fn [connection frame]
(println "sending " frame " to " connection) connection) frame))
#'user/send-frame
user> (send-frame (first @connections) "hello")
sending hello to 0
#<Agent@da69a9c: 0>
user> (defn dispatch-frame [frame]
(doall (map #(send-frame % frame) @connections)))
#'user/dispatch-frame
user> (dispatch-frame "hello")
sending hello to 0
sending hello to 1
sending hello to 2
sending hello to 3
sending hello to 4
sending hello to 5
sending hello to 6
sending hello to 7
sending hello to 8
sending hello to 9
(#<Agent@da69a9c: 0> #<Agent@34f07ec4: 1> #<Agent@11ee68d1: 2> #<Agent@3b237a89: 3> #<Agent@1641d6b4: 4> #<Agent@3c76ced6: 5> #<Agent@1c05629d: 6> #<Agent@258d3fca: 7> #<Agent@5c56fa08: 8> #<Agent@52395294: 9>)
user>
看看Aleph吧 。 這是一個用於提供“異步通道”的庫,可以幫助您實現所需的方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.