|
151 | 151 | process-fn
|
152 | 152 | max-workers]
|
153 | 153 | :or {max-workers 2}}]
|
154 |
| - (let [workers (atom #{}) |
| 154 | + (let [executor (ua/make-virtual-thread-executor) |
| 155 | + workers (atom #{}) |
155 | 156 | ;; Use a promise so we can access it in the `on-add` function
|
156 | 157 | grouped-queue (promise)
|
157 | 158 | on-add (fn []
|
158 | 159 | (when (< (count @workers) max-workers)
|
159 |
| - (ua/vfuture |
160 |
| - (loop [worker-id (Object.)] |
161 |
| - (when (contains? (swap! workers |
162 |
| - (fn [workers] |
163 |
| - (if (= (count workers) max-workers) |
164 |
| - workers |
165 |
| - (conj workers worker-id)))) |
166 |
| - worker-id) |
167 |
| - (try |
168 |
| - (loop [] |
169 |
| - (when (process! @grouped-queue {:reserve-fn reserve-fn |
170 |
| - :process-fn process-fn}) |
171 |
| - ;; Continue processing items until the queue is empty |
172 |
| - (recur))) |
173 |
| - (catch Throwable t |
174 |
| - (tracer/record-exception-span! t {:name "grouped-queue/process-error"})) |
175 |
| - (finally |
176 |
| - (swap! workers disj worker-id))) |
177 |
| - ;; One last check to prevent a race where something is added to the queue |
178 |
| - ;; while we're removing ourselves from the workers |
179 |
| - (when (and (peek @grouped-queue) |
180 |
| - (< (count @workers) max-workers)) |
181 |
| - (recur worker-id)))))))] |
| 160 | + (ua/worker-vfuture |
| 161 | + executor |
| 162 | + (loop [worker-id (Object.)] |
| 163 | + (when (contains? (swap! workers |
| 164 | + (fn [workers] |
| 165 | + (if (= (count workers) max-workers) |
| 166 | + workers |
| 167 | + (conj workers worker-id)))) |
| 168 | + worker-id) |
| 169 | + (try |
| 170 | + (loop [] |
| 171 | + (when (process! @grouped-queue {:reserve-fn reserve-fn |
| 172 | + :process-fn process-fn}) |
| 173 | + ;; Continue processing items until the queue is empty |
| 174 | + (recur))) |
| 175 | + (catch Throwable t |
| 176 | + (tracer/record-exception-span! t {:name "grouped-queue/process-error"})) |
| 177 | + (finally |
| 178 | + (swap! workers disj worker-id))) |
| 179 | + ;; One last check to prevent a race where something is added to the queue |
| 180 | + ;; while we're removing ourselves from the workers |
| 181 | + (when (and (peek @grouped-queue) |
| 182 | + (< (count @workers) max-workers)) |
| 183 | + (recur worker-id)))))))] |
182 | 184 | (deliver grouped-queue (create {:group-fn group-fn
|
183 | 185 | :on-add on-add}))
|
184 | 186 | {:grouped-queue @grouped-queue
|
185 |
| - :get-worker-count (fn [] (count @workers))})) |
| 187 | + :get-worker-count (fn [] (count @workers)) |
| 188 | + :virtual-thread-executor executor})) |
186 | 189 |
|
187 | 190 | (comment
|
188 | 191 | (def gq (create {:group-fn :k}))
|
|
205 | 208 | #_(Thread/sleep 10000)
|
206 | 209 | (println "done"))}))
|
207 | 210 |
|
| 211 | + (require 'clojure.tools.logging) |
| 212 | + |
208 | 213 | (defn test-grouped-queue []
|
209 | 214 | (let [finished (promise)
|
210 | 215 | started (promise)
|
|
217 | 222 | (inflight-queue-reserve (max 1 (rand-int 25)) iq))
|
218 | 223 | :process-fn (fn [_ workset]
|
219 | 224 | @started
|
220 |
| - (tracer/record-info! {:name "workset" |
221 |
| - :attributes {:workset-count (count workset) |
222 |
| - :total total-items |
223 |
| - :worker-count ((:get-worker-count @gq))}}) |
| 225 | + (clojure.tools.logging/info {:name "workset" |
| 226 | + :attributes {:workset-count (count workset) |
| 227 | + :total total-items |
| 228 | + :worker-count ((:get-worker-count @gq))}}) |
224 | 229 | (.addAndGet process-total (count workset))
|
225 | 230 | (when (zero? (.addAndGet total-items (- (count workset))))
|
226 | 231 | (deliver finished true))
|
227 | 232 | nil)
|
228 |
| - :max-workers 100}) |
| 233 | + :max-workers 1000}) |
229 | 234 | _ (deliver gq q)
|
230 | 235 |
|
231 | 236 | wait (future
|
|
247 | 252 | (deliver started true)
|
248 | 253 | (tool/def-locals)
|
249 | 254 | @wait))
|
| 255 | + |
250 | 256 | (test-grouped-queue))
|
0 commit comments