private NewQmqClient SelectWritableClient(string subject, bool forceRefreshBrokerGroup, bool isDelayMessage)
        {
            var cluster = isDelayMessage ? _brokerGroupService.DelayProducerGetSubjectCluster(subject, forceRefreshBrokerGroup) : _brokerGroupService.ProducerGetSubjectCluster(subject, forceRefreshBrokerGroup);

            if (cluster.BrokerGroups.Count == 0)
            {
                return(null);
            }

            var brokerGroups = cluster.BrokerGroups.Values.Where(BrokerGroup.IsWritable).ToList();

            if (brokerGroups.Count == 0)
            {
                return(null);
            }

            // First of all, random select a client.
            // If random selected client is not writable, then select first writable client.
            var randBrokerGroup = brokerGroups.ElementAt(StaticRandom.NextRand(brokerGroups.Count));
            var randClient      = _clientManager.GetOrCreate(randBrokerGroup);

            if (randClient.Writtable)
            {
                return(randClient);
            }
            return(brokerGroups.Select(brokerGroup => _clientManager.GetOrCreate(brokerGroup))
                   .FirstOrDefault(client => client.Writtable));
        }
        private NewQmqClient CreateOrGetClient(BrokerGroup group)
        {
            var randClient = _clientManager.GetOrCreate(group);

            if (randClient.Writtable)
            {
                return(randClient);
            }

            return(null);
        }
        private NewQmqClient GetBrokerGroupClient(string brokerGroupName, string subject, string group)
        {
            var brokerGroups = _brokerGroupService.ConsumerGetSubjectCluster(subject, group).BrokerGroups;

            if (brokerGroups.Count == 0 || !brokerGroups.ContainsKey(brokerGroupName))
            {
                return(null);
            }

            var brokerGroup = brokerGroups[brokerGroupName];

            if (brokerGroup == null || !BrokerGroup.IsReadable(brokerGroup))
            {
                return(null);
            }

            return(_clientManager.GetOrCreate(brokerGroup));
        }