/// <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> /// 回收一个连接 /// </summary> /// <param name="client"></param> public void Push(Tuple <TTransport, T> client, string host, string token) { try { client.Item1.Close(); client.Item1.Dispose(); client = null; } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); } _count--; }
/// <summary> /// 回收一个连接 /// </summary> /// <param name="client"></param> public void Push(Tuple <TTransport, T> client, string host, string token) { lock (_lockPopHelper) { _listPop.Remove(token); } //错误的连接 if (_hashErrorPop.Contains(token)) { ThriftLog.Info("回收错误的连接:" + token); _hashErrorPop.Remove(token); try { client.Item1.Close(); client.Item1.Dispose(); client = null; } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); } _count--; return; } //超过最大空闲 if (_clients.Count() >= _config.ServiceConfig.MaxConnectionsIdle) { ThriftLog.Info($"当前连接数:{_count},超过最大空闲数:{_config.ServiceConfig.MaxConnectionsIdle},如果此条信息过多,请增加最大空闲数"); try { client.Item1.Close(); client.Item1.Dispose(); client = null; } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); } _count--; return; } _clients.Enqueue(new ThriftClient <T>(client, this, host, token)); }
public override async Task process(WatchedEvent watchedEvent) { Console.WriteLine("WatchedEvent:" + watchedEvent.getState().ToString() + ":" + watchedEvent.get_Type().ToString()); if (watchedEvent.getState() == KeeperState.Expired) { ThriftLog.Info(" 重新连接zk"); await _zk.closeAsync(); _zk = null; _serviceConfig = GetServiceConfig(); return; } try { if (watchedEvent.get_Type() == EventType.NodeChildrenChanged) { var data = await _zk.getChildrenAsync(_serviceConfig.ZookeeperConfig.NodeParent, this); var children = data.Children; if (children != null && children.Count > 0) { _serviceConfig.Host = string.Join(",", children); } else { _serviceConfig.Host = _defaultHost; } if (_updateHostDelegate != null) { _updateHostDelegate(); } } } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); _zk = null; _serviceConfig = GetServiceConfig(); ThriftLog.Info(" 重新连接zk2"); } return; }
private bool SetServerConfig(Config.Service service) { try { if (_zk == null) { _zk = new ZooKeeper(service.ZookeeperConfig.Host, service.ZookeeperConfig.SessionTimeout, this); int count = 0; while (_zk.getState() != ZooKeeper.States.CONNECTED) { if (count++ > 50) { throw new Exception("ZooKeeper 连接失败"); } System.Threading.Thread.Sleep(100); } } _defaultHost = service.Host; var children = _zk.getChildrenAsync(service.ZookeeperConfig.NodeParent, this).Result.Children; if (children != null && children.Count > 0) { service.Host = string.Join(",", children); } if (!_firstGetConfig) //首次连接,不需要执行更新方法。 { if (_updateHostDelegate != null) { _updateHostDelegate(); } } return(true); } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); return(false); } }