Exemplo n.º 1
0
 /// <summary>
 /// 归还一个连接至连接池
 /// </summary>
 /// <param name="client">连接</param>
 public virtual void ReturnClient(ISerClient client)
 {
     lock (locker)
     {
         //空闲连接数达到上限或者连接版本过期,不再返回线程池,直接销毁
         if (idleCount >= MaxIdle ||
             this.Version != client.Version)
         {
             DestoryClient(client);
         }
         else
         {
             if (ValidateClient(client))
             {
                 //有效连接回归连接池
                 client.Reset();
                 EnqueueClient(client);
             }
             else
             {
                 //无效连接回收
                 DestoryClient(client);
             }
             //发通知信号,连接池有连接变动
             resetEvent.Set();
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 初始化连接,隐藏创建细节
 /// </summary>
 /// <returns>连接</returns>
 protected ISerClient InitializeClient()
 {
     for (var i = 0; i < Hosts.Length; i++)
     {
         try
         {
             var        threadId  = Thread.CurrentThread.ManagedThreadId;
             var        hostIndex = (threadId + i) % Hosts.Length;
             ISerClient client    = CreateClient(Hosts[hostIndex]);
             if (ValidateClient(client))
             {
                 activeCount++;
                 client.Reset();
                 return(client);
             }
         }
         catch
         {
             continue;
         }
     }
     return(null);
 }
Exemplo n.º 3
0
        /// <summary>
        /// 初始化连接,隐藏创建细节
        /// </summary>
        /// <returns>连接</returns>
        protected ISerClient InitializeClient(out Exception err)
        {
            err = null;
            if (Hosts.Length == 0)
            {
                err = new NullReferenceException("没有可用服务节点");
                return(null);
            }
            var hostIndexs = new int[Hosts.Length];
            int j          = 0;

            for (var i = 0; i < Hosts.Length; i++)
            {
                hostIndexs[i] = i;
            }
            for (var i = 0; i < Hosts.Length; i++)
            {
                try
                {
                    j = rand.Next(Hosts.Length);
                    ISerClient client = CreateClient(Hosts[hostIndexs[j]]);
                    if (ValidateClient(client, out err))
                    {
                        activeCount++;
                        client.Reset();
                        return(client);
                    }
                }
                catch (Exception e)
                {
                    hostIndexs[j] = hostIndexs[(j + 1) % Hosts.Length];
                    err           = e;
                    continue;
                }
            }
            return(null);
        }