core-async

gws 2019-05-28T05:13:13.012100Z

yeah that's a good point, the order isn't defined with a/merge, so it wouldn't be strictly ordered by arrival time if you e.g. waited a few seconds to take from the merged channel of typical web service calls

đź‘Ť 1
leontalbot 2019-05-28T13:32:16.013300Z

Really interesting. In my case, as long as a have the fastest 4, I don’t care about the order within those 4.

leontalbot 2019-05-28T13:32:56.014200Z

One thing I wonder is, if for some reason service is not available, how would you do a timeout to ensure this doesn’t block forever?

leontalbot 2019-05-28T13:33:19.014700Z

cc @dpsutton @gws

dpsutton 2019-05-28T13:34:53.015600Z

merge takes channels so you could just add four (a/timeout your-preferred-delay) or if doing my original way make each request alt on the actual response or a timeout channel

🙌 1
leontalbot 2019-05-28T14:02:50.016200Z

right, something like that:

(defn random-get [response-ch n]
    (a/go
      (let [wait (rand 4000)
            res (a/alt! (a/timeout wait) wait
                        (a/timeout 1000) :skipped)]
        (a/>! response-ch [n res]))))

(let [results-chan (a/chan 3 (take 4))]
  (a/go
    (dotimes [n 10]
      (random-get results-chan n)))
  (prn (repeatedly 4 #(a/<!! results-chan))))