-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve performance for api/types (most of the code in Southwind)
- Loading branch information
1 parent
9a4b7ff
commit b47a37c
Showing
3 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Net.Http.Headers; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Signum.React.Filters | ||
{ | ||
public class ETagMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ETagMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
var response = context.Response; | ||
var originalStream = response.Body; | ||
|
||
using (var ms = new MemoryStream()) | ||
{ | ||
response.Body = ms; | ||
|
||
await _next(context); | ||
|
||
if (IsEtagSupported(response)) | ||
{ | ||
string checksum = CalculateChecksum(ms); | ||
|
||
response.Headers[HeaderNames.ETag] = checksum; | ||
|
||
if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag) && checksum == etag) | ||
{ | ||
response.StatusCode = StatusCodes.Status304NotModified; | ||
return; | ||
} | ||
} | ||
|
||
ms.Position = 0; | ||
await ms.CopyToAsync(originalStream); | ||
} | ||
} | ||
|
||
private static bool IsEtagSupported(HttpResponse response) | ||
{ | ||
if (response.StatusCode != StatusCodes.Status200OK) | ||
return false; | ||
|
||
// The 20kb length limit is not based in science. Feel free to change | ||
if (response.Body.Length > 10 * 1024 * 1024) | ||
return false; | ||
|
||
if (response.Headers.ContainsKey(HeaderNames.ETag)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
private static string CalculateChecksum(MemoryStream ms) | ||
{ | ||
using (var algo = SHA1.Create()) | ||
{ | ||
ms.Position = 0; | ||
byte[] bytes = algo.ComputeHash(ms); | ||
return WebEncoders.Base64UrlEncode(bytes); | ||
} | ||
} | ||
} | ||
|
||
public static class ApplicationBuilderExtensions | ||
{ | ||
public static void UseETagger(this IApplicationBuilder app) | ||
{ | ||
app.UseMiddleware<ETagMiddleware>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b47a37c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve api/reflection/types performance in loading animation.
Checkout the latest changes in Southind:
api/reflection/type
by adding HTTP cache using ETag and HTTP Compression. Very important to improve performance of applications with many entities when logging-in or openning a new tab. Also, I've added a loading animation in Index.cshtml , it's a customized version of one of the spinners in [https://365webresources.com/best-pure-css-loading-spinners/]. Feel free to change it by something that fit better with your design.b47a37c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.