Example #1
0
        private DisposableBeanstalkdClientLabel GetConcumer(IList <string> watchList)
        {
            lock (_pool)
            {
                BeanstalkdClientLabel client = null;
                if (_pool.Count == 0)
                {
                    client = new BeanstalkdClientLabel
                    {
                        Client      = ManagedBeanstalkdClientFactory.Create(_host, _port),
                        CurrentTube = "default",
                        WatchList   = new List <string> {
                            "default"
                        }
                    };
                }
                else
                {
                    var it = _pool.First;

                    while (true)
                    {
                        if (it == null)
                        {
                            break;
                        }
                        if (AreWatchListsEqual(watchList, it.Value.WatchList))
                        {
                            client = it.Value;
                            break;
                        }
                        if (it == _pool.Last)
                        {
                            break;
                        }
                        it = it.Next;
                    }
                    if (client == null)
                    {
                        client = _pool.First.Value;
                    }
                    _pool.Remove(client);
                }

                if (!AreWatchListsEqual(client.WatchList, WatchList))
                {
                    WatchList.ForEach(tube =>
                    {
                        if (!client.WatchList.Contains(tube))
                        {
                            client.Client.Watch(tube);
                        }
                    });
                    client.WatchList.ForEach(tube =>
                    {
                        if (!WatchList.Contains(tube))
                        {
                            client.Client.Ignore(tube);
                        }
                    });
                    client.WatchList = WatchList;
                }
                return(new DisposableBeanstalkdClientLabel(client, _pool));
            }
        }