1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Performance improvements

This commit is contained in:
Uncled1023 2022-05-23 23:45:40 -07:00
parent ecfff24d25
commit 22c5553d3a
10 changed files with 134 additions and 91 deletions

View File

@ -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

View File

@ -130,18 +130,21 @@ namespace Teknik.Areas.Paste
// Fire and forget updating of the download count
queue.QueueBackgroundWorkItem(async token =>
{
var optionsBuilder = new DbContextOptionsBuilder<TeknikEntities>();
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<TeknikEntities>();
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);
}
}
}
});
});
}

View File

@ -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
{

View File

@ -152,18 +152,21 @@ namespace Teknik.Areas.Upload
// Fire and forget updating of the download count
queue.QueueBackgroundWorkItem(async token =>
{
var optionsBuilder = new DbContextOptionsBuilder<TeknikEntities>();
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<TeknikEntities>();
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);
}
}
}
});
});
}

View File

@ -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);
});
});
}

View File

@ -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);
});
});
}
}
}

View File

@ -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);
});
});
}
}

View File

@ -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);
}
}
}

View File

@ -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<string, Tuple<DateTime, object>> objectCache = new Dictionary<string, Tuple<DateTime, object>>();
private readonly static ConcurrentDictionary<string, Tuple<DateTime, object>> objectCache = new ConcurrentDictionary<string, Tuple<DateTime, object>>();
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)

View File

@ -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();