mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2024-11-25 05:42:45 +01:00
Compare commits
No commits in common. "07615de87ad369d6ae877891ee466867612daf66" and "67b9c1284367a6d9eaa648975d30fca2b3ec684f" have entirely different histories.
07615de87a
...
67b9c12843
@ -15,7 +15,6 @@ sealed class Arguments {
|
||||
public string? DatabaseFile { get; }
|
||||
public ushort? ServerPort { get; }
|
||||
public string? ServerToken { get; }
|
||||
public byte? ConcurrentDownloads { get; }
|
||||
|
||||
public Arguments(IReadOnlyList<string> args) {
|
||||
for (int i = FirstArgument; i < args.Count; i++) {
|
||||
@ -51,11 +50,11 @@ sealed class Arguments {
|
||||
continue;
|
||||
|
||||
case "-port": {
|
||||
if (!ushort.TryParse(value, out var port)) {
|
||||
Log.Warn("Invalid port number: " + value);
|
||||
if (ushort.TryParse(value, out var port)) {
|
||||
ServerPort = port;
|
||||
}
|
||||
else {
|
||||
ServerPort = port;
|
||||
Log.Warn("Invalid port number: " + value);
|
||||
}
|
||||
|
||||
continue;
|
||||
@ -64,20 +63,6 @@ sealed class Arguments {
|
||||
case "-token":
|
||||
ServerToken = value;
|
||||
continue;
|
||||
|
||||
case "-concurrentdownloads":
|
||||
if (!ulong.TryParse(value, out var concurrentDownloads) || concurrentDownloads == 0) {
|
||||
Log.Warn("Invalid concurrent downloads count: " + value);
|
||||
}
|
||||
else if (concurrentDownloads > 10) {
|
||||
Log.Warn("Limiting concurrent downloads to 10");
|
||||
ConcurrentDownloads = 10;
|
||||
}
|
||||
else {
|
||||
ConcurrentDownloads = (byte) concurrentDownloads;
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
default:
|
||||
Log.Warn("Unknown command line argument: " + key);
|
||||
|
@ -30,7 +30,6 @@ sealed partial class MainWindowModel : ObservableObject, IAsyncDisposable {
|
||||
private MainContentScreenModel? mainContentScreenModel;
|
||||
|
||||
private readonly Window window;
|
||||
private readonly int? concurrentDownloads;
|
||||
|
||||
private State? state;
|
||||
|
||||
@ -74,8 +73,6 @@ sealed partial class MainWindowModel : ObservableObject, IAsyncDisposable {
|
||||
if (args.ServerToken != null) {
|
||||
ServerConfiguration.Token = args.ServerToken;
|
||||
}
|
||||
|
||||
concurrentDownloads = args.ConcurrentDownloads;
|
||||
}
|
||||
|
||||
private async void OnDatabaseSelected(object? sender, IDatabaseFile db) {
|
||||
@ -83,7 +80,7 @@ sealed partial class MainWindowModel : ObservableObject, IAsyncDisposable {
|
||||
|
||||
await DisposeState();
|
||||
|
||||
state = new State(db, concurrentDownloads);
|
||||
state = new State(db);
|
||||
|
||||
try {
|
||||
await state.Server.Start(ServerConfiguration.Port, ServerConfiguration.Token);
|
||||
|
@ -11,18 +11,16 @@ public sealed class Downloader {
|
||||
public bool IsDownloading => current != null;
|
||||
|
||||
private readonly IDatabaseFile db;
|
||||
private readonly int? concurrentDownloads;
|
||||
private readonly SemaphoreSlim semaphore = new (1, 1);
|
||||
|
||||
internal Downloader(IDatabaseFile db, int? concurrentDownloads) {
|
||||
internal Downloader(IDatabaseFile db) {
|
||||
this.db = db;
|
||||
this.concurrentDownloads = concurrentDownloads;
|
||||
}
|
||||
|
||||
public async Task<IObservable<DownloadItem>> Start(DownloadItemFilter filter) {
|
||||
await semaphore.WaitAsync();
|
||||
try {
|
||||
current ??= new DownloaderTask(db, filter, concurrentDownloads);
|
||||
current ??= new DownloaderTask(db, filter);
|
||||
return current.FinishedItems;
|
||||
} finally {
|
||||
semaphore.Release();
|
||||
|
@ -15,14 +15,10 @@ namespace DHT.Server.Download;
|
||||
sealed class DownloaderTask : IAsyncDisposable {
|
||||
private static readonly Log Log = Log.ForType<DownloaderTask>();
|
||||
|
||||
private const int DefaultConcurrentDownloads = 4;
|
||||
private const int DownloadTasks = 4;
|
||||
private const int QueueSize = 25;
|
||||
private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
|
||||
|
||||
private static int GetDownloadTaskCount(int? concurrentDownloads) {
|
||||
return Math.Max(1, concurrentDownloads ?? DefaultConcurrentDownloads);
|
||||
}
|
||||
|
||||
private readonly Channel<DownloadItem> downloadQueue = Channel.CreateBounded<DownloadItem>(new BoundedChannelOptions(QueueSize) {
|
||||
SingleReader = false,
|
||||
SingleWriter = true,
|
||||
@ -42,12 +38,12 @@ sealed class DownloaderTask : IAsyncDisposable {
|
||||
|
||||
public IObservable<DownloadItem> FinishedItems => finishedItemPublisher;
|
||||
|
||||
internal DownloaderTask(IDatabaseFile db, DownloadItemFilter filter, int? concurrentDownloads) {
|
||||
internal DownloaderTask(IDatabaseFile db, DownloadItemFilter filter) {
|
||||
this.db = db;
|
||||
this.filter = filter;
|
||||
this.cancellationToken = cancellationTokenSource.Token;
|
||||
this.queueWriterTask = Task.Run(RunQueueWriterTask);
|
||||
this.downloadTasks = Enumerable.Range(1, GetDownloadTaskCount(concurrentDownloads)).Select(taskIndex => Task.Run(() => RunDownloadTask(taskIndex))).ToArray();
|
||||
this.downloadTasks = Enumerable.Range(1, DownloadTasks).Select(taskIndex => Task.Run(() => RunDownloadTask(taskIndex))).ToArray();
|
||||
}
|
||||
|
||||
private async Task RunQueueWriterTask() {
|
||||
@ -78,12 +74,8 @@ sealed class DownloaderTask : IAsyncDisposable {
|
||||
try {
|
||||
var downloadedBytes = await client.GetByteArrayAsync(item.DownloadUrl, cancellationToken);
|
||||
await db.Downloads.AddDownload(item.ToSuccess(downloadedBytes));
|
||||
} catch (OperationCanceledException e) when (e.CancellationToken == cancellationToken) {
|
||||
} catch (OperationCanceledException) {
|
||||
// Ignore.
|
||||
} catch (TaskCanceledException e) {
|
||||
// HttpClient request timed out.
|
||||
await db.Downloads.AddDownload(item.ToFailure());
|
||||
log.Error(e.Message);
|
||||
} catch (HttpRequestException e) {
|
||||
await db.Downloads.AddDownload(item.ToFailure(e.StatusCode));
|
||||
log.Error(e);
|
||||
|
@ -6,12 +6,18 @@ using DHT.Server.Service;
|
||||
|
||||
namespace DHT.Server;
|
||||
|
||||
public sealed class State(IDatabaseFile db, int? concurrentDownloads) : IAsyncDisposable {
|
||||
public static State Dummy { get; } = new (DummyDatabaseFile.Instance, null);
|
||||
public sealed class State : IAsyncDisposable {
|
||||
public static State Dummy { get; } = new (DummyDatabaseFile.Instance);
|
||||
|
||||
public IDatabaseFile Db { get; } = db;
|
||||
public Downloader Downloader { get; } = new (db, concurrentDownloads);
|
||||
public ServerManager Server { get; } = new (db);
|
||||
public IDatabaseFile Db { get; }
|
||||
public Downloader Downloader { get; }
|
||||
public ServerManager Server { get; }
|
||||
|
||||
public State(IDatabaseFile db) {
|
||||
Db = db;
|
||||
Downloader = new Downloader(db);
|
||||
Server = new ServerManager(db);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync() {
|
||||
await Downloader.Stop();
|
||||
|
Loading…
Reference in New Issue
Block a user