Skip to content

Alternative Algorithm Implementation #1

@Kache

Description

@Kache

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
  1. implement using the algorithm's inverse, which is more declarative.
  2. the instant probability over ttl, e.g. e^(-ttl/5.0) becomes easy to graph and understand
  3. xfetch_early? is static/pure, having no dependency on Time
  4. it's time-unit agnostic, use secs, msecs, minutes, etc for ttl and compute_time

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions