Skip to content

Commit 8a9a2fe

Browse files
authored
Merge pull request #197 from Eizock/add-MinChunkSize
Add MinimumChunkSize to set minimum size a single chunk should have
2 parents 1d9379f + 8af223f commit 8a9a2fe

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/Downloader/ChunkHub.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23

34
namespace Downloader;
45

@@ -69,6 +70,16 @@ private void Validate(DownloadPackage package)
6970
_chunkCount = 1;
7071
}
7172

73+
if (_config.MinimumChunkSize > 0)
74+
{
75+
int maxChunksByMinSize = (int)(package.TotalFileSize / _config.MinimumChunkSize);
76+
if (maxChunksByMinSize < 1)
77+
maxChunksByMinSize = 1;
78+
79+
// Respect both ChunkCount and MinChunkSize constraints
80+
_chunkCount = Math.Min(_config.ChunkCount, maxChunksByMinSize);
81+
}
82+
7283
_chunkSize = package.TotalFileSize / _chunkCount;
7384
}
7485

src/Downloader/DownloadConfiguration.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class DownloadConfiguration : ICloneable, INotifyPropertyChanged
2929
// minimum size of chunking to download a file in multiple parts
3030
private long _minimumSizeOfChunking = 512;
3131

32+
// minimum size of a single chunk
33+
private long _minimumChunkSize = 0;
34+
3235
// Before starting the download, reserve the storage space of the file as file size.
3336
private bool _reserveStorageSpaceBeforeStartingDownload;
3437

@@ -218,6 +221,18 @@ public long MinimumSizeOfChunking
218221
set => OnPropertyChanged(ref _minimumSizeOfChunking, value);
219222
}
220223

224+
/// <summary>
225+
/// Gets or sets the minimum size of a single chunk
226+
/// If it is not 0 it dynamically reduces the chunk count to keep the chunk size above this value
227+
/// Keeps ChunkCount as a Maximum
228+
/// Default value is 0
229+
/// </summary>
230+
public long MinimumChunkSize
231+
{
232+
get => _minimumChunkSize;
233+
set => OnPropertyChanged(ref _minimumChunkSize, value);
234+
}
235+
221236
/// <summary>
222237
/// Gets or sets a value indicating whether to reserve the storage space of the file as file size before starting the download.
223238
/// Default value is false.

src/Samples/Downloader.Sample/Program.Config.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ private static DownloadConfiguration GetDownloadConfiguration()
2424
RangeHigh = 0, // ceiling offset of download range of a large file
2525
ClearPackageOnCompletionWithFailure = true, // Clear package and downloaded data when download completed with failure, default value is false
2626
MinimumSizeOfChunking = 1024, // minimum size of chunking to download a file in multiple parts, default value is 512
27+
MinimumChunkSize = 0, // minimum size of a single chunk, 0 disables this, default is 0
2728
ReserveStorageSpaceBeforeStartingDownload = false, // Before starting the download, reserve the storage space of the file as file size, default value is false
2829
EnableLiveStreaming = false, // Get on demand downloaded data with ReceivedBytes on downloadProgressChanged event
2930
RequestConfiguration =

0 commit comments

Comments
 (0)