This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-10
Channels
- # announcements (1)
- # babashka (16)
- # beginners (42)
- # calva (6)
- # clj-kondo (7)
- # clj-yaml (13)
- # cljdoc (7)
- # clojure (34)
- # clojure-bay-area (7)
- # clojure-dev (14)
- # clojure-europe (10)
- # clojure-nl (1)
- # clojure-norway (26)
- # clojure-sweden (91)
- # clojurescript (7)
- # datalevin (3)
- # datomic (35)
- # dev-tooling (8)
- # emacs (22)
- # graphql (1)
- # honeysql (13)
- # hyperfiddle (28)
- # introduce-yourself (1)
- # jobs-discuss (11)
- # juxt (4)
- # lsp (21)
- # off-topic (7)
- # reagent (4)
- # releases (3)
- # spacemacs (16)
- # xtdb (4)
- # yamlscript (2)
Been playing around with Executors/newVirtualThreadPerTaskExecutor
in babashka, and it works great. Does anyone have any reference implementation on how to constrain the amount of concurrent threads when using virtual threads? Like a fixed thread pool, just with virtual ones. Reason I'm asking is because I'm working against an API that limits the amount of concurrent requests being done from one client
I guess it's not a specific babashka question so you could also try #C03S1KBA2 or even StackOverflow (it's basically a general Java question) if you don't get good answers here
Agreed, just thought I'd start here since I've seen a few discussions about the virtual threads here earlier! Thanks
I would suggest you try to limit the number of such requests at a higher level, perhaps by using a bounded queue.
One simple usage of a bounded queue is in the source of tap>
(defn tap>
...
(force tap-loop)
(.offer tapq (if (nil? x) ::tap-nil x)))
where tapq
is defined above
(defonce ^:private ^java.util.concurrent.ArrayBlockingQueue tapq (java.util.concurrent.ArrayBlockingQueue. 1024))
It is recommended to use Semaphores to control concurrency https://blogs.oracle.com/javamagazine/post/java-loom-virtual-threads-platform-threads Check the section "Three pieces of practical advice".
Usually you should not need to, you can run millions of them in fact. If target resource needs to be controlled for concurrency (like connection or so) semaphore is a good option.
...also bounds can be set using structured concurrency pattern (StructuredTaskScope) I don't know if this is supported in babashka though.
not sure, but semaphore should work. countdownlatch could also work, but this is not in bb yet I think
StructuredTaskScope I think it's a bit clumsy for use in Lisp. It's too javaish. But I can imagine some sort of macro that can do this in elegant Lispy way.