• Skip to main content
  • Skip to footer

PurelyFunctional.tv

  • Learn Clojure
  • About
  • 🛒 Cart
  • Log in

Use advanced patterns in core.async

core.async is very powerful, but sometimes it can be hard to know exactly how to use it. Knowing the individual pieces is not enough. You've got to master the pieces, but then you've got to be thinking in terms of broader patterns. This course presents some powerful patterns, and hopefully points the way to using core.async in a more holistic way. I really like how little code it takes to build powerful concurrency patterns in core.async.

In fact, core.async has changed my code tremendously. It is great for mastering:

  • complex timing, like timeouts, rate limiting, and starting multiple things at the same time
  • preventing too much work from overloading your server
  • and dealing with values that change over time

core.async Patterns

Updated February 26, 2021

Want this course?

Online License

$18
Get this course
--
Watch Online
Lifetime Access
For an individual
Buy for $18

Love it or leave it guarantee

If you don't learn as much from this course as you thought, just ask for a refund within 30 days and I'll give you your money back.

Video time: 50m02s
0 / 10
0 / 10

Lessons

6 min

1. Take with Timeout

What if you want to take from a channel, but give up after a time? This is very useful when testing that something was put onto a channel. You can't wait forever, because then your tests will never finish. core.async makes this quite easy.

1 min

2. Put with Timeout

We saw how to take with a timeout. But how do you put with a timeout? If a channel is full, a put will block or park until it is empty. If that never happens, your code will never complete. core.async can do this as well.

7 min

3. go-throw

I often have trouble figuring out when my go blocks fail and why. Exceptions are silently ignored. We can write a macro that captures the exception and passes it back out of the go block to the code where it is used.

5 min

4. Thread pool

When work tasks are being produced, we often want to handle them in a thread pool. This is exceedingly easy in core.async. Channels are queues, and we can use the core.async/thread macro to create threads easily. If each loops through the tasks it gets from the queue, the work can be done in parallel.

8 min

5. Backpressure

Backpressure is incredibly important in a distrubuted environment. Our servers can face more load than they can handle. Web requests are cheaper to produce than to respond to. The best practice when faced with more requests than you can handle is to immediately respond that you can't handle it so that you can continue processing the requests you've already accepted.

3 min

6. Kill switch

On the JVM, there is no way to stop a thread from the outside. The same is true of core.async go processes. The only way to stop it is from the inside. A common way to kill multiple threads/go processes is to make a channel signal that they should die.

4 min

7. Poison pill

If you need finer control over which workers die, you could implement the poison pill pattern. In this one, a special value is considered to be a signal to die.

2 min

8. Start latch

When you need many go blocks to start work at very close to the same time, you can use a start latch. This pattern preps go blocks and signals them to start with the same channel.

6 min

9. Token bucket

Rate limiting is kind of hard to implement, especially across multiple threads. But in core.async, it's not hard at all. In this lesson, we implement a simple rate limiting algorithm called a token bucket.

8 min

10. Refining values over time

core.async lets us do things to play with time that we otherwise would have a lot of trouble with. One thing we might want is to request a value that is always current.

Footer CTA

Level up your Clojure skills

The PurelyFunctional.tv Newsletter is a weekly email that helps you improve your Clojure skills through challenges, tips, and news.

Enter your email address to receive emails about Clojure and Functional Programming. These include the weekly newsletter and occasional offers. You can unsubscribe any time.

Copyright © 2021 LispCast and Eric Normand