Ejemplo n.º 1
0
        public void ReleaseHttpClient(HttpClientBag bag)
        {
            lock (_lock)
                FreeHttpClients.Enqueue(bag);

            if (GetHttpClientQueue.Count > 0)
            {
                ManualResetEvent wait = null;
                lock (_lock_GetHttpClientQueue)
                    if (GetHttpClientQueue.Count > 0)
                    {
                        wait = GetHttpClientQueue.Dequeue();
                    }
                if (wait != null)
                {
                    wait.Set();
                }
            }
        }
Ejemplo n.º 2
0
        public HttpClientBag GetHttpClient()
        {
            HttpClientBag bag = null;

            if (FreeHttpClients.Count > 0)
            {
                lock (_lock)
                    if (FreeHttpClients.Count > 0)
                    {
                        bag = FreeHttpClients.Dequeue();
                    }
            }
            if (bag == null && AllHttpClients.Count < MaxPoolSize)
            {
                lock (_lock)
                    if (AllHttpClients.Count < MaxPoolSize)
                    {
                        bag = new HttpClientBag();
                        AllHttpClients.Add(bag);
                    }
                if (bag != null)
                {
                    bag.Pool       = this;
                    bag.HttpClient = new HttpClient();
                }
            }
            if (bag == null)
            {
                ManualResetEvent wait = new ManualResetEvent(false);
                lock (_lock_GetHttpClientQueue)
                    GetHttpClientQueue.Enqueue(wait);
                if (wait.WaitOne(TimeSpan.FromSeconds(10)))
                {
                    return(GetHttpClient());
                }
                return(null);
            }
            bag.ThreadId   = Thread.CurrentThread.ManagedThreadId;
            bag.LastActive = DateTime.Now;
            Interlocked.Increment(ref bag.UseSum);
            return(bag);
        }