Skip to content

Instantly share code, notes, and snippets.

@felixyz
Last active December 18, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixyz/5771982 to your computer and use it in GitHub Desktop.
Save felixyz/5771982 to your computer and use it in GitHub Desktop.
This illustrates celluloid-redis issue #9: https://github.com/celluloid/celluloid-redis/issues/9 A repo containing the below code and branches with variations that variously provoke the same bug or not can be found here: https://github.com/Felixyz/celluloid-redis-bug
require 'thread'
require 'celluloid/io'
require 'celluloid/redis'
KEY = "magic_key"
VALUE = {a: 1, b:2}
class RedisActor
include Celluloid::IO
def initialize(queue)
@queue = queue
connect_redis
set_hash
end
def connect_redis
options = {
:host => 'localhost',
:port => 6379,
:password => nil
}
options[:driver] = :celluloid
@redis = ::Redis.new(options)
end
def set_hash
@redis.hmset(KEY, *VALUE)
end
def get_hash
@redis.hgetall(KEY)
end
def run_one
@queue << get_hash
end
def run_ten
10.times { run_one }
end
end
queue = Queue.new
actor = RedisActor.new(queue)
puts "RUN 10 TIMES IN ONE FIBER"
actor.async.run_ten
10.times { puts queue.pop }
puts "RUN 10 TIMES IN 10 SEPARATE FIBERS"
10.times { actor.async.run_one }
10.times { puts queue.pop }
$ ruby ./celluloid_redis_bug.rb
RUN 10 TIMES IN ONE FIBER
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
RUN 10 TIMES IN 10 SEPARATE FIBERS
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
<the actor waits forever for fibers to resume>
$ redis-cli monitor
OK
1371110619.433504 [0 127.0.0.1:64267] "hmset" "magic_key" "a" "1" "b" "2"
1371110619.437558 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.439829 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.442623 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.444574 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.446475 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.448381 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.450422 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.452266 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.454113 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110619.456022 [0 127.0.0.1:64267] "hgetall" "magic_key"
<the first round of ten fetches in one fiber finished successfuly>
1371110621.461116 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110621.464132 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110621.467759 [0 127.0.0.1:64267] "hgetall" "magic_key"
1371110621.471275 [0 127.0.0.1:64267] "hgetall" "magic_key"
$ ruby ./celluloid_redis_bug.rb
RUN 10 TIMES IN ONE FIBER
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
RUN 10 TIMES IN 10 SEPARATE FIBERS
{"a"=>"1", "b"=>"2"}
{"a"=>"1", "b"=>"2"}
<the actor waits forever for fibers to resume>
$ redis-cli monitor
OK
1371110766.280246 [0 127.0.0.1:64364] "hmset" "magic_key" "a" "1" "b" "2"
1371110766.280754 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.281429 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.281681 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.281901 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.282135 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.282365 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.282618 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.282826 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.283090 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110766.283297 [0 127.0.0.1:64364] "hgetall" "magic_key"
<the first round of ten fetches in one fiber finished successfuly>
1371110768.284922 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285266 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285331 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285417 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285501 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285584 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285654 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285730 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285811 [0 127.0.0.1:64364] "hgetall" "magic_key"
1371110768.285914 [0 127.0.0.1:64364] "hgetall" "magic_key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment