Skip to content
Daniel Pepper edited this page Mar 7, 2023 · 4 revisions

Mutex

Synchronize work across systems using a mutex mixin.

class Counter
  include Berater::Mutex

  def incr
    synchronize { ... }
  end

  def incr_key(key)
    synchronize(key) { ... }
  end
end

Riddle

What's the difference between a rate limiter and a concurrency limiter? Can you build one with the other?

Both enforce limits, but differ with respect to time and memory. A rate limiter can be implemented using a concurrency limiter, by allowing every lock to timeout rather than be released. It wastes memory, but is functionally equivalent. A concurrency limiter can nearly be implemented using a rate limiter, by decrementing the used capacity when a lock is released. The order of locks, however, is lost and thus a timeout will not properly function.

An example is worth a thousand words :)

DSL

Experimental...

using Berater::DSL

Berater(:key) { 1.per second } do
  ...
end

Berater(:key) { 3.at_once } do
  ...
end
Clone this wiki locally