Class: EventMachine::Bucketer::Redis

Inherits:
Object
  • Object
show all
Includes:
Base, Database::Redis
Defined in:
lib/em-bucketer/redis.rb

Constant Summary collapse

BUCKET_THRESHOLD_SIZE_DEFAULT =
1000
BUCKET_MAX_AGE_DEFAULT =
3600

Instance Method Summary collapse

Methods included from Base

#add_item, #empty_bucket, #get_and_empty_bucket, #get_and_remove, #get_bucket, #on_bucket_full, #on_bucket_timeout, #setup

Constructor Details

#initialize(redis_prefix, bucket_threshold_size: BUCKET_THRESHOLD_SIZE_DEFAULT, bucket_max_age: BUCKET_MAX_AGE_DEFAULT) ⇒ Redis

Creates a new redis Bucketer with the requested configurations. NOTE The redis bucketer uses Marshal to store the objects in redis. This puts limitations on the data that cannot be stored in these buckets. For example you cannot store an object that references a proc as an instance variable.

The redis bucketer also sets all timers on startup for buckets already in the redis database. This ensures that even if your app is restarted the previous timers will still get set and you won't ever lose a bucket.

bucket in redis. This is necessary because you may want to have multiple bucketers using one redis instance and you don't want them conflicting. Also this can't just be random because the whole point of the redis bucketer is that you can restart your app and get back the same bucketer without any data loss. after which the on_bucket_full callback is called can remain before the on_bucket_timed_out is called

Parameters:

  • redis_prefix (String)

    The prefix for the

  • bucket_threshold_size (Integer) (defaults to: BUCKET_THRESHOLD_SIZE_DEFAULT)

    the max size of the bucket

  • bucket_max_age (Integer) (defaults to: BUCKET_MAX_AGE_DEFAULT)

    max number of seconds a bucket



39
40
41
42
43
44
45
# File 'lib/em-bucketer/redis.rb', line 39

def initialize(redis_prefix, bucket_threshold_size: BUCKET_THRESHOLD_SIZE_DEFAULT, bucket_max_age: BUCKET_MAX_AGE_DEFAULT)
  @redis = EM::Hiredis.connect
  @redis_prefix = redis_prefix
  setup(bucket_threshold_size, bucket_max_age)
  setup_db
  set_timers
end

Instance Method Details

#set_timersObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/em-bucketer/redis.rb', line 47

def set_timers
  known_buckets.callback do |bucket_ids|
    bucket_ids.each do |bucket_id|
      add_timer_if_first(bucket_id)
    end
  end.errback do |e|
    # I think this is okay since it will only happen when
    # you are initializing the bucketer so it will hopefully
    # bring the issue to your attention on startup. I
    # couldn't actually pass this error back through to
    # anyone so I needed to raise it. It is also a bad
    # exception since it means you are not properly reloading
    # your buckets from redis
    raise e
  end
end