/// <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> /// 从连接池返回一个可用连接 /// </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> /// get /// </summary> /// <param name="configPath"></param> /// <param name="sectionName"></param> /// <param name="serviceName"></param> /// <returns></returns> static public ThriftClient GetClient(string configPath, string sectionName, string serviceName) { if (string.IsNullOrEmpty(sectionName)) { throw new ArgumentNullException("sectionName"); } if (string.IsNullOrEmpty(serviceName)) { throw new ArgumentNullException("serviceName"); } return(_dic.GetOrAdd(string.Concat(configPath ?? "", "/", sectionName, "/", serviceName), key => new Lazy <Tuple <ThriftClient, object> >(() => ThriftClientFactory.Create(configPath, sectionName, serviceName), LazyThreadSafetyMode.ExecutionAndPublication)).Value.Item1); }