netstandard2.0 support

This commit is contained in:
David Nissimoff 2018-05-10 16:12:36 -07:00
parent aa3de16464
commit b1641e003c
17 changed files with 128 additions and 82 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Ocelot.Configuration.File;
using Ocelot.Infrastructure;
using Ocelot.Infrastructure.Extensions;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Responses;

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Text;
using Ocelot.Infrastructure.Extensions;
namespace Ocelot.Configuration.File
{

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ocelot.Infrastructure.Extensions;
namespace Ocelot.Configuration.File
{

View File

@ -49,7 +49,7 @@ namespace Ocelot.Configuration
if (test != null)
{
var node = (INode)test;
var result = node.Accept(new UpdateFileConfiguration(fileConfiguration));
var result = await node.Accept(new UpdateFileConfiguration(fileConfiguration));
if (result.GetType() == typeof(Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>))
{
return new BadRequestObjectResult("There was a problem. This error message sucks raise an issue in GitHub.");

View File

@ -3,6 +3,7 @@ namespace Ocelot.Headers
using System.Collections.Generic;
using Ocelot.Configuration.Creator;
using Ocelot.Infrastructure;
using Ocelot.Infrastructure.Extensions;
using Ocelot.Logging;
using Ocelot.Middleware;

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ocelot.Infrastructure.Extensions
{
/// <summary>
/// Trivial implementations of methods present in .NET Core 2 but not supported on .NET Standard 2.0.
/// </summary>
internal static class NetCoreSupportExtensions
{
internal static void AppendJoin<T>(this StringBuilder builder, char separator, IEnumerable<T> values)
{
builder.Append(string.Join(separator.ToString(), values));
}
internal static string[] Split(this string input, string separator, StringSplitOptions options = StringSplitOptions.None)
{
return input.Split(new[] { separator }, options);
}
internal static bool StartsWith(this string input, char value)
{
return input.StartsWith(value.ToString());
}
internal static bool EndsWith(this string input, char value)
{
return input.EndsWith(value.ToString());
}
}
}

View File

@ -35,7 +35,7 @@ namespace Ocelot.LoadBalancer.LoadBalancers
{
if (stickySession.Expiry < DateTime.UtcNow)
{
_stored.Remove(stickySession.Key, out _);
_stored.TryRemove(stickySession.Key, out _);
_loadBalancer.Release(stickySession.HostAndPort);
}
}

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
<NETStandardImplicitPackageVersion>2.0.0</NETStandardImplicitPackageVersion>
<NoPackageAnalysis>true</NoPackageAnalysis>
@ -25,14 +25,22 @@
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
<PackageReference Include="Butterfly.Client" Version="0.0.8" />
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8">
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="FluentValidation" Version="7.5.2" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.4.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="2.0.2" />
<PackageReference Include="Microsoft.Data.SQLite" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.0.1">
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.1" />
@ -48,6 +56,6 @@
<PackageReference Include="Polly" Version="5.8.0" />
<PackageReference Include="Pivotal.Discovery.Client" Version="1.1.0" />
<PackageReference Include="IdentityServer4" Version="2.1.3" />
<PackageReference Include="Rafty" Version="0.4.2" />
<PackageReference Include="Rafty" Version="0.4.3" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Rafty.FiniteStateMachine;
using Rafty.Infrastructure;
@ -17,7 +18,7 @@ namespace Ocelot.Raft
_id = nodeId.Id.Replace("/","").Replace(":","");
}
public void Handle(LogEntry log)
public Task Handle(LogEntry log)
{
try
{
@ -28,6 +29,8 @@ namespace Ocelot.Raft
{
Console.WriteLine(exception);
}
return Task.CompletedTask;
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Ocelot.Configuration;
using Ocelot.Middleware;
@ -35,7 +36,7 @@ namespace Ocelot.Raft
public string Id {get; private set;}
public RequestVoteResponse Request(RequestVote requestVote)
public async Task<RequestVoteResponse> Request(RequestVote requestVote)
{
if(_token == null)
{
@ -48,7 +49,7 @@ namespace Ocelot.Raft
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/requestvote", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<RequestVoteResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), _jsonSerializerSettings);
return JsonConvert.DeserializeObject<RequestVoteResponse>(await response.Content.ReadAsStringAsync(), _jsonSerializerSettings);
}
else
{
@ -56,7 +57,7 @@ namespace Ocelot.Raft
}
}
public AppendEntriesResponse Request(AppendEntries appendEntries)
public async Task<AppendEntriesResponse> Request(AppendEntries appendEntries)
{
try
{
@ -71,7 +72,7 @@ namespace Ocelot.Raft
var response = _httpClient.PostAsync($"{_hostAndPort}/administration/raft/appendEntries", content).GetAwaiter().GetResult();
if(response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<AppendEntriesResponse>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(),_jsonSerializerSettings);
return JsonConvert.DeserializeObject<AppendEntriesResponse>(await response.Content.ReadAsStringAsync(), _jsonSerializerSettings);
}
else
{
@ -85,7 +86,7 @@ namespace Ocelot.Raft
}
}
public Response<T> Request<T>(T command)
public async Task<Response<T>> Request<T>(T command)
where T : ICommand
{
Console.WriteLine("SENDING REQUEST....");
@ -107,7 +108,7 @@ namespace Ocelot.Raft
else
{
Console.WriteLine("REQUEST NOT OK....");
return new ErrorResponse<T>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult(), command);
return new ErrorResponse<T>(await response.Content.ReadAsStringAsync(), command);
}
}

View File

@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Ocelot.Configuration.Setter;
using Rafty.FiniteStateMachine;
using Rafty.Log;
@ -14,12 +15,12 @@ namespace Ocelot.Raft
_setter = setter;
}
public void Handle(LogEntry log)
public async Task Handle(LogEntry log)
{
//todo - handle an error
//hack it to just cast as at the moment we know this is the only command :P
var hack = (UpdateFileConfiguration)log.CommandData;
_setter.Set(hack.Configuration).GetAwaiter().GetResult();
await _setter.Set(hack.Configuration);
}
}
}

View File

@ -5,6 +5,7 @@ using Newtonsoft.Json;
using System;
using Rafty.Infrastructure;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Ocelot.Raft
{
@ -40,89 +41,80 @@ namespace Ocelot.Raft
}
}
public int LastLogIndex
public Task<int> LastLogIndex()
{
get
lock(_lock)
{
lock(_lock)
var result = 1;
using(var connection = new SqliteConnection($"Data Source={_path};"))
{
var result = 1;
using(var connection = new SqliteConnection($"Data Source={_path};"))
connection.Open();
var sql = @"select id from logs order by id desc limit 1";
using(var command = new SqliteCommand(sql, connection))
{
connection.Open();
var sql = @"select id from logs order by id desc limit 1";
using(var command = new SqliteCommand(sql, connection))
var index = Convert.ToInt32(command.ExecuteScalar());
if(index > result)
{
var index = Convert.ToInt32(command.ExecuteScalar());
if(index > result)
{
result = index;
}
result = index;
}
}
return result;
}
return Task.FromResult(result);
}
}
public long LastLogTerm
public Task<long> LastLogTerm ()
{
get
lock(_lock)
{
lock(_lock)
long result = 0;
using(var connection = new SqliteConnection($"Data Source={_path};"))
{
long result = 0;
using(var connection = new SqliteConnection($"Data Source={_path};"))
connection.Open();
var sql = @"select data from logs order by id desc limit 1";
using(var command = new SqliteCommand(sql, connection))
{
connection.Open();
var sql = @"select data from logs order by id desc limit 1";
using(var command = new SqliteCommand(sql, connection))
var data = Convert.ToString(command.ExecuteScalar());
var jsonSerializerSettings = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All
};
var log = JsonConvert.DeserializeObject<LogEntry>(data, jsonSerializerSettings);
if(log != null && log.Term > result)
{
var data = Convert.ToString(command.ExecuteScalar());
var jsonSerializerSettings = new JsonSerializerSettings() {
TypeNameHandling = TypeNameHandling.All
};
var log = JsonConvert.DeserializeObject<LogEntry>(data, jsonSerializerSettings);
if(log != null && log.Term > result)
{
result = log.Term;
}
result = log.Term;
}
}
return result;
}
return Task.FromResult(result);
}
}
public int Count
public Task<int> Count ()
{
get
lock(_lock)
{
lock(_lock)
var result = 0;
using(var connection = new SqliteConnection($"Data Source={_path};"))
{
var result = 0;
using(var connection = new SqliteConnection($"Data Source={_path};"))
connection.Open();
var sql = @"select count(id) from logs";
using(var command = new SqliteCommand(sql, connection))
{
connection.Open();
var sql = @"select count(id) from logs";
using(var command = new SqliteCommand(sql, connection))
var index = Convert.ToInt32(command.ExecuteScalar());
if(index > result)
{
var index = Convert.ToInt32(command.ExecuteScalar());
if(index > result)
{
result = index;
}
result = index;
}
}
return result;
}
return Task.FromResult(result);
}
}
public int Apply(LogEntry log)
public Task<int> Apply(LogEntry log)
{
lock(_lock)
{
@ -145,13 +137,13 @@ namespace Ocelot.Raft
using(var command = new SqliteCommand(sql, connection))
{
var result = command.ExecuteScalar();
return Convert.ToInt32(result);
return Task.FromResult(Convert.ToInt32(result));
}
}
}
}
public void DeleteConflictsFromThisLog(int index, LogEntry logEntry)
public Task DeleteConflictsFromThisLog(int index, LogEntry logEntry)
{
lock(_lock)
{
@ -174,15 +166,17 @@ namespace Ocelot.Raft
var deleteSql = $"delete from logs where id >= {index};";
using(var deleteCommand = new SqliteCommand(deleteSql, connection))
{
var result = deleteCommand.ExecuteNonQuery();
var result = deleteCommand.ExecuteNonQuery();
}
}
}
}
}
return Task.CompletedTask;
}
public LogEntry Get(int index)
public Task<LogEntry> Get(int index)
{
lock(_lock)
{
@ -199,13 +193,13 @@ namespace Ocelot.Raft
TypeNameHandling = TypeNameHandling.All
};
var log = JsonConvert.DeserializeObject<LogEntry>(data, jsonSerializerSettings);
return log;
return Task.FromResult(log);
}
}
}
}
public System.Collections.Generic.List<(int index, LogEntry logEntry)> GetFrom(int index)
public Task<List<(int index, LogEntry logEntry)>> GetFrom(int index)
{
lock(_lock)
{
@ -235,11 +229,11 @@ namespace Ocelot.Raft
}
}
return logsToReturn;
return Task.FromResult(logsToReturn);
}
}
public long GetTermAtIndex(int index)
public Task<long> GetTermAtIndex(int index)
{
lock(_lock)
{
@ -264,11 +258,11 @@ namespace Ocelot.Raft
}
}
return result;
return Task.FromResult(result);
}
}
public void Remove(int indexOfCommand)
public Task Remove(int indexOfCommand)
{
lock(_lock)
{
@ -284,6 +278,8 @@ namespace Ocelot.Raft
}
}
}
return Task.CompletedTask;
}
}
}

View File

@ -52,7 +52,7 @@ namespace Ocelot.Requester
if(context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator)
{
httpclientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
httpclientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true;
_logger
.LogWarning($"You have ignored all SSL warnings by using DangerousAcceptAnyServerCertificateValidator for this DownstreamReRoute, UpstreamPathTemplate: {context.DownstreamReRoute.UpstreamPathTemplate}, DownstreamPathTemplate: {context.DownstreamReRoute.DownstreamPathTemplate}");

View File

@ -54,6 +54,7 @@
<PackageReference Include="TestStack.BDDfy" Version="4.3.2" />
<PackageReference Include="Consul" Version="0.7.2.4" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
<PackageReference Include="Rafty" Version="0.4.3" />
</ItemGroup>
</Project>

View File

@ -44,7 +44,7 @@
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="TestStack.BDDfy" Version="4.3.2" />
<PackageReference Include="Consul" Version="0.7.2.4" />
<PackageReference Include="Rafty" Version="0.4.2" />
<PackageReference Include="Rafty" Version="0.4.3" />
<PackageReference Include="Microsoft.Data.SQLite" Version="2.0.1" />
</ItemGroup>
</Project>

View File

@ -126,14 +126,14 @@ namespace Ocelot.UnitTests.Controllers
{
_node
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
.Returns(new Rafty.Concensus.OkResponse<UpdateFileConfiguration>(new UpdateFileConfiguration(new FileConfiguration())));
.ReturnsAsync(new Rafty.Concensus.OkResponse<UpdateFileConfiguration>(new UpdateFileConfiguration(new FileConfiguration())));
}
private void GivenTheNodeReturnsError()
{
_node
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
.Returns(new Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>("error", new UpdateFileConfiguration(new FileConfiguration())));
.ReturnsAsync(new Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>("error", new UpdateFileConfiguration(new FileConfiguration())));
}
private void GivenTheConfigSetterReturns(Response response)

View File

@ -49,10 +49,10 @@
<PackageReference Include="TestStack.BDDfy" Version="4.3.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
<PackageReference Include="Rafty" Version="0.4.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="WebSockets\" />
</ItemGroup>
</Project>