diff --git a/Tools/BiblioTech/CodexChecking/CheckRepo.cs b/Tools/BiblioTech/CodexChecking/CheckRepo.cs index a8b2860b..a02e44b7 100644 --- a/Tools/BiblioTech/CodexChecking/CheckRepo.cs +++ b/Tools/BiblioTech/CodexChecking/CheckRepo.cs @@ -1,16 +1,20 @@ -using Newtonsoft.Json; +using Logging; +using Newtonsoft.Json; +using Utils; namespace BiblioTech.CodexChecking { public class CheckRepo { private const string modelFilename = "model.json"; + private readonly ILog log; private readonly Configuration config; private readonly object _lock = new object(); private CheckRepoModel? model = null; - public CheckRepo(Configuration config) + public CheckRepo(ILog log, Configuration config) { + this.log = log; this.config = config; } @@ -18,20 +22,32 @@ namespace BiblioTech.CodexChecking { lock (_lock) { - if (model == null) LoadModel(); - - var existing = model.Reports.SingleOrDefault(r => r.UserId == userId); - if (existing == null) + var sw = System.Diagnostics.Stopwatch.StartNew(); + try { - var newEntry = new CheckReport + if (model == null) LoadModel(); + + var existing = model.Reports.SingleOrDefault(r => r.UserId == userId); + if (existing == null) { - UserId = userId, - }; - model.Reports.Add(newEntry); - SaveChanges(); - return newEntry; + var newEntry = new CheckReport + { + UserId = userId, + }; + model.Reports.Add(newEntry); + SaveChanges(); + return newEntry; + } + return existing; + } + finally + { + var elapsed = sw.Elapsed; + if (elapsed > TimeSpan.FromMilliseconds(500)) + { + log.Log($"Warning {nameof(GetOrCreate)} took {Time.FormatDuration(elapsed)}"); + } } - return existing; } } diff --git a/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs b/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs index 1dc6b681..47508b4b 100644 --- a/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs +++ b/Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs @@ -43,7 +43,7 @@ namespace BiblioTech.CodexChecking repo.SaveChanges(); } - var cid = UploadData(check.UniqueData); + var cid = await UploadData(check.UniqueData); await handler.GiveCidToUser(cid); } @@ -87,7 +87,7 @@ namespace BiblioTech.CodexChecking return; } - var manifest = GetManifest(receivedCid); + var manifest = await GetManifest(receivedCid); if (manifest == null) { await handler.CouldNotDownloadCid(); @@ -96,7 +96,7 @@ namespace BiblioTech.CodexChecking if (IsManifestLengthCompatible(handler, check, manifest)) { - if (IsContentCorrect(handler, check, receivedCid)) + if (await IsContentCorrect(handler, check, receivedCid)) { await CheckNowCompleted(handler, check, userId, "UploadCheck"); return; @@ -120,7 +120,7 @@ namespace BiblioTech.CodexChecking check.CompletedUtc < expiry; } - private string UploadData(string uniqueData) + private async Task UploadData(string uniqueData) { var filePath = Path.Combine(config.ChecksDataPath, Guid.NewGuid().ToString()); @@ -129,7 +129,7 @@ namespace BiblioTech.CodexChecking File.WriteAllText(filePath, uniqueData); var file = new TrackedFile(log, filePath, "checkData"); - return codexWrapper.OnCodex(node => + return await codexWrapper.OnCodex(node => { return node.UploadFile(file).Id; }); @@ -145,11 +145,11 @@ namespace BiblioTech.CodexChecking } } - private Manifest? GetManifest(string receivedCid) + private async Task GetManifest(string receivedCid) { try { - return codexWrapper.OnCodex(node => + return await codexWrapper.OnCodex(node => { return node.DownloadManifestOnly(new ContentId(receivedCid)).Manifest; }); @@ -172,11 +172,11 @@ namespace BiblioTech.CodexChecking manifestLength < (dataLength + 1); } - private bool IsContentCorrect(ICheckResponseHandler handler, TransferCheck check, string receivedCid) + private async Task IsContentCorrect(ICheckResponseHandler handler, TransferCheck check, string receivedCid) { try { - var content = codexWrapper.OnCodex(node => + var content = await codexWrapper.OnCodex(node => { var file = node.DownloadContent(new ContentId(receivedCid)); if (file == null) return string.Empty; diff --git a/Tools/BiblioTech/CodexChecking/CodexWrapper.cs b/Tools/BiblioTech/CodexChecking/CodexWrapper.cs index 3c295d7f..f7f871fa 100644 --- a/Tools/BiblioTech/CodexChecking/CodexWrapper.cs +++ b/Tools/BiblioTech/CodexChecking/CodexWrapper.cs @@ -23,20 +23,26 @@ namespace BiblioTech.CodexChecking factory = new CodexNodeFactory(log, httpFactory, dataDir: config.DataPath); } - public void OnCodex(Action action) + public async Task OnCodex(Action action) { - lock (codexLock) + await Task.Run(() => { - action(Get()); - } + lock (codexLock) + { + action(Get()); + } + }); } - public T OnCodex(Func func) + public async Task OnCodex(Func func) { - lock (codexLock) + return await Task.Run(() => { - return func(Get()); - } + lock (codexLock) + { + return func(Get()); + } + }); } private ICodexNode Get() @@ -76,10 +82,28 @@ namespace BiblioTech.CodexChecking var tokens = config.CodexEndpointAuth.Split(':'); if (tokens.Length != 2) throw new Exception("Expected ':' in CodexEndpointAuth parameter."); - return new HttpFactory(log, onClientCreated: client => + return new HttpFactory(log, new SnappyTimeSet(), onClientCreated: client => { client.SetBasicAuthentication(tokens[0], tokens[1]); }); } + + public class SnappyTimeSet : IWebCallTimeSet + { + public TimeSpan HttpCallRetryDelay() + { + return TimeSpan.FromSeconds(1.0); + } + + public TimeSpan HttpCallTimeout() + { + return TimeSpan.FromSeconds(3.0); + } + + public TimeSpan HttpRetryTimeout() + { + return TimeSpan.FromSeconds(12.0); + } + } } } diff --git a/Tools/BiblioTech/Commands/CheckDownloadCommand.cs b/Tools/BiblioTech/Commands/CheckDownloadCommand.cs index 0e1aa563..d2119833 100644 --- a/Tools/BiblioTech/Commands/CheckDownloadCommand.cs +++ b/Tools/BiblioTech/Commands/CheckDownloadCommand.cs @@ -18,7 +18,7 @@ namespace BiblioTech.Commands } public override string Name => "checkdownload"; - public override string StartingMessage => RandomBusyMessage.Get(); + public override string StartingMessage => "Connecting to the testnet... Please be patient... " + RandomBusyMessage.Get(); public override string Description => "Checks the download connectivity of your Codex node."; public override CommandOption[] Options => [contentOption]; diff --git a/Tools/BiblioTech/Commands/CheckUploadCommand.cs b/Tools/BiblioTech/Commands/CheckUploadCommand.cs index b1589b5c..51d6d394 100644 --- a/Tools/BiblioTech/Commands/CheckUploadCommand.cs +++ b/Tools/BiblioTech/Commands/CheckUploadCommand.cs @@ -18,7 +18,7 @@ namespace BiblioTech.Commands } public override string Name => "checkupload"; - public override string StartingMessage => RandomBusyMessage.Get(); + public override string StartingMessage => "Connecting to the testnet... Please be patient... " + RandomBusyMessage.Get(); public override string Description => "Checks the upload connectivity of your Codex node."; public override CommandOption[] Options => [cidOption]; diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index 0ce84bde..f0d63758 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -88,7 +88,7 @@ namespace BiblioTech client = new DiscordSocketClient(); client.Log += ClientLog; - var checkRepo = new CheckRepo(Config); + var checkRepo = new CheckRepo(Log, Config); var codexWrapper = new CodexWrapper(Log, Config); var checker = new CodexTwoWayChecker(Log, Config, checkRepo, codexWrapper); var notifyCommand = new NotifyCommand();