Playing around with this concept myself, I'd like to suggest the following implementation:
def xfetch_early?(ttl:, compute_time:, beta: 1.0)
Kernel.rand < Math::E ** -(ttl.to_f / compute_time / beta)
end
def original_xfetch_api(expiry, delta:, beta: 1.0)
# original: Time.now - (delta * beta * Math.log(rand)) >= expiry
xfetch_early?(expiry - Time.now, compute_time: delta, beta: beta)
end
- implement using the algorithm's inverse, which is more declarative.
- the instant probability over
ttl, e.g. e^(-ttl/5.0) becomes easy to graph and understand
xfetch_early? is static/pure, having no dependency on Time
- it's time-unit agnostic, use secs, msecs, minutes, etc for
ttl and compute_time
Playing around with this concept myself, I'd like to suggest the following implementation:
ttl, e.g.e^(-ttl/5.0)becomes easy to graph and understandxfetch_early?is static/pure, having no dependency onTimettlandcompute_time