Ejemplo n.º 1
0
        public YouScribeClient(Func <HttpMessageHandler> handlerFactory, string baseUrl,
                               Func <Func <HttpMessageHandler>, IYousScribeHttpClient> baseClientFactory, TimeSpan timeout, ClientsPoolProvider poolProvider = null)
        {
            this.BaseUrl      = baseUrl;
            this.poolProvider = poolProvider ?? ClientsPoolProvider.Default;
            if (baseClientFactory == null)
            {
                this.baseClientFactory = (c) =>
                {
                    var client = c == null ? new HttpClient() : new HttpClient(c());
                    client.Timeout = timeout;

                    return(new YousScribeHttpClient(client));
                };
            }
            else
            {
                this.baseClientFactory = baseClientFactory;
            }
            this.simpleClientFactory = () => this.baseClientFactory(handlerFactory);
            this.clientFactory       = () =>
            {
                var client = this.ReserveClient(handlerFactory);
                if (client == null)
                {
                    var count = this.poolProvider.Clients.Count;
                    lock (this.poolProvider.Clients)
                    {
                        if (this.poolProvider.Clients.Count != count)
                        {
                            client = this.ReserveClient(handlerFactory);
                        }
                        if (client != null)
                        {
                            return(client);
                        }
                        for (var i = 0; i < ClientsPoolProvider.PoolIncrement - 1; i++)
                        {
                            this.poolProvider.Clients.Add(new ConcurentClient()
                            {
                                Reserved = 0, Client = null
                            });
                        }
                        var item = new ConcurentClient()
                        {
                            Reserved = 1, Client = this.simpleClientFactory()
                        };
                        this.poolProvider.Clients.Add(item);
                        return(new DisposableClient(item));
                    }
                }
                return(client);
            };
        }
Ejemplo n.º 2
0
 internal DisposableClient(ConcurentClient client)
 {
     this.client = client;
 }
Ejemplo n.º 3
0
 internal DisposableClient(ConcurentClient client)
 {
     this.client = client;
 }
Ejemplo n.º 4
0
        public YouScribeClient(Func<HttpMessageHandler> handlerFactory, string baseUrl, 
            Func<Func<HttpMessageHandler>, IYousScribeHttpClient> baseClientFactory, TimeSpan timeout, ClientsPoolProvider poolProvider = null)
        {
            this.BaseUrl = baseUrl;
            this.poolProvider = poolProvider ?? ClientsPoolProvider.Default;
            if (baseClientFactory == null)
            {
                this.baseClientFactory = (c) =>
                {
                    var client = c == null ? new HttpClient() : new HttpClient(c());
                    client.Timeout = timeout;

                    return new YousScribeHttpClient(client);
                };
            }
            else
            {
                this.baseClientFactory = baseClientFactory;
            }
            this.simpleClientFactory = () => this.baseClientFactory(handlerFactory);
            this.clientFactory = () =>
            {
                var client = this.ReserveClient(handlerFactory);
                if (client == null)
                {
                    var count = this.poolProvider.Clients.Count;
                    lock (this.poolProvider.Clients)
                    {
                        if (this.poolProvider.Clients.Count != count)
                            client = this.ReserveClient(handlerFactory);
                        if (client != null)
                            return client;
                        for (var i = 0; i < ClientsPoolProvider.PoolIncrement - 1; i++)
                        {
                            this.poolProvider.Clients.Add(new ConcurentClient() { Reserved = 0, Client = null });
                        }
                        var item = new ConcurentClient() { Reserved = 1, Client = this.simpleClientFactory() };
                        this.poolProvider.Clients.Add(item);
                        return new DisposableClient(item);
                    }
                }
                return client;
            };
        }