Feature/test for #441 (#444)

* #441 added a test for this change

* #441 bit of tidying up and fixing of erros
This commit is contained in:
Tom Pallister
2018-07-01 08:56:06 +01:00
committed by GitHub
parent 2832cb1bf4
commit a87bc92c60
8 changed files with 105 additions and 67 deletions

View File

@ -61,8 +61,8 @@
downstreamRoute = new OkResponse<DownstreamRoute>(new DownstreamRoute(new List<PlaceholderNameAndValue>(), reRoute));
_cache.AddOrUpdate(loadBalancerKey, downstreamRoute, (x, y) => downstreamRoute);
_cache.AddOrUpdate(loadBalancerKey, downstreamRoute, (x, y) => downstreamRoute);
return downstreamRoute;
}

View File

@ -20,7 +20,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
public async Task<Response<ServiceHostAndPort>> Lease(DownstreamContext downstreamContext)
{
var services = await _services();
//todo first or default could be null..
if (services == null || services.Count == 0)
{
return new ErrorResponse<ServiceHostAndPort>(new ServicesAreEmptyError("There were no services in NoLoadBalancer"));

View File

@ -99,6 +99,7 @@ namespace Ocelot.Raft
}
}
}
_sempaphore.Release();
return result;
}
@ -120,6 +121,7 @@ namespace Ocelot.Raft
}
}
}
_sempaphore.Release();
return result;
}
@ -135,6 +137,7 @@ namespace Ocelot.Raft
TypeNameHandling = TypeNameHandling.All
};
var data = JsonConvert.SerializeObject(log, jsonSerializerSettings);
//todo - sql injection dont copy this..
var sql = $"insert into logs (data) values ('{data}')";
_logger.LogInformation($"id: {_nodeId.Id}, sql: {sql}");
@ -162,6 +165,7 @@ namespace Ocelot.Raft
using (var connection = new SqliteConnection($"Data Source={_path};"))
{
connection.Open();
//todo - sql injection dont copy this..
var sql = $"select data from logs where id = {index};";
_logger.LogInformation($"id: {_nodeId.Id} sql: {sql}");
@ -188,6 +192,7 @@ namespace Ocelot.Raft
}
}
}
_sempaphore.Release();
}
@ -197,6 +202,7 @@ namespace Ocelot.Raft
using (var connection = new SqliteConnection($"Data Source={_path};"))
{
connection.Open();
//todo - sql injection dont copy this..
var sql = $"select data from logs where id = {index};";
using (var command = new SqliteCommand(sql, connection))
@ -227,6 +233,7 @@ namespace Ocelot.Raft
using (var connection = new SqliteConnection($"Data Source={_path};"))
{
connection.Open();
//todo - sql injection dont copy this..
var sql = $"select data from logs where id = {index}";
using (var command = new SqliteCommand(sql, connection))
@ -251,6 +258,7 @@ namespace Ocelot.Raft
using (var connection = new SqliteConnection($"Data Source={_path};"))
{
connection.Open();
//todo - sql injection dont copy this..
var sql = $"select id, data from logs where id >= {index}";
using (var command = new SqliteCommand(sql, connection))
@ -267,10 +275,10 @@ namespace Ocelot.Raft
};
var log = JsonConvert.DeserializeObject<LogEntry>(data, jsonSerializerSettings);
logsToReturn.Add((id, log));
}
}
}
_sempaphore.Release();
return logsToReturn;
}
@ -283,6 +291,7 @@ namespace Ocelot.Raft
using (var connection = new SqliteConnection($"Data Source={_path};"))
{
connection.Open();
//todo - sql injection dont copy this..
var sql = $"select data from logs where id = {index}";
using (var command = new SqliteCommand(sql, connection))
@ -299,15 +308,18 @@ namespace Ocelot.Raft
}
}
}
_sempaphore.Release();
return result;
}
public async Task Remove(int indexOfCommand)
{
_sempaphore.Wait();
using (var connection = new SqliteConnection($"Data Source={_path};"))
{
connection.Open();
//todo - sql injection dont copy this..
var deleteSql = $"delete from logs where id >= {indexOfCommand};";
_logger.LogInformation($"id: {_nodeId.Id} Remove {deleteSql}");
@ -316,6 +328,7 @@ namespace Ocelot.Raft
var result = await deleteCommand.ExecuteNonQueryAsync();
}
}
_sempaphore.Release();
}
}

View File

@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Ocelot.Requester
namespace Ocelot.Requester
{
using System;
public interface IHttpClientCache
{
bool Exists(string id);
IHttpClient Get(string id);
void Remove(string id);
void Set(string id, IHttpClient handler, TimeSpan expirationTime);
}
}

View File

@ -1,21 +1,20 @@
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Ocelot.Requester
namespace Ocelot.Requester
{
using System;
using System.Collections.Concurrent;
public class MemoryHttpClientCache : IHttpClientCache
{
private readonly ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>> _httpClientsCache = new ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>>();
private readonly ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>> _httpClientsCache;
public MemoryHttpClientCache()
{
_httpClientsCache = new ConcurrentDictionary<string, ConcurrentQueue<IHttpClient>>();
}
public void Set(string id, IHttpClient client, TimeSpan expirationTime)
{
ConcurrentQueue<IHttpClient> connectionQueue;
if (_httpClientsCache.TryGetValue(id, out connectionQueue))
if (_httpClientsCache.TryGetValue(id, out var connectionQueue))
{
connectionQueue.Enqueue(client);
}
@ -27,28 +26,15 @@ namespace Ocelot.Requester
}
}
public bool Exists(string id)
{
ConcurrentQueue<IHttpClient> connectionQueue;
return _httpClientsCache.TryGetValue(id, out connectionQueue);
}
public IHttpClient Get(string id)
{
IHttpClient client= null;
ConcurrentQueue<IHttpClient> connectionQueue;
if (_httpClientsCache.TryGetValue(id, out connectionQueue))
if (_httpClientsCache.TryGetValue(id, out var connectionQueue))
{
connectionQueue.TryDequeue(out client);
}
return client;
}
public void Remove(string id)
{
ConcurrentQueue<IHttpClient> connectionQueue;
_httpClientsCache.TryRemove(id, out connectionQueue);
}
}
}
}