I'm using this fairly straightforward means to match two live players :
class Seek
def self.create(uuid)
if opponent = REDIS.spop("seeks")
Game.start(uuid, opponent)
else
REDIS.sadd("seeks", uuid)
end
end
def self.remove(uuid)
REDIS.srem("seeks", uuid)
end
end
Then when my game starts, I simply do Seek.create(uuid)
.
There's very little niche problem I get where sometimes two people Seek at the same time. I'm guessing that Redis.spop("seeks")
returns nil
for both players, and converts then adds them both to REDIS.sadd("seeks", uuid)
. And then they both wait indefinitely ( unless of course another player comes along ).
My situation seems like a rather rare case, but I'm curious if my seek.rb
file could be written in a better way to prevent this.