class Routemaster::Dirty::Filter

Service object, filters an event payload, only include events that reflect an entity state that is _more recent_ than previously received events.

Can be used to Ignore events received out-of-order (e.g. an ‘update` event about en entity received after the `delete` event for that same entity), given Routemaster makes no guarantee of in-order delivery of events.

Constants

EXPIRY

Public Class Methods

new(redis:nil) click to toggle source

@param redis [Redis, Redis::Namespace] a connection to Redis, used to persists the known state

# File lib/routemaster/dirty/filter.rb, line 17
def initialize(redis:nil)
  @redis  = redis || Config.drain_redis
  @expiry = Config.cache_expiry
end

Public Instance Methods

run(payload) click to toggle source

Process a payload, and returns part if this payload containing only the latest event for a given entity.

Events are skipped if they are older than a previously processed event for the same entity.

Order of kept events is not guaranteed to be preserved.

# File lib/routemaster/dirty/filter.rb, line 29
def run(payload)
  events = {} # url -> event

  payload.each do |event|
    known_state = State.get(@redis, event['url'])

    # skip events older than what we already know
    next if known_state.t > event['t']

    new_state = State.new(event['url'], event['t'])

    next if new_state == known_state
    new_state.save(@redis, @expiry)
    events[event['url']] = event
  end

  events.values
end