Beispiel #1
0
        /// <summary>
        /// 从连接池返回一个可用连接
        /// </summary>
        /// <returns></returns>
        public ThriftClient <T> Pop()
        {
            ThriftClient <T> client = GetOrCreateConnection(false);

            if (client != null)
            {
                return(client);
            }

            int count = 0;

            while (true)
            {
                System.Threading.Thread.Sleep(10);
                if (count++ <= 3)
                {
                    client = GetOrCreateConnection(false);
                }
                else
                {
                    client = GetOrCreateConnection(true);
                }

                if (client != null)
                {
                    return(client);
                }

                if (count >= _config.ServiceConfig.PoolTimeout / 10) //获取可用连接超时
                {
                    return(null);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="num"></param>
        private void CreateConnections(int num)
        {
            lock (_lockHelper)
            {
                for (int i = 0; i < num; ++i)
                {
                    if (_count >= _config.ServiceConfig.MaxConnectionsNum)
                    {
                        return;
                    }

                    var item = ThriftClientFactory.Create(_config.ServiceConfig, true);
                    if (item == null)
                    {
                        return;
                    }
                    var token  = System.Guid.NewGuid().ToString();
                    var client = new ThriftClient <T>(Tuple.Create(item.Item1, item.Item2 as T), this, item.Item3, token);

                    _clients.Enqueue(client);
                    _count++;
                    ThriftLog.Info("连接池数:" + _count);
                }
            }
        }
 /// <summary>
 /// init
 /// </summary>
 /// <param name="thriftClient"></param>
 /// <param name="config"></param>
 /// <exception cref="ArgumentNullException">thriftClient is null</exception>
 /// <exception cref="ArgumentNullException">config is null</exception>
 public void Init(ThriftClient thriftClient, Config.ServiceConfig config)
 {
     if (thriftClient == null) throw new ArgumentNullException("thriftClient");
     if (config == null) throw new ArgumentNullException("config");
     this._thriftClient = thriftClient;
     this._config = config;
 }
        /// <summary>
        /// create
        /// </summary>
        /// <param name="configPath"></param>
        /// <param name="sectionName"></param>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        static public Tuple<ThriftClient, object> Create(string configPath, string sectionName, string serviceName)
        {
            if (string.IsNullOrEmpty(sectionName))
                throw new ArgumentNullException("sectionName");

            //load config...
            Config.ThriftConfigSection config = null;
            if (string.IsNullOrEmpty(configPath))
                config = ConfigurationManager.GetSection(sectionName) as Config.ThriftConfigSection;
            else
            {
                config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
                {
                    ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configPath)
                }, ConfigurationUserLevel.None).GetSection(sectionName) as Config.ThriftConfigSection;
            }

            foreach (Config.Service service in config.Services)
            {
                if (service.Name != serviceName) continue;

                var client = new ThriftClient(service,
                    service.SocketBufferSize,
                    service.MessageBufferSize,
                    service.SendTimeout,
                    service.ReceiveTimeout);
                client.Start();

                return Tuple.Create(client, Type.GetType(service.Client, true)
                    .GetConstructor(new Type[] { typeof(IThriftClient) })
                    .Invoke(new object[] { client }));
            }
            return null;
        }
Beispiel #5
0
        /// <summary>
        /// 从连接池返回一个可用连接
        /// </summary>
        /// <returns></returns>
        public ThriftClient <T> Pop()
        {
            if (_count >= _config.ServiceConfig.MaxConnectionsNum)
            {
                ThriftLog.Error("连接池达到最大数:" + _count);
                return(null);
            }
            if (_hostCount == 0)
            {
                return(null);
            }

            _config.ServiceConfig.Host = _host[_hostIndex % _hostCount];

            var item = ThriftClientFactory.Create(_config.ServiceConfig, false);

            if (item == null)
            {
                return(null);
            }
            var client = new ThriftClient <T>(Tuple.Create(item.Item1, item.Item2 as T), this, item.Item3, "");

            _count++;
            return(client);
        }
        /// <summary>
        /// init
        /// </summary>
        /// <param name="thriftClient"></param>
        /// <param name="config"></param>
        /// <exception cref="ArgumentNullException">thriftClient is null</exception>
        /// <exception cref="ArgumentNullException">config is null</exception>
        public void Init(ThriftClient thriftClient, Config.ServiceConfig config)
        {
            if (thriftClient == null) throw new ArgumentNullException("thriftClient");
            if (config == null) throw new ArgumentNullException("config");

            this._thriftClient = thriftClient;
            this._config = config;
            this._methods = string.Join(",", Type.GetType(config.Client).GetInterfaces()[0].GetMethods().Select(c => c.Name).ToArray());
        }
Beispiel #7
0
        /// <summary>
        /// create
        /// </summary>
        /// <param name="configPath"></param>
        /// <param name="sectionName"></param>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        static public Tuple <ThriftClient, object> Create(string configPath, string sectionName, string serviceName)
        {
            if (string.IsNullOrEmpty(sectionName))
            {
                throw new ArgumentNullException("sectionName");
            }

            //load config...
            Config.ThriftConfigSection config = null;
            if (string.IsNullOrEmpty(configPath))
            {
                config = ConfigurationManager.GetSection(sectionName) as Config.ThriftConfigSection;
            }
            else
            {
                config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
                {
                    ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configPath)
                }, ConfigurationUserLevel.None).GetSection(sectionName) as Config.ThriftConfigSection;
            }

            foreach (Config.Service service in config.Services)
            {
                if (service.Name != serviceName)
                {
                    continue;
                }

                var client = new ThriftClient(service,
                                              service.SocketBufferSize,
                                              service.MessageBufferSize,
                                              service.SendTimeout,
                                              service.ReceiveTimeout);
                client.Start();

                return(Tuple.Create(client, Type.GetType(service.Client, true)
                                    .GetConstructor(new Type[] { typeof(IThriftClient) })
                                    .Invoke(new object[] { client })));
            }
            return(null);
        }
Beispiel #8
0
        /// <summary>
        /// 更新连接池,删除不可用的连接
        /// </summary>
        public void UpdatePool()
        {
            lock (_lockHelper)
            {
                int count = _clients.Count;
                for (int i = 0; i < count; i++)
                {
                    ThriftClient <T> client = null;
                    if (!_clients.TryDequeue(out client))
                    {
                        break;
                    }

                    if (!_config.ServiceConfig.Host.Contains(client.Host))
                    {
                        ThriftLog.Info("删除不可用的连接:" + client.Host);
                        client.Destroy(); //删除不可用的连接
                    }
                    else
                    {
                        client.Push(); //主动回收
                    }
                }
            }

            //回收连接时进行过滤
            lock (_lockPopHelper)
            {
                _hashErrorPop = new HashSet <string>();
                foreach (var item in _listPop)
                {
                    if (!_config.ServiceConfig.Host.Contains(item.Value))
                    {
                        _hashErrorPop.Add(item.Key);
                    }
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// 返回连接或创建
        /// </summary>
        /// <returns></returns>
        private ThriftClient <T> GetOrCreateConnection(bool create)
        {
            //lock (_lockHelper)
            //{
            //}

            ThriftClient <T> client = null;

            if (_clients.TryDequeue(out client))
            {
                lock (_lockPopHelper)
                {
                    _listPop.Add(client.Token, client.Host);
                    return(client);
                }
            }

            if (create)
            {
                CreateConnections(_config.ServiceConfig.IncrementalConnections);
            }
            return(null);
        }