Fork me on GitHub
#core-async
<
2015-07-21
>
danielcompton18:07:15

Is there a way to get something like a promise in core.async? When w client connects I establish an initial connection that will be used by subsequent queries. I want to be able to park my go block until the db connection is established.

alejandro19:07:27

@danielcompton: could use a channel with a single value

danielcompton19:07:58

@alejandro: but that channel will be exhausted after the first query, how do I get the connection for subsequent queries?

alejandro19:07:24

gotcha, may be easiest to use a real promise that is delivered on by a callback from a take! operation?

nullptr19:07:01

you could also just have a separate chan for each client needing a connection

danielcompton20:07:29

@nullptr: I only want to make the connection once

danielcompton20:07:36

Looks like ASYNC-103 is what I want

nullptr20:07:52

seems reasonable -- though keep in mind that you can put the same connection on multiple chans

danielcompton20:07:25

@nullptr: I am going to make a query an unbounded number of times, I'm sure there's a way I could do this, but it seems messy

nullptr20:07:39

“software” :)

erik_price21:07:33

You can always just infinite loop:

(defn <get-conn [params]
  (let [out-ch (chan)]
    (go-loop [conn nil]
      (if conn
        (do
          (>! out-ch conn)
          (recur conn))
        (recur (make-db-connection! params))))
    out-ch))

danielcompton22:07:00

@erik_price: I want one db-connection shared, but I see what you’re saying about the pattern

erik_price22:07:17

this does that, for better or for worse

danielcompton22:07:33

@erik_price: oh, I didn’t read that correctly, I thought it recurred with a new db each time

danielcompton22:07:00

could be kinda nice to add checks there if the connection is closed too, e.t.c. Thanks!

erik_price22:07:51

Sure. This can get even bigger if you want. You could even pass in a control channel that would let a caller shut down the current database connection (you’d have to alts on that vs the normal path) if you needed teardown functionality.