|
1 |
| -using Microsoft.AspNetCore; |
| 1 | +using KestrelRedisServer; |
2 | 2 | using Microsoft.AspNetCore.Connections;
|
3 |
| -using Microsoft.AspNetCore.Hosting; |
| 3 | +using StackExchange.Redis.Server; |
4 | 4 |
|
5 |
| -namespace KestrelRedisServer |
| 5 | +var server = new MemoryCacheRedisServer(); |
| 6 | + |
| 7 | +var builder = WebApplication.CreateBuilder(args); |
| 8 | +builder.Services.AddSingleton<RespServer>(server); |
| 9 | +builder.WebHost.ConfigureKestrel(options => |
6 | 10 | {
|
7 |
| - public static class Program |
8 |
| - { |
9 |
| - public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run(); |
| 11 | + // HTTP 5000 (test/debug API only) |
| 12 | + options.ListenLocalhost(5000); |
10 | 13 |
|
11 |
| - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => |
12 |
| - WebHost.CreateDefaultBuilder(args) |
13 |
| - .UseKestrel(options => |
14 |
| - { |
15 |
| - // Moved to SocketTransportOptions.UnsafePreferInlineScheduling = true; |
16 |
| - //options.ApplicationSchedulingMode = SchedulingMode.Inline; |
| 14 | + // this is the core of using Kestrel to create a TCP server |
| 15 | + // TCP 6379 |
| 16 | + options.ListenLocalhost(6379, builder => builder.UseConnectionHandler<RedisConnectionHandler>()); |
| 17 | +}); |
17 | 18 |
|
18 |
| - // HTTP 5000 |
19 |
| - options.ListenLocalhost(5000); |
| 19 | +var app = builder.Build(); |
20 | 20 |
|
21 |
| - // TCP 6379 |
22 |
| - options.ListenLocalhost(6379, builder => builder.UseConnectionHandler<RedisConnectionHandler>()); |
23 |
| - }).UseStartup<Startup>(); |
| 21 | +// redis-specific hack - there is a redis command to shutdown the server |
| 22 | +_ = server.Shutdown.ContinueWith(static (t, s) => |
| 23 | +{ |
| 24 | + try |
| 25 | + { // if the resp server is shutdown by a client: stop the kestrel server too |
| 26 | + if (t.Result == RespServer.ShutdownReason.ClientInitiated) |
| 27 | + { |
| 28 | + ((IServiceProvider)s!).GetService<IHostApplicationLifetime>()?.StopApplication(); |
| 29 | + } |
24 | 30 | }
|
25 |
| -} |
| 31 | + catch { /* Don't go boom on shutdown */ } |
| 32 | +}, app.Services); |
| 33 | + |
| 34 | +// add debug route |
| 35 | +app.Run(context => context.Response.WriteAsync(server.GetStats())); |
| 36 | + |
| 37 | +// run the server |
| 38 | +await app.RunAsync(); |
0 commit comments