I'm trying to put together a super simple queuing sample based on BookSleeve.
Here's what I have so far for the queue consumer side:
using System;
using System.Text;
using BookSleeve;
static class Program
{
static void Main()
{
using (var redis = new RedisConnection("127.0.0.1"))
{
redis.Open();
while (true)
{
// BRPOP the queue
var t = redis.Lists.BlockingRemoveFirst(0, new[] { "test" }, 0);
t.Wait();
var val = Encoding.UTF8.GetString(t.Result.Item2);
Console.WriteLine("Got: {0}", val);
}
}
}
}
I'm running the following in LINQPad as the producer:
using(var redis = new RedisConnection("localhost"))
{
redis.Open();
foreach(var i in Enumerable.Range(0, 10))
{
// LPUSH
redis.Lists.AddFirst(0, "test", "foo" + i)
// Call wait to keep things in order
.Wait();
}
Thread.Sleep(500); // Let Redis do it's thing
Console.WriteLine("queue length: " + redis.Lists.GetLength(0, "test").Result);
}
I'm getting some really weird results though:
1st run:
Got: foo2
Got: foo5
Got: foo7
Got: foo9
2nd run:
Got: foo1
Got: foo4
Got: foo7
3rd run:
Got: foo0
Got: foo3
Got: foo6
Got: foo8
Also, after every run, LINQPad outputs: queue length: 0
, and running LLEN test
using the actual redis client returns (integer) 0
.
I can't help but feel that I'm missing something here, most likely having to do with the async stuff, but I just can't see it.
My redis version is 2.8.3, BookSleeve version is 1.3.41.
The actual question here is: Why is BookSleeve only returning a subset of the messages that are sent to the redis list?
I simply can't get it to error. Are you absolutey sure you only have 1 consumer? Note that the reason the order is unpredictable is because you are using it as a stack rather than a queue - to get reliable order you should either add to the start and remove from the end, or add to the end and remove from the start. If you add and remove from the start it is random-ish order.
However, it works fine for me. Console output:
I suggest using
redis-cli
inmonitor
mode - for me, I get the following output:Here connection 4957 is one that just removes the key (to ensure a known state); 4958 is the consumer, and 4959 is the producer.
For reference, I'm using the 2.6.12 Windows build in the above test (because I'm on my Windows laptop, and didn't have time to set up a linux VM) - but: I would expect the same on the official linux builds too.
So again: are you sure (via
redis-cli
) that you only have 2 connections involved here?If I change to
AddLast
: