Skip to content

Instantly share code, notes, and snippets.

@hlship
Last active March 26, 2023 17:25
Show Gist options
  • Save hlship/bd3938545395be7e0545 to your computer and use it in GitHub Desktop.
Save hlship/bd3938545395be7e0545 to your computer and use it in GitHub Desktop.
(def payment-codes-per-block 4096)
(defn create-payment-code-feed
[db]
(let [payment-code-feed (chan 10)]
(go-loop []
(let [block-id (allocate-block db)
block-start (* block-id payment-codes-per-block)]
(-> (onto-chan payment-code-feed
(->>
(range block-start (+ block-start payment-codes-per-block))
(map sa/int->six-alpha))
;; Don't close!
false)
;; park here until all of that block has been consumed.
<!))
;; Loop forever.
(recur))
payment-code-feed))
@glangford
Copy link

Very nice, and I also like the related article
https://medium.com/@hlship/better-abstractions-with-core-async-1cd5fa509eca

A very minor nit - for someone else reading the code, I think it is more clear to omit the threading macro on line 9. It is more explicit that the parking is waiting for onto-chan to finish if the code is closer together :

(<! (onto-chan payment-code-feed
                             (->>
                                (range block-start (+ block-start payment-codes-per-block))
                                (map sa/int->six-alpha))
                              ;; Don't close!
                              false)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment