Skip to content

Commit

Permalink
cold start optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dopoto committed Sep 14, 2024
1 parent 6235ded commit 30e9430
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
39 changes: 5 additions & 34 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,15 @@
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddUserSecrets<EnvironmentSpecificSettings>(optional: true)
.AddEnvironmentVariables();

// Add services to the container.

builder.Services.AddSignalR();

Check warning on line 15 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'Microsoft.Extensions.DependencyInjection.SignalRDependencyInjectionExtensions.AddSignalR(IServiceCollection)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. SignalR does not currently support trimming or native AOT. https://aka.ms/aspnet/trimming

Check warning on line 15 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'Microsoft.Extensions.DependencyInjection.SignalRDependencyInjectionExtensions.AddSignalR(IServiceCollection)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. SignalR does not currently support trimming or native AOT. https://aka.ms/aspnet/trimming
//builder.Services.AddCors();
//builder.Services.AddControllers();
//// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
//builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();

builder.Services.Add(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<GroupService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
// app.UseSwagger();
// app.UseSwaggerUI();
//}

//app.UseHttpsRedirection();

//app.UseAuthorization();

//app.MapControllers();

//app.Run();

//app.UseCors(builder =>
//{
// builder.WithOrigins(origins: environmentSettings.AllowedCorsOrigins ?? []);
// builder.AllowAnyHeader();
// builder.AllowAnyMethod();
//});
// Preload SignalR hubs by making a dummy connection during startup
var hubContext = app.Services.GetService<IHubContext<BeepHub>>();
await hubContext.Clients.All.SendAsync("preload");

Check warning on line 23 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 23 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

app.UseHttpsRedirection();
app.UseRouting();
Expand All @@ -59,11 +32,10 @@
(HttpContext context, GroupService groupService) =>
groupService.GetConnectionsByGroup(context.Request.Query["groupName"])

Check warning on line 33 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'groupName' in 'List<string> GroupService.GetConnectionsByGroup(string groupName)'.

Check warning on line 33 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'groupName' in 'List<string> GroupService.GetConnectionsByGroup(string groupName)'.

Check warning on line 33 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'groupName' in 'List<string> GroupService.GetConnectionsByGroup(string groupName)'.
);

app.MapGet("/healthcheck", () => "1");

app.Run();


public class BeepHub : Hub
{
private readonly GroupService _groupService;
Expand All @@ -73,7 +45,6 @@ public BeepHub(GroupService groupService)
_groupService = groupService;
}


public async Task Play(string freqInKhz, string durationInSeconds, string groupName)
{
Console.WriteLine("Play - freqInKhz=" + freqInKhz + ", durationInSeconds:" + durationInSeconds + ", group:" + groupName);
Expand Down
6 changes: 6 additions & 0 deletions remote-beep-backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<RootNamespace></RootNamespace>
</PropertyGroup>

<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
<TieredCompilation>true</TieredCompilation>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
Expand Down

0 comments on commit 30e9430

Please sign in to comment.