Class: Routemaster::Dirty::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/routemaster/dirty/filter.rb

Overview

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.

Constant Summary collapse

EXPIRY =
86_400

Instance Method Summary collapse

Constructor Details

#initialize(redis: nil) ⇒ Filter

persists the known state

Parameters:

  • redis (Redis, Redis::Namespace) (defaults to: nil)

    a connection to Redis, used to



17
18
19
20
# File 'lib/routemaster/dirty/filter.rb', line 17

def initialize(redis:nil)
  @redis  = redis || Config.drain_redis
  @expiry = Config.cache_expiry
end

Instance Method Details

#run(payload) ⇒ Object

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.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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