示例#1
0
        public override IServiceProvider Bootstrap()
        {
            var container = new Container();

            container.Options.AllowOverridingRegistrations = true;

            container.Register <IAmADependency, MyDependency>();
            var muxer = ConnectionMultiplexer.Connect("localhost");

            container.RegisterSingleton(muxer);

            var q1 = new RedisQueue <PingRequest>(muxer);
            var q2 = new RedisQueue <PingRequest>(muxer);

            container.RegisterSingleton <IQueue <PingRequest> >(() => q2);

            Task.Run(async() => {
                var startDate = DateTime.Now;
                while (startDate.AddSeconds(30) > DateTime.Now)
                {
                    Console.WriteLine("Enqueueing ping.");
                    await q1.EnqueueAsync(new PingRequest {
                        Data = "Hi"
                    }).AnyContext();
                    await Task.Delay(RandomData.GetInt(100, 1000)).AnyContext();
                }
            }).AnyContext();

            return(container);
        }
示例#2
0
        public async void Start1()
        {
            IConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("47.94.140.80:6379");

            //ISubscriber subscriber = connectionMultiplexer.GetSubscriber();


            IQueue <SimpleWorkItem> queue = new RedisQueue <SimpleWorkItem>(new RedisQueueOptions <SimpleWorkItem>()
            {
                ConnectionMultiplexer = (ConnectionMultiplexer)connectionMultiplexer
            });//Queue<SimpleWorkItem>(new InMemoryQueueOptions<SimpleWorkItem>());

            await queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            });

            //var workItem = await queue.DequeueAsync();

            //Console.WriteLine($"queue:{workItem.Value.Data}");
        }
示例#3
0
        public async void Start1()
        {
            IConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("47.94.140.80:6379");

            //ISubscriber subscriber = connectionMultiplexer.GetSubscriber();


            IQueue <SimpleWorkItem> queue = new RedisQueue <SimpleWorkItem>(new RedisQueueOptions <SimpleWorkItem>()
            {
                ConnectionMultiplexer = (ConnectionMultiplexer)connectionMultiplexer
            });//Queue<SimpleWorkItem>(new InMemoryQueueOptions<SimpleWorkItem>());

            for (int i = 0; i < 100; i++)
            {
                await queue.EnqueueAsync(new SimpleWorkItem
                {
                    Data = $"Hello   {i}       " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
                });
            }
        }
示例#4
0
        public override IServiceProvider Bootstrap() {
            var container = new Container();
            container.Options.AllowOverridingRegistrations = true;

            container.Register<IAmADependency, MyDependency>();
            var muxer = ConnectionMultiplexer.Connect("localhost");
            container.RegisterSingleton(muxer);

            var q1 = new RedisQueue<PingRequest>(muxer);
            var q2 = new RedisQueue<PingRequest>(muxer);
            container.RegisterSingleton<IQueue<PingRequest>>(() => q2);

            Task.Run(async () => {
                var startDate = DateTime.Now;
                while (startDate.AddSeconds(30) > DateTime.Now) {
                    Console.WriteLine("Enqueueing ping.");
                    await q1.EnqueueAsync(new PingRequest { Data = "Hi" }).AnyContext();
                    await Task.Delay(RandomData.GetInt(100, 1000)).AnyContext();
                }
            }).AnyContext();

            return container;
        }
        public async Task DatabaseTimeoutDuringDequeueHandledCorectly_Issue64()
        {
            // not using GetQueue() here because I need to change the ops timeout in the redis connection string
            const int OPS_TIMEOUT_MS   = 100;
            string    connectionString = Configuration.GetConnectionString("RedisConnectionString") + $",syncTimeout={OPS_TIMEOUT_MS},asyncTimeout={OPS_TIMEOUT_MS}";;
            var       muxer            = await ConnectionMultiplexer.ConnectAsync(connectionString);

            const string QUEUE_NAME = "Test";
            var          queue      = new RedisQueue <SimpleWorkItem>(o => o
                                                                      .ConnectionMultiplexer(muxer)
                                                                      .LoggerFactory(Log)
                                                                      .Name(QUEUE_NAME)
                                                                      .RunMaintenanceTasks(false)
                                                                      );

            using (queue) {
                await queue.DeleteQueueAsync();

                // enqueue item to queue, no reader yet
                await queue.EnqueueAsync(new SimpleWorkItem());

                // create database, we want to cause delay in redis to reproduce the issue
                var database = muxer.GetDatabase();

                // sync / async ops timeout is not working as described: https://stackexchange.github.io/StackExchange.Redis/Configuration
                // it should have timed out after 100 ms but it actually takes a lot more time to time out so we have to use longer delay until this issue is resolved
                // value can be up to 1,000,000 - 1
                //const int DELAY_TIME_USEC = 200000; // 200 msec
                //string databaseDelayScript = $"local usecnow = tonumber(redis.call(\"time\")[2]); while ((((tonumber(redis.call(\"time\")[2]) - usecnow) + 1000000) % 1000000) < {DELAY_TIME_USEC}) do end";

                const int DELAY_TIME_SEC      = 5;
                string    databaseDelayScript = $@"
local now = tonumber(redis.call(""time"")[1]); 
while ((((tonumber(redis.call(""time"")[1]) - now))) < {DELAY_TIME_SEC}) " +
                                                "do end";

                // db will be busy for DELAY_TIME_USEC which will cause timeout on the dequeue to follow
                database.ScriptEvaluateAsync(databaseDelayScript);

                var completion = new TaskCompletionSource <bool>();
                await queue.StartWorkingAsync(async (item) => {
                    await item.CompleteAsync();
                    completion.SetResult(true);
                });

                // wait for the databaseDelayScript to finish
                await Task.Delay(DELAY_TIME_SEC * 1000);

                // item should've either time out at some iterations and after databaseDelayScript is done be received
                // or it might have moved to work, in this case we want to make sure the correct keys were created
                var  stopwatch = Stopwatch.StartNew();
                bool success   = false;
                while (stopwatch.Elapsed.TotalSeconds < 10)
                {
                    string workListName = $"q:{QUEUE_NAME}:work";
                    long   workListLen  = await database.ListLengthAsync(new RedisKey(workListName));

                    var item = await database.ListLeftPopAsync(workListName);

                    string dequeuedItemKey       = String.Concat("q:", QUEUE_NAME, ":", item, ":dequeued");
                    bool   dequeuedItemKeyExists = await database.KeyExistsAsync(new RedisKey(dequeuedItemKey));

                    if (workListLen == 1)
                    {
                        Assert.True(dequeuedItemKeyExists);
                        success = true;
                        break;
                    }

                    var timeoutCancellationTokenSource = new CancellationTokenSource();
                    var completedTask = await Task.WhenAny(completion.Task, Task.Delay(TimeSpan.FromMilliseconds(100), timeoutCancellationTokenSource.Token));

                    if (completion.Task == completedTask)
                    {
                        success = true;
                        break;
                    }
                }

                Assert.True(success);
            }
        }