Fork me on GitHub
#core-async
<
2020-10-30
>
abdullahibra20:10:58

is it possible to do something like:

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

abdullahibra20:10:30

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

phronmophobic20:10:49

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.

phronmophobic20:10:04

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)))

phronmophobic20:10:02

why not just do the following?

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

abdullahibra20:10:32

client/send! is asynchronous ?

abdullahibra20:10:54

i assumed it’s not and use a/go to make it async

phronmophobic21:10:50

why do you want to make it asynchronous?

phronmophobic21:10:39

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

noisesmith21:10:51

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

noisesmith21:10:23

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

noisesmith21:10:24

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

abdullahibra22:10:10

Perfect, thanks guys 🙂

Ben Sless03:10:44

This is Kafka specific but keep in mind compression happens on your thread, so while send is asynchronous, when the buffer is full you'll block for the duration of compression. What I usually do with a producer is put it in a loop inside a real thread which takes the messages from a channel.

👍 6