From 22c5553d3a8258e86eedfb47ef8deaa528d03101 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Mon, 23 May 2022 23:45:40 -0700 Subject: [PATCH] Performance improvements --- Logging/Logger.cs | 1 - Teknik/Areas/Paste/PasteHelper.cs | 21 ++-- .../Upload/Controllers/UploadController.cs | 4 +- Teknik/Areas/Upload/UploadHelper.cs | 21 ++-- Teknik/Attributes/TrackDownloadAttribute.cs | 16 ++- Teknik/Attributes/TrackLinkAttribute.cs | 22 +++- Teknik/Attributes/TrackPageViewAttribute.cs | 27 ++++- Tracking/Tracking.cs | 104 +++++++----------- Utilities/ObjectCache.cs | 5 +- Utilities/ResponseHelper.cs | 4 +- 10 files changed, 134 insertions(+), 91 deletions(-) diff --git a/Logging/Logger.cs b/Logging/Logger.cs index a6d17e7..940cd37 100644 --- a/Logging/Logger.cs +++ b/Logging/Logger.cs @@ -57,7 +57,6 @@ namespace Teknik.Logging private void WriteLogMessage(LogMessage log) { - try { // Lock the file processing so only 1 thread is working on the log file at a time diff --git a/Teknik/Areas/Paste/PasteHelper.cs b/Teknik/Areas/Paste/PasteHelper.cs index a2604fd..b22724c 100644 --- a/Teknik/Areas/Paste/PasteHelper.cs +++ b/Teknik/Areas/Paste/PasteHelper.cs @@ -130,18 +130,21 @@ namespace Teknik.Areas.Paste // Fire and forget updating of the download count queue.QueueBackgroundWorkItem(async token => { - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(config.DbConnection); - - using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options)) + await Task.Run(() => { - var paste = GetPaste(db, url); - if (paste != null) + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(config.DbConnection); + + using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options)) { - paste.Views++; - ModifyPaste(db, paste); + var paste = GetPaste(db, url); + if (paste != null) + { + paste.Views++; + ModifyPaste(db, paste); + } } - } + }); }); } diff --git a/Teknik/Areas/Upload/Controllers/UploadController.cs b/Teknik/Areas/Upload/Controllers/UploadController.cs index 297343e..2f0fc75 100644 --- a/Teknik/Areas/Upload/Controllers/UploadController.cs +++ b/Teknik/Areas/Upload/Controllers/UploadController.cs @@ -405,7 +405,9 @@ namespace Teknik.Areas.Upload.Controllers byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); - return new BufferedFileStreamResult(contentType, async (response) => await ResponseHelper.StreamToOutput(response, true, new AesCounterStream(fileStream, false, keyBytes, ivBytes), (int)length, _config.UploadConfig.ChunkSize), false); + var aesStream = new AesCounterStream(fileStream, false, keyBytes, ivBytes); + + return new BufferedFileStreamResult(contentType, async (response) => await ResponseHelper.StreamToOutput(response, true, aesStream, (int)length, _config.UploadConfig.ChunkSize), false); } else // Otherwise just send it { diff --git a/Teknik/Areas/Upload/UploadHelper.cs b/Teknik/Areas/Upload/UploadHelper.cs index 3e16cd2..792cce4 100644 --- a/Teknik/Areas/Upload/UploadHelper.cs +++ b/Teknik/Areas/Upload/UploadHelper.cs @@ -152,18 +152,21 @@ namespace Teknik.Areas.Upload // Fire and forget updating of the download count queue.QueueBackgroundWorkItem(async token => { - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(config.DbConnection); - - using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options)) + await Task.Run(() => { - var upload = GetUpload(db, url); - if (upload != null) + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(config.DbConnection); + + using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options)) { - upload.Downloads++; - ModifyUpload(db, upload); + var upload = GetUpload(db, url); + if (upload != null) + { + upload.Downloads++; + ModifyUpload(db, upload); + } } - } + }); }); } diff --git a/Teknik/Attributes/TrackDownloadAttribute.cs b/Teknik/Attributes/TrackDownloadAttribute.cs index 8cd8963..561a041 100644 --- a/Teknik/Attributes/TrackDownloadAttribute.cs +++ b/Teknik/Attributes/TrackDownloadAttribute.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using System.Web; using Teknik.Configuration; using Teknik.Utilities; -using Teknik.Tracking; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Extensions; @@ -37,6 +36,9 @@ namespace Teknik.Attributes public override void OnActionExecuted(ActionExecutedContext filterContext) { + if (!_config.PiwikConfig.Enabled) + return; + HttpRequest request = filterContext.HttpContext.Request; string doNotTrack = request.Headers["DNT"]; @@ -50,12 +52,22 @@ namespace Teknik.Attributes string url = request.GetEncodedUrl(); + string tokenAuth = _config.PiwikConfig.TokenAuth; + int siteId = _config.PiwikConfig.SiteId; + string apiUrl = _config.PiwikConfig.Url; + // Fire and forget. Don't need to wait for it. _queue.QueueBackgroundWorkItem(async token => { await Task.Run(() => { - Tracking.Tracking.TrackDownload(filterContext.HttpContext, _config, userAgent, clientIp, url, urlReferrer); + Tracking.Tracking.TrackDownload(tokenAuth, + siteId, + apiUrl, + userAgent, + clientIp, + url, + urlReferrer); }); }); } diff --git a/Teknik/Attributes/TrackLinkAttribute.cs b/Teknik/Attributes/TrackLinkAttribute.cs index 5c66d48..d58892d 100644 --- a/Teknik/Attributes/TrackLinkAttribute.cs +++ b/Teknik/Attributes/TrackLinkAttribute.cs @@ -15,10 +15,12 @@ namespace Teknik.Attributes { public class TrackLink : ActionFilterAttribute { + private readonly IBackgroundTaskQueue _queue; private readonly Config _config; - public TrackLink(Config config) + public TrackLink(IBackgroundTaskQueue queue, Config config) { + _queue = queue; _config = config; } @@ -41,8 +43,24 @@ namespace Teknik.Attributes string url = request.GetEncodedUrl(); + string tokenAuth = _config.PiwikConfig.TokenAuth; + int siteId = _config.PiwikConfig.SiteId; + string apiUrl = _config.PiwikConfig.Url; + // Fire and forget. Don't need to wait for it. - Tracking.Tracking.TrackLink(filterContext.HttpContext, _config, userAgent, clientIp, url, urlReferrer); + _queue.QueueBackgroundWorkItem(async token => + { + await Task.Run(() => + { + Tracking.Tracking.TrackLink(tokenAuth, + siteId, + apiUrl, + userAgent, + clientIp, + url, + urlReferrer); + }); + }); } } } diff --git a/Teknik/Attributes/TrackPageViewAttribute.cs b/Teknik/Attributes/TrackPageViewAttribute.cs index 23d279f..6d8047c 100644 --- a/Teknik/Attributes/TrackPageViewAttribute.cs +++ b/Teknik/Attributes/TrackPageViewAttribute.cs @@ -38,6 +38,9 @@ namespace Teknik.Attributes public override void OnActionExecuted(ActionExecutedContext filterContext) { + if (!_config.PiwikConfig.Enabled) + return; + HttpRequest request = filterContext.HttpContext.Request; string doNotTrack = request.Headers["DNT"]; @@ -68,10 +71,32 @@ namespace Teknik.Attributes bool hasJava = false; + string tokenAuth = _config.PiwikConfig.TokenAuth; + int siteId = _config.PiwikConfig.SiteId; + string apiUrl = _config.PiwikConfig.Url; + bool isDev = _config.DevEnvironment; + // Fire and forget. Don't need to wait for it. _queue.QueueBackgroundWorkItem(async token => { - Tracking.Tracking.TrackPageView(filterContext.HttpContext, _config, title, sub, clientIp, url, urlReferrer, userAgent, pixelWidth, pixelHeight, hasCookies, acceptLang, hasJava); + await Task.Run(() => + { + Tracking.Tracking.TrackPageView(tokenAuth, + siteId, + apiUrl, + title, + sub, + clientIp, + url, + urlReferrer, + userAgent, + pixelWidth, + pixelHeight, + hasCookies, + acceptLang, + hasJava, + isDev); + }); }); } } diff --git a/Tracking/Tracking.cs b/Tracking/Tracking.cs index b073b5e..e4bcae9 100644 --- a/Tracking/Tracking.cs +++ b/Tracking/Tracking.cs @@ -9,86 +9,66 @@ namespace Teknik.Tracking { public static class Tracking { - public static void TrackPageView(HttpContext context, Config config, string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava) + public static void TrackPageView(string token, int siteId, string apiUrl, string title, string sub, string clientIp, string url, string urlReferrer, string userAgent, int pixelWidth, int pixelHeight, bool hasCookies, string acceptLang, bool hasJava, bool isDev) { - try + if (isDev) { - if (config.PiwikConfig.Enabled) - { - if (config.DevEnvironment) - { - sub = "dev - " + sub; - } - - PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url, context); - - // Set Request Info - tracker.SetIp(clientIp); - tracker.SetTokenAuth(config.PiwikConfig.TokenAuth); - - tracker.SetUserAgent(userAgent); - - // Set browser info - tracker.SetResolution(pixelWidth, pixelHeight); - tracker.SetBrowserHasCookies(hasCookies); - if (!string.IsNullOrEmpty(acceptLang)) - tracker.SetBrowserLanguage(acceptLang); - tracker.SetPlugins(new BrowserPlugins { Java = hasJava }); - - // Get Referral - if (!string.IsNullOrEmpty(urlReferrer)) - tracker.SetUrlReferrer(urlReferrer); - - if (!string.IsNullOrEmpty(url)) - tracker.SetUrl(url); - - // Send the tracking request - tracker.DoTrackPageView(string.Format("{0}/{1}", sub, title)); - } + sub = "dev - " + sub; } - catch (Exception) - { - } + PiwikTracker tracker = new PiwikTracker(siteId, apiUrl); + + // Set Request Info + tracker.SetIp(clientIp); + tracker.SetTokenAuth(token); + + tracker.SetUserAgent(userAgent); + + // Set browser info + tracker.SetResolution(pixelWidth, pixelHeight); + tracker.SetBrowserHasCookies(hasCookies); + if (!string.IsNullOrEmpty(acceptLang)) + tracker.SetBrowserLanguage(acceptLang); + tracker.SetPlugins(new BrowserPlugins { Java = hasJava }); + + // Get Referral + if (!string.IsNullOrEmpty(urlReferrer)) + tracker.SetUrlReferrer(urlReferrer); + + if (!string.IsNullOrEmpty(url)) + tracker.SetUrl(url); + + // Send the tracking request + tracker.DoTrackPageView(string.Format("{0}/{1}", sub, title)); } - public static void TrackDownload(HttpContext context, Config config, string userAgent, string clientIp, string url, string urlReferrer) + public static void TrackDownload(string token, int siteId, string apiUrl, string userAgent, string clientIp, string url, string urlReferrer) { - TrackAction(context, config, ActionType.Download, userAgent, clientIp, url, urlReferrer); + TrackAction(token, siteId, apiUrl, ActionType.Download, userAgent, clientIp, url, urlReferrer); } - public static void TrackLink(HttpContext context, Config config, string userAgent, string clientIp, string url, string urlReferrer) + public static void TrackLink(string token, int siteId, string apiUrl, string userAgent, string clientIp, string url, string urlReferrer) { - TrackAction(context, config, ActionType.Link, userAgent, clientIp, url, urlReferrer); + TrackAction(token, siteId, apiUrl, ActionType.Link, userAgent, clientIp, url, urlReferrer); } - private static void TrackAction(HttpContext context, Config config, ActionType type, string userAgent, string clientIp, string url, string urlReferrer) + private static void TrackAction(string token, int siteId, string apiUrl, ActionType type, string userAgent, string clientIp, string url, string urlReferrer) { - try - { - if (config.PiwikConfig.Enabled) - { - PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId, config.PiwikConfig.Url, context); + PiwikTracker tracker = new PiwikTracker(siteId, apiUrl); - tracker.SetUserAgent(userAgent); + tracker.SetUserAgent(userAgent); - tracker.SetIp(clientIp); - tracker.SetTokenAuth(config.PiwikConfig.TokenAuth); + tracker.SetIp(clientIp); + tracker.SetTokenAuth(token); - // Get Referral - if (!string.IsNullOrEmpty(urlReferrer)) - tracker.SetUrlReferrer(urlReferrer); + // Get Referral + if (!string.IsNullOrEmpty(urlReferrer)) + tracker.SetUrlReferrer(urlReferrer); - if (!string.IsNullOrEmpty(url)) - tracker.SetUrl(url); + if (!string.IsNullOrEmpty(url)) + tracker.SetUrl(url); - tracker.DoTrackAction(url, type); - } - } - catch (Exception ex) - { - - } + tracker.DoTrackAction(url, type); } } } diff --git a/Utilities/ObjectCache.cs b/Utilities/ObjectCache.cs index 7898241..57a4ca7 100644 --- a/Utilities/ObjectCache.cs +++ b/Utilities/ObjectCache.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,7 +9,7 @@ namespace Teknik.Utilities { public class ObjectCache { - private readonly static Dictionary> objectCache = new Dictionary>(); + private readonly static ConcurrentDictionary> objectCache = new ConcurrentDictionary>(); private readonly int _cacheSeconds; public ObjectCache(int cacheSeconds) @@ -48,7 +49,7 @@ namespace Teknik.Utilities public void DeleteObject(string key) { - objectCache.Remove(key); + objectCache.TryRemove(key, out _); } public bool CacheValid(string key) diff --git a/Utilities/ResponseHelper.cs b/Utilities/ResponseHelper.cs index 2b6e46d..5136660 100644 --- a/Utilities/ResponseHelper.cs +++ b/Utilities/ResponseHelper.cs @@ -30,7 +30,7 @@ namespace Teknik.Utilities // Flush the response if (flush) { - await response.Body.FlushAsync(); + //await response.Body.FlushAsync(); } } } @@ -42,7 +42,7 @@ namespace Teknik.Utilities } finally { - await response.Body.FlushAsync(); + //await response.Body.FlushAsync(); // dispose of file stream stream?.Dispose();