Commit 78a37102 authored by Marc Gravell's avatar Marc Gravell

experimental kestrel csproj

parent fe830a47
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" />
<ProjectReference Include="..\StackExchange.Redis.Server\StackExchange.Redis.Server.csproj" />
</ItemGroup>
</Project>
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
namespace KestrelRedisServer
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
// TCP 6379
options.ListenLocalhost(8007, builder =>
{
builder.UseConnectionHandler<RedisConnectionHandler>();
});
});
}
}
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.Logging;
using StackExchange.Redis.Server;
namespace KestrelRedisServer
{
public class RedisConnectionHandler : ConnectionHandler
{
private readonly MemoryCacheRedisServer _server;
public RedisConnectionHandler(ILogger<RedisConnectionHandler> logger)
{
_server = new MemoryCacheRedisServer();
}
public override async Task OnConnectedAsync(ConnectionContext connection)
{
var client = _server.AddClient();
try
{
while (true)
{
var read = await connection.Transport.Input.ReadAsync();
var buffer = read.Buffer;
bool makingProgress = false;
while (_server.TryProcessRequest(ref buffer, client, connection.Transport.Output))
{
makingProgress = true;
await connection.Transport.Output.FlushAsync();
}
connection.Transport.Input.AdvanceTo(buffer.Start, buffer.End);
if (!makingProgress && read.IsCompleted) break;
}
}
finally
{
_server.RemoveClient(client);
connection.Transport.Input.Complete();
connection.Transport.Output.Complete();
}
}
}
}
......@@ -199,22 +199,23 @@ private static void StartOnScheduler(PipeScheduler scheduler, Action<object> cal
}
// for extensibility, so that a subclass can get their own client type
// to be used via ListenForConnections
protected virtual RedisClient CreateClient() => new RedisClient();
public virtual RedisClient CreateClient() => new RedisClient();
public int ClientCount
{
get { lock (_clients) { return _clients.Count; } }
}
public int TotalClientCount { get; private set; }
public void AddClient(RedisClient client)
public RedisClient AddClient()
{
if (client == null) throw new ArgumentNullException(nameof(client));
var client = CreateClient();
lock (_clients)
{
ThrowIfShutdown();
_clients.Add(client);
TotalClientCount++;
}
return client;
}
public bool RemoveClient(RedisClient client)
{
......@@ -234,9 +235,8 @@ private async void ListenForConnections(PipeOptions sendOptions, PipeOptions rec
var client = await _listener.AcceptAsync();
SocketConnection.SetRecommendedServerOptions(client);
var pipe = SocketConnection.Create(client, sendOptions, receiveOptions);
var c = CreateClient();
var c = AddClient();
c.LinkedPipe = pipe;
AddClient(c);
StartOnScheduler(receiveOptions.ReaderScheduler, RunClientCallback, c);
}
}
......@@ -410,7 +410,7 @@ public static bool TryParseRequest(ref ReadOnlySequence<byte> buffer, out RedisR
return false;
}
bool TryProcessRequest(ref ReadOnlySequence<byte> buffer, RedisClient client, PipeWriter output)
public bool TryProcessRequest(ref ReadOnlySequence<byte> buffer, RedisClient client, PipeWriter output)
{
if (!buffer.IsEmpty && TryParseRequest(ref buffer, out var request))
{
......
......@@ -79,7 +79,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Failover", "Failover", "{D0
RedisConfigs\Failover\slave-6383.conf = RedisConfigs\Failover\slave-6383.conf
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StackExchange.Redis.Server", "StackExchange.Redis.Server\StackExchange.Redis.Server.csproj", "{8375813E-FBAF-4DA3-A2C7-E4645B39B931}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackExchange.Redis.Server", "StackExchange.Redis.Server\StackExchange.Redis.Server.csproj", "{8375813E-FBAF-4DA3-A2C7-E4645B39B931}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KestrelRedisServer", "KestrelRedisServer\KestrelRedisServer.csproj", "{3DA1EEED-E9FE-43D9-B293-E000CFCCD91A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -123,6 +125,10 @@ Global
{8375813E-FBAF-4DA3-A2C7-E4645B39B931}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8375813E-FBAF-4DA3-A2C7-E4645B39B931}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8375813E-FBAF-4DA3-A2C7-E4645B39B931}.Release|Any CPU.Build.0 = Release|Any CPU
{3DA1EEED-E9FE-43D9-B293-E000CFCCD91A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DA1EEED-E9FE-43D9-B293-E000CFCCD91A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DA1EEED-E9FE-43D9-B293-E000CFCCD91A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DA1EEED-E9FE-43D9-B293-E000CFCCD91A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment