-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
117 lines (101 loc) · 3.77 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.HttpResults;
using Voting.Data;
using Voting.Helpers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Reflection.Metadata.Ecma335;
using System.Numerics;
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureHostOptions(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(5));
builder.Host.UseOrleans((ctx, builder) =>
{
if (ctx.HostingEnvironment.IsDevelopment())
{
// During development time, we don't want to have to deal with
// storage emulators or other dependencies. Just "Hit F5" to run.
builder.UseLocalhostClustering();
builder.AddMemoryGrainStorage("votes");
builder.UseDashboard(options => {
options.Port = 8888;
});
}
else
{
// In Kubernetes, we use environment variables and the pod manifest
//orleansBuilder.UseKubernetesHosting();
// Use Redis for clustering & persistence
//var redisAddress = $"{Environment.GetEnvironmentVariable("REDIS")}:6379";
//orleansBuilder.UseRedisClustering(options => options.ConnectionString = redisAddress);
//orleansBuilder.AddRedisGrainStorage("votes", options => options.ConnectionString = redisAddress);
}
});
// Add services to the container.
//builder.Services.AddSingleton<IHostLifetime>(sp => new DelayedShutdownHostLifetime(
// sp.GetRequiredService<IHostApplicationLifetime>(),
// TimeSpan.FromSeconds(5)
//));
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<PollService>();
builder.Services.AddSingleton<DemoService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Map("/longop/{value}", async Task<Results<StatusCodeHttpResult, Ok<String>>> (int value, CancellationToken ct, [FromServices] IHostApplicationLifetime appLifetime) =>
{
var effectiveCt = ct;
// var effectiveCt = CancellationTokenSource.CreateLinkedTokenSource(ct, appLifetime.ApplicationStopping).Token;
try
{
await Task.Delay(value * 1000, effectiveCt);
} catch (OperationCanceledException)
{
return TypedResults.StatusCode(StatusCodes.Status503ServiceUnavailable);
}
return TypedResults.Ok($"Worked {value} seconds, looks good");
});
app.Map("/longop2/{value}", Results<StatusCodeHttpResult, Ok<String>> (int value, CancellationToken ct, [FromServices] IHostApplicationLifetime appLifetime) =>
{
var effectiveCt = ct;
// var effectiveCt = CancellationTokenSource.CreateLinkedTokenSource(ct, appLifetime.ApplicationStopping).Token;
DateTimeOffset deadline = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(value);
var rng = new Random();
byte[] bytes = new byte[4];
rng.NextBytes(bytes);
var v = new BigInteger(bytes);
try
{
while (DateTimeOffset.UtcNow < deadline)
{
for (int i = 0; i < 10_000; i++)
{
if (i % 2 == 0)
{
v /= 2;
} else
{
v = v * 3 + 1;
}
}
}
}
catch (OperationCanceledException)
{
return TypedResults.StatusCode(StatusCodes.Status503ServiceUnavailable);
}
return TypedResults.Ok($"Worked {value} seconds, final value is {v}");
});
app.Map("/shortop", Ok<String> () => {
return TypedResults.Ok("Short, but sweeet success");
});
app.Run();