4

我正在尝试基于 BookSleeve组合一个超级简单的排队示例。

到目前为止,这是队列消费者方面的内容:

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);
            }
        }
    }
}

我作为生产者在LINQPad中运行以下命令:

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);
}

我得到了一些非常奇怪的结果:

第一次运行:

Got: foo2
Got: foo5
Got: foo7
Got: foo9

第二次运行:

Got: foo1
Got: foo4
Got: foo7

第三次运行:

Got: foo0
Got: foo3
Got: foo6
Got: foo8

此外,每次运行后,LINQPad 都会输出:queue length: 0,并且LLEN test使用实际的 redis 客户端运行会返回(integer) 0

我不禁觉得我在这里遗漏了一些东西,很可能与异步的东西有关,但我就是看不到。

我的redis版本是2.8.3,BookSleeve版本是1.3.41。

这里的实际问题是:为什么 BookSleeve 只返回发送到 redis 列表的消息的子集?

4

1 回答 1

2

我根本无法让它出错。您确定您只有 1 个消费者吗?请注意,顺序不可预测的原因是因为您将其用作堆栈而不是队列 - 要获得可靠的顺序,您应该添加到开头并从末尾删除,或者添加到末尾并从开头删除。如果您从一开始就添加和删除它是随机顺序。

但是,它对我来说很好。控制台输出:

Got: foo0
Got: foo1
Got: foo3
Got: foo5
Got: foo6
Got: foo8
Got: foo9
Got: foo7
Got: foo4
Got: foo2
queue length: 0

我建议redis-climonitor模式下使用 - 对我来说,我得到以下输出:

redis 127.0.0.1:6379> monitor
OK
1389454168.068869 [0 127.0.0.1:4957] "INFO"
1389454168.068869 [0 127.0.0.1:4957] "CONFIG" "GET" "timeout"
1389454168.068869 [0 127.0.0.1:4957] "DEL" "test"
1389454168.129869 [0 127.0.0.1:4958] "INFO"
1389454168.129869 [0 127.0.0.1:4958] "CONFIG" "GET" "timeout"
1389454168.136869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.139872 [0 127.0.0.1:4959] "INFO"
1389454168.139872 [0 127.0.0.1:4959] "CONFIG" "GET" "timeout"
1389454168.139872 [0 127.0.0.1:4959] "LPUSH" "test" "foo0"
1389454168.142869 [0 127.0.0.1:4959] "LPUSH" "test" "foo1"
1389454168.142869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.142869 [0 127.0.0.1:4959] "LPUSH" "test" "foo2"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo3"
1389454168.143871 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo4"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo5"
1389454168.144870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.144870 [0 127.0.0.1:4959] "LPUSH" "test" "foo6"
1389454168.144870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.144870 [0 127.0.0.1:4959] "LPUSH" "test" "foo7"
1389454168.145869 [0 127.0.0.1:4959] "LPUSH" "test" "foo8"
1389454168.145869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.145869 [0 127.0.0.1:4959] "LPUSH" "test" "foo9"
1389454168.145869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.146869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.146869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.147870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.147870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.648594 [0 127.0.0.1:4959] "LLEN" "test"

这里的连接 4957 只是删除密钥(以确保已知状态);4958 是消费者,4959 是生产者。

作为参考,我在上述测试中使用的是2.6.12 Windows 版本(因为我在我的 Windows 笔记本电脑上,没有时间设置 linux VM) - 但是:我希望在官方的 linux 也构建了。

再说一遍:您确定(通过redis-cli)您这里只涉及 2 个连接吗?

如果我更改为AddLast

Got: foo0
Got: foo1
Got: foo2
Got: foo3
Got: foo4
Got: foo5
Got: foo6
Got: foo7
Got: foo8
Got: foo9
queue length: 0
于 2014-01-11T15:34:51.813 回答