Fork me on GitHub

Looking for a quick review of a small gist: - in general, I'm just unsure whether this is the right approach for ensuring that only one instance of a stateful resource is created when multiple threads might be requesting that resource at any time.


hey @tomc with jdbc data sources in particular, these semantics are handled internally in pool libraries like c3po. something like is a reasonable approach


c3po has internal locking and concurrency handling and so forth


another angle- the posted code is in the vein of a semaphore pattern in clojure- managing a counted resource- while using agents for mutual exclusion. conceptually that's fine, though there are more idiomatic approaches. however, there is a concrete reason not to use agents in a cases where there may be timing expectations on the resource consumer (e.g. usually when people want to talk to the database, they want to do it NOW)- send-off will cause the function to run in another thread, the scheduling around which is not guaranteed. agents are mostly useful for decoupling work that is not timing sensitive. say you have a bunch of time-sensitive jobs doing work and you want them to log when they're done, and because logging (due to file system hiccups) can block, it's a good idea to send-off the logging function.


await with agents is sometimes necessary but should be unusual- better to use something like core.async when that kind of coordination is necessary


anyway- hope that's helpful. good luck!