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

Optimizations

This commit is contained in:
Uncled1023 2022-05-27 23:03:57 -07:00
parent dc76918300
commit 92ac154619
5 changed files with 40 additions and 11 deletions

View File

@ -14,6 +14,7 @@ namespace Teknik.ContentScanningService
public class HashScanner : ContentScanner
{
private static readonly HttpClient _client = new HttpClient();
public HashScanner(Config config) : base(config)
{ }

View File

@ -16,6 +16,8 @@ namespace Teknik.Areas.Users.Utility
{
public static class IdentityHelper
{
private static HttpClient _httpClient = new HttpClient();
public static async Task GetAccessToken(this HttpClient client, Config config)
{
var token = await client.GetAccessToken(config.UserConfig.IdentityServerConfig.Authority, config.UserConfig.IdentityServerConfig.ClientId, config.UserConfig.IdentityServerConfig.ClientSecret, "auth-api");
@ -48,10 +50,9 @@ namespace Teknik.Areas.Users.Utility
public static async Task<IdentityResult> Get(Config config, Uri url)
{
var client = new HttpClient();
await client.GetAccessToken(config);
await _httpClient.GetAccessToken(config);
var content = await client.GetStringAsync(url);
var content = await _httpClient.GetStringAsync(url);
if (!string.IsNullOrEmpty(content))
{
return JsonConvert.DeserializeObject<IdentityResult>(content);
@ -62,11 +63,10 @@ namespace Teknik.Areas.Users.Utility
public static async Task<IdentityResult> Post(Config config, Uri url, object data)
{
var client = new HttpClient();
await client.GetAccessToken(config);
await _httpClient.GetAccessToken(config);
var response = await client.PostAsJsonAsync(url, data);
var response = await _httpClient.PostAsJsonAsync(url, data);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();

View File

@ -248,8 +248,8 @@ namespace Teknik.Utilities.Cryptography
protected override void Dispose(bool disposing)
{
_Cipher.Dispose();
_Inner.Dispose();
_Cipher.Dispose();
base.Dispose(disposing);
}

26
Utilities/PooledArray.cs Normal file
View File

@ -0,0 +1,26 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Utilities
{
public class PooledArray : IDisposable
{
private static ArrayPool<byte> _arrayPool = ArrayPool<byte>.Create();
public byte[] Array { get; private set; }
public PooledArray(int size)
{
Array = _arrayPool.Rent(size);
}
public void Dispose()
{
_arrayPool.Return(Array);
}
}
}

View File

@ -13,20 +13,22 @@ namespace Teknik.Utilities
{
public async static Task StreamToOutput(HttpResponse response, Stream stream, int length, int chunkSize)
{
response.RegisterForDisposeAsync(stream);
var bufferSize = chunkSize;
if (length < chunkSize)
bufferSize = length;
Memory<byte> buffer = new byte[bufferSize];
var pooledArray = new PooledArray(bufferSize);
response.RegisterForDispose(stream);
response.RegisterForDispose(pooledArray);
try
{
int processedBytes;
do
{
processedBytes = await stream.ReadAsync(buffer);
processedBytes = await stream.ReadAsync(pooledArray.Array);
if (processedBytes > 0)
{
await response.Body.WriteAsync(buffer.Slice(0, processedBytes));
await response.Body.WriteAsync(pooledArray.Array, 0, processedBytes);
await response.Body.FlushAsync();
}