Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize performance when running on WebAssembly by caching assembly payload that needs to be served to new clients #859

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions Oqtane.Server/Controllers/InstallationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Oqtane.Modules;
using Oqtane.Shared;
using Oqtane.Themes;
using Microsoft.Extensions.Caching.Memory;

namespace Oqtane.Controllers
{
Expand All @@ -21,13 +22,15 @@ public class InstallationController : Controller
private readonly IInstallationManager _installationManager;
private readonly IDatabaseManager _databaseManager;
private readonly ILocalizationManager _localizationManager;
private readonly IMemoryCache _cache;

public InstallationController(IConfigurationRoot config, IInstallationManager installationManager, IDatabaseManager databaseManager, ILocalizationManager localizationManager)
public InstallationController(IConfigurationRoot config, IInstallationManager installationManager, IDatabaseManager databaseManager, ILocalizationManager localizationManager, IMemoryCache cache)
{
_config = config;
_installationManager = installationManager;
_databaseManager = databaseManager;
_localizationManager = localizationManager;
_cache = cache;
}

// POST api/<controller>
Expand Down Expand Up @@ -70,6 +73,19 @@ public Installation Upgrade()
public IActionResult Load()
{
if (_config.GetSection("Runtime").Value == "WebAssembly")
{
return File(GetAssemblies(), System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.zip");
}
else
{
HttpContext.Response.StatusCode = 401;
return null;
}
}

private byte[] GetAssemblies()
{
return _cache.GetOrCreate("assemblies", entry =>
{
// get list of assemblies which should be downloaded to client
var assemblies = AppDomain.CurrentDomain.GetOqtaneClientAssemblies();
Expand All @@ -85,7 +101,7 @@ public IActionResult Load()
continue;
}

if(Directory.Exists(assembliesFolderPath))
if (Directory.Exists(assembliesFolderPath))
{
foreach (var resourceFile in Directory.EnumerateFiles(assembliesFolderPath))
{
Expand Down Expand Up @@ -120,42 +136,32 @@ public IActionResult Load()
}

// create zip file containing assemblies and debug symbols
byte[] zipfile;
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry entry;
foreach (string file in list)
{
entry = archive.CreateEntry(file + ".dll");
using (var filestream = new FileStream(Path.Combine(binFolder, file + ".dll"), FileMode.Open, FileAccess.Read))
using (var entrystream = entry.Open())
using (var entrystream = archive.CreateEntry(file + ".dll").Open())
{
filestream.CopyTo(entrystream);
}

// include debug symbols ( we may want to consider restricting this to only host users or when running in debug mode for performance )
// include debug symbols
if (System.IO.File.Exists(Path.Combine(binFolder, file + ".pdb")))
{
entry = archive.CreateEntry(file + ".pdb");
using (var filestream = new FileStream(Path.Combine(binFolder, file + ".pdb"), FileMode.Open, FileAccess.Read))
using (var entrystream = entry.Open())
using (var entrystream = archive.CreateEntry(file + ".pdb").Open())
{
filestream.CopyTo(entrystream);
}
}
}
}
zipfile = memoryStream.ToArray();
return memoryStream.ToArray();
}
return File(zipfile, System.Net.Mime.MediaTypeNames.Application.Octet, "oqtane.zip");
}
else
{
HttpContext.Response.StatusCode = 401;
return null;
}
});
}
}
}