mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
Merge pull request #82 from geffzhang/HttpClientPerformance
Http client performance looks good!
This commit is contained in:
commit
ab9c8e30b2
@ -22,17 +22,7 @@ namespace Ocelot.Requester
|
|||||||
|
|
||||||
public async Task<Response<HttpResponseMessage>> GetResponse(Request.Request request)
|
public async Task<Response<HttpResponseMessage>> GetResponse(Request.Request request)
|
||||||
{
|
{
|
||||||
var builder = new HttpClientBuilder();
|
IHttpClient httpClient = GetHttpClient(request);
|
||||||
|
|
||||||
var cacheKey = GetCacheKey(request, builder);
|
|
||||||
|
|
||||||
var httpClient = _cacheHandlers.Get(cacheKey);
|
|
||||||
if (httpClient == null)
|
|
||||||
{
|
|
||||||
httpClient = builder.Create();
|
|
||||||
_cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(6));
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await httpClient.SendAsync(request.HttpRequestMessage);
|
var response = await httpClient.SendAsync(request.HttpRequestMessage);
|
||||||
@ -55,6 +45,19 @@ namespace Ocelot.Requester
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IHttpClient GetHttpClient(Request.Request request)
|
||||||
|
{
|
||||||
|
var builder = new HttpClientBuilder();
|
||||||
|
var cacheKey = GetCacheKey(request, builder);
|
||||||
|
var httpClient = _cacheHandlers.Get(cacheKey);
|
||||||
|
if (httpClient == null)
|
||||||
|
{
|
||||||
|
httpClient = builder.Create();
|
||||||
|
}
|
||||||
|
_cacheHandlers.Set(cacheKey, httpClient, TimeSpan.FromHours(6));
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
private string GetCacheKey(Request.Request request, IHttpClientBuilder builder)
|
private string GetCacheKey(Request.Request request, IHttpClientBuilder builder)
|
||||||
{
|
{
|
||||||
string baseUrl = $"{request.HttpRequestMessage.RequestUri.Scheme}://{request.HttpRequestMessage.RequestUri.Authority}";
|
string baseUrl = $"{request.HttpRequestMessage.RequestUri.Scheme}://{request.HttpRequestMessage.RequestUri.Authority}";
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
@ -9,38 +10,44 @@ namespace Ocelot.Requester
|
|||||||
{
|
{
|
||||||
public class MemoryHttpClientCache : IHttpClientCache
|
public class MemoryHttpClientCache : IHttpClientCache
|
||||||
{
|
{
|
||||||
private readonly IMemoryCache _memoryCache;
|
private readonly ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>> _httpClientsCache = new ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>>();
|
||||||
|
|
||||||
public MemoryHttpClientCache(IMemoryCache memoryCache)
|
|
||||||
{
|
|
||||||
_memoryCache = memoryCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Set(string id, IHttpClient client, TimeSpan expirationTime)
|
public void Set(string id, IHttpClient client, TimeSpan expirationTime)
|
||||||
{
|
{
|
||||||
_memoryCache.Set(id, client, new MemoryCacheEntryOptions().SetAbsoluteExpiration(expirationTime));
|
ConcurrentQueue<IHttpClient> connectionQueue;
|
||||||
|
if (_httpClientsCache.TryGetValue(id, out connectionQueue))
|
||||||
|
{
|
||||||
|
connectionQueue.Enqueue(client);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connectionQueue = new ConcurrentQueue<IHttpClient>();
|
||||||
|
connectionQueue.Enqueue(client);
|
||||||
|
_httpClientsCache.TryAdd(id, connectionQueue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Exists(string id)
|
public bool Exists(string id)
|
||||||
{
|
{
|
||||||
IHttpClient counter;
|
ConcurrentQueue<IHttpClient> connectionQueue;
|
||||||
return _memoryCache.TryGetValue(id, out counter);
|
return _httpClientsCache.TryGetValue(id, out connectionQueue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IHttpClient Get(string id)
|
public IHttpClient Get(string id)
|
||||||
{
|
{
|
||||||
IHttpClient counter;
|
IHttpClient client= null;
|
||||||
if (_memoryCache.TryGetValue(id, out counter))
|
ConcurrentQueue<IHttpClient> connectionQueue;
|
||||||
|
if (_httpClientsCache.TryGetValue(id, out connectionQueue))
|
||||||
{
|
{
|
||||||
return counter;
|
connectionQueue.TryDequeue(out client);
|
||||||
}
|
}
|
||||||
|
return client;
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(string id)
|
public void Remove(string id)
|
||||||
{
|
{
|
||||||
_memoryCache.Remove(id);
|
ConcurrentQueue<IHttpClient> connectionQueue;
|
||||||
|
_httpClientsCache.TryRemove(id, out connectionQueue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user