core-async

abdullahibra 2020-10-30T20:44:10.152200Z

Hi everyone,

abdullahibra 2020-10-30T20:45:58.153Z

is it possible to do something like:

(a/go 
(doseq [m messages*]
      (a/go
        (client/send! c/kafka-producer "topic" id h))

abdullahibra 2020-10-30T20:46:30.153600Z

what i mean, is it possible to nest a/go ?, is that a bad habit or something wrong in the example above?

phronmophobic 2020-10-30T20:51:49.153800Z

it's possible to nest go blocks, although it may have unexpected results if you don't have the right mental model for what a nested go block implies. the above example might be ok, but it depends on whether messages* blocks when you iterate over it. If iterating over messages* does block, then it probably does not belong in a go block.

phronmophobic 2020-10-30T20:53:04.154200Z

otherwise, the above example is fine. however, it probably doesn't have much benefit in practice over:

(doseq [m messages*]
      (a/go
        (client/send! c/kafka-producer "topic" id h)))

phronmophobic 2020-10-30T20:54:02.154400Z

why not just do the following?

(doseq [m messages*]
 (client/send! c/kafka-producer "topic" id h))

abdullahibra 2020-10-30T20:57:32.154700Z

client/send! is asynchronous ?

abdullahibra 2020-10-30T20:57:54.154900Z

i assumed itโ€™s not and use a/go to make it async

phronmophobic 2020-10-30T21:02:50.155100Z

why do you want to make it asynchronous?

phronmophobic 2020-10-30T21:14:39.155300Z

conceptually, a go block that doesn't interact with any channels is just queuing up some work to do on a thread pool. it doesn't seem like running client/send! from a thread pool versus the current thread makes much of a difference unless there something else going on

2020-10-30T21:54:51.155500Z

also, if client/send! can take long enough to effectively matter, it should be inside thread, as putting it in go will clog up the limited core.async threadpool

2020-10-30T21:55:23.155700Z

@abdullahibra this is a very common error, it behaves fine in unit tests and simple experiments, and has severe consequences under load

2020-10-30T21:57:24.155900Z

also, given that kafka enforces ordering internally, writing to one topic from N concurrent threads will be slower than doing them one at a time

abdullahibra 2020-10-30T22:09:10.156100Z

Perfect, thanks guys ๐Ÿ™‚