Skip to content

Commit

Permalink
Use options instead of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceredron committed Nov 14, 2024
1 parent 4222609 commit 1fb3509
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .azure/modules/containerApp/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var containerAppEnvVars = [
}
{ name: 'MaskinportenSettings__EncodedJwk', secretRef: 'maskinporten-jwk' }
{ name: 'GeneralSettings__SlackUrl', secretRef: 'slack-url' }
{ name: 'AzureStorageOptions__BlockSize', value: '4194304' }
{ name: 'AzureStorageOptions__BlockSize', value: '33554432' }
{ name: 'AzureStorageOptions__ConcurrentUploadThreads', value: '3' }
{ name: 'AzureStorageOptions__BlocksBeforeCommit', value: '1000' }
]
Expand Down
5 changes: 2 additions & 3 deletions src/Altinn.Broker.API/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
"ExhangeToAltinnToken": true
},
"GeneralSettings": {
"SlackUrl": "",
"CorrespondenceBaseUrl": "https://localhost:7241/"
"SlackUrl": ""
},
"AzureStorageOptions": {
"BlockSize": 4194304,
"BlockSize": 33554432,
"ConcurrentUploadThreads": 3,
"BlocksBeforeCommit": 1000
}
Expand Down
4 changes: 1 addition & 3 deletions src/Altinn.Broker.Core/Options/GeneralSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ namespace Altinn.Broker.Core.Options;
public class GeneralSettings
{
public string SlackUrl { get; set; } = string.Empty;

public string CorrespondenceBaseUrl { get; set; } = string.Empty;
}
}
18 changes: 8 additions & 10 deletions src/Altinn.Broker.Integrations/Azure/AzureStorageService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security.Cryptography;

using Altinn.Broker.Core.Domain;
using Altinn.Broker.Core.Options;
using Altinn.Broker.Core.Services;

using Azure;
Expand All @@ -9,17 +10,14 @@
using Azure.Storage.Blobs.Specialized;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using Polly;

namespace Altinn.Broker.Integrations.Azure;

public class AzureStorageService(IResourceManager resourceManager, ILogger<AzureStorageService> logger) : IBrokerStorageService
public class AzureStorageService(IResourceManager resourceManager, IOptions<AzureStorageOptions> azureStorageOptions, ILogger<AzureStorageService> logger) : IBrokerStorageService
{
private const int BLOCK_SIZE = 1024 * 1024 * 32; // 32MB
private const int BLOCKS_BEFORE_COMMIT = 1000;
private const int UPLOAD_THREADS = 3;

private async Task<BlobContainerClient> GetBlobContainerClient(FileTransferEntity fileTransferEntity, ServiceOwnerEntity serviceOwnerEntity)
{
var storageProvider = serviceOwnerEntity.GetStorageProvider(fileTransferEntity.UseVirusScan);
Expand Down Expand Up @@ -69,7 +67,7 @@ public async Task<Stream> DownloadFile(ServiceOwnerEntity serviceOwnerEntity, Fi

int blocksInBatch = 0;
var uploadTasks = new List<Task>();
var semaphore = new SemaphoreSlim(UPLOAD_THREADS); // Limit concurrent operations
var semaphore = new SemaphoreSlim(azureStorageOptions.Value.ConcurrentUploadThreads); // Limit concurrent operations

while (position < streamLength)
{
Expand All @@ -80,7 +78,7 @@ public async Task<Stream> DownloadFile(ServiceOwnerEntity serviceOwnerEntity, Fi
position += bytesRead;

bool isLastBlock = position >= streamLength;
if (accumulationBuffer.Length >= BLOCK_SIZE || isLastBlock)
if (accumulationBuffer.Length >= azureStorageOptions.Value.BlockSize || isLastBlock)
{
accumulationBuffer.Position = 0;
var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
Expand Down Expand Up @@ -108,13 +106,13 @@ async Task UploadBlockAsync(BlockBlobClient client, string currentBlockId, byte[
}
}

if (uploadTasks.Count >= BLOCKS_BEFORE_COMMIT)
if (uploadTasks.Count >= azureStorageOptions.Value.BlocksBeforeCommit)
{
await Task.WhenAll(uploadTasks);

// Commit the blocks we have so far
var blocksToCommit = blockList.ToList();
var isFirstCommit = blockList.Count <= BLOCKS_BEFORE_COMMIT;
var isFirstCommit = blockList.Count <= azureStorageOptions.Value.BlocksBeforeCommit;
await CommitBlocks(blockBlobClient, blocksToCommit, firstCommit: isFirstCommit, null, cancellationToken);

uploadTasks.Clear();
Expand All @@ -129,7 +127,7 @@ async Task UploadBlockAsync(BlockBlobClient client, string currentBlockId, byte[
{
throw new Exception("Failed to calculate MD5 hash of uploaded file");
}
await CommitBlocks(blockBlobClient, blockList, firstCommit: blockList.Count <= BLOCKS_BEFORE_COMMIT, blobMd5.Hash, cancellationToken);
await CommitBlocks(blockBlobClient, blockList, firstCommit: blockList.Count <= azureStorageOptions.Value.BlocksBeforeCommit, blobMd5.Hash, cancellationToken);

double finalSpeedMBps = position / (1024.0 * 1024) / (stopwatch.ElapsedMilliseconds / 1000.0);
logger.LogInformation($"Successfully uploaded {position / (1024.0 * 1024.0 * 1024.0):N2} GiB " +
Expand Down

0 comments on commit 1fb3509

Please sign in to comment.