Skip to content

Commit c5ff76d

Browse files
committed
Improving check command responsiveness
1 parent 0c74aca commit c5ff76d

File tree

6 files changed

+73
-33
lines changed

6 files changed

+73
-33
lines changed

Tools/BiblioTech/CodexChecking/CheckRepo.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
1-
using Newtonsoft.Json;
1+
using Logging;
2+
using Newtonsoft.Json;
3+
using Utils;
24

35
namespace BiblioTech.CodexChecking
46
{
57
public class CheckRepo
68
{
79
private const string modelFilename = "model.json";
10+
private readonly ILog log;
811
private readonly Configuration config;
912
private readonly object _lock = new object();
1013
private CheckRepoModel? model = null;
1114

12-
public CheckRepo(Configuration config)
15+
public CheckRepo(ILog log, Configuration config)
1316
{
17+
this.log = log;
1418
this.config = config;
1519
}
1620

1721
public CheckReport GetOrCreate(ulong userId)
1822
{
1923
lock (_lock)
2024
{
21-
if (model == null) LoadModel();
25+
var sw = System.Diagnostics.Stopwatch.StartNew();
26+
try
27+
{
28+
if (model == null) LoadModel();
2229

23-
var existing = model.Reports.SingleOrDefault(r => r.UserId == userId);
24-
if (existing == null)
30+
var existing = model.Reports.SingleOrDefault(r => r.UserId == userId);
31+
if (existing == null)
32+
{
33+
var newEntry = new CheckReport
34+
{
35+
UserId = userId,
36+
};
37+
model.Reports.Add(newEntry);
38+
SaveChanges();
39+
return newEntry;
40+
}
41+
return existing;
42+
}
43+
finally
2544
{
26-
var newEntry = new CheckReport
45+
var elapsed = sw.Elapsed;
46+
if (elapsed > TimeSpan.FromMilliseconds(500))
2747
{
28-
UserId = userId,
29-
};
30-
model.Reports.Add(newEntry);
31-
SaveChanges();
32-
return newEntry;
48+
log.Log($"Warning {nameof(GetOrCreate)} took {Time.FormatDuration(elapsed)}");
49+
}
3350
}
34-
return existing;
3551
}
3652
}
3753

Tools/BiblioTech/CodexChecking/CodexTwoWayChecker.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task StartDownloadCheck(ICheckResponseHandler handler, ulong userId
4343
repo.SaveChanges();
4444
}
4545

46-
var cid = UploadData(check.UniqueData);
46+
var cid = await UploadData(check.UniqueData);
4747
await handler.GiveCidToUser(cid);
4848
}
4949

@@ -87,7 +87,7 @@ public async Task VerifyUploadCheck(ICheckResponseHandler handler, ulong userId,
8787
return;
8888
}
8989

90-
var manifest = GetManifest(receivedCid);
90+
var manifest = await GetManifest(receivedCid);
9191
if (manifest == null)
9292
{
9393
await handler.CouldNotDownloadCid();
@@ -96,7 +96,7 @@ public async Task VerifyUploadCheck(ICheckResponseHandler handler, ulong userId,
9696

9797
if (IsManifestLengthCompatible(handler, check, manifest))
9898
{
99-
if (IsContentCorrect(handler, check, receivedCid))
99+
if (await IsContentCorrect(handler, check, receivedCid))
100100
{
101101
await CheckNowCompleted(handler, check, userId, "UploadCheck");
102102
return;
@@ -120,7 +120,7 @@ private bool IsUniqueDataStale(TransferCheck check)
120120
check.CompletedUtc < expiry;
121121
}
122122

123-
private string UploadData(string uniqueData)
123+
private async Task<string> UploadData(string uniqueData)
124124
{
125125
var filePath = Path.Combine(config.ChecksDataPath, Guid.NewGuid().ToString());
126126

@@ -129,7 +129,7 @@ private string UploadData(string uniqueData)
129129
File.WriteAllText(filePath, uniqueData);
130130
var file = new TrackedFile(log, filePath, "checkData");
131131

132-
return codexWrapper.OnCodex(node =>
132+
return await codexWrapper.OnCodex(node =>
133133
{
134134
return node.UploadFile(file).Id;
135135
});
@@ -145,11 +145,11 @@ private string UploadData(string uniqueData)
145145
}
146146
}
147147

148-
private Manifest? GetManifest(string receivedCid)
148+
private async Task<Manifest?> GetManifest(string receivedCid)
149149
{
150150
try
151151
{
152-
return codexWrapper.OnCodex(node =>
152+
return await codexWrapper.OnCodex(node =>
153153
{
154154
return node.DownloadManifestOnly(new ContentId(receivedCid)).Manifest;
155155
});
@@ -172,11 +172,11 @@ private bool IsManifestLengthCompatible(ICheckResponseHandler handler, TransferC
172172
manifestLength < (dataLength + 1);
173173
}
174174

175-
private bool IsContentCorrect(ICheckResponseHandler handler, TransferCheck check, string receivedCid)
175+
private async Task<bool> IsContentCorrect(ICheckResponseHandler handler, TransferCheck check, string receivedCid)
176176
{
177177
try
178178
{
179-
var content = codexWrapper.OnCodex(node =>
179+
var content = await codexWrapper.OnCodex(node =>
180180
{
181181
var file = node.DownloadContent(new ContentId(receivedCid));
182182
if (file == null) return string.Empty;

Tools/BiblioTech/CodexChecking/CodexWrapper.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,26 @@ public CodexWrapper(ILog log, Configuration config)
2323
factory = new CodexNodeFactory(log, httpFactory, dataDir: config.DataPath);
2424
}
2525

26-
public void OnCodex(Action<ICodexNode> action)
26+
public async Task OnCodex(Action<ICodexNode> action)
2727
{
28-
lock (codexLock)
28+
await Task.Run(() =>
2929
{
30-
action(Get());
31-
}
30+
lock (codexLock)
31+
{
32+
action(Get());
33+
}
34+
});
3235
}
3336

34-
public T OnCodex<T>(Func<ICodexNode, T> func)
37+
public async Task<T> OnCodex<T>(Func<ICodexNode, T> func)
3538
{
36-
lock (codexLock)
39+
return await Task<T>.Run(() =>
3740
{
38-
return func(Get());
39-
}
41+
lock (codexLock)
42+
{
43+
return func(Get());
44+
}
45+
});
4046
}
4147

4248
private ICodexNode Get()
@@ -76,10 +82,28 @@ private HttpFactory CreateHttpFactory()
7682
var tokens = config.CodexEndpointAuth.Split(':');
7783
if (tokens.Length != 2) throw new Exception("Expected '<username>:<password>' in CodexEndpointAuth parameter.");
7884

79-
return new HttpFactory(log, onClientCreated: client =>
85+
return new HttpFactory(log, new SnappyTimeSet(), onClientCreated: client =>
8086
{
8187
client.SetBasicAuthentication(tokens[0], tokens[1]);
8288
});
8389
}
90+
91+
public class SnappyTimeSet : IWebCallTimeSet
92+
{
93+
public TimeSpan HttpCallRetryDelay()
94+
{
95+
return TimeSpan.FromSeconds(1.0);
96+
}
97+
98+
public TimeSpan HttpCallTimeout()
99+
{
100+
return TimeSpan.FromSeconds(3.0);
101+
}
102+
103+
public TimeSpan HttpRetryTimeout()
104+
{
105+
return TimeSpan.FromSeconds(12.0);
106+
}
107+
}
84108
}
85109
}

Tools/BiblioTech/Commands/CheckDownloadCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public CheckDownloadCommand(CodexTwoWayChecker checker)
1818
}
1919

2020
public override string Name => "checkdownload";
21-
public override string StartingMessage => RandomBusyMessage.Get();
21+
public override string StartingMessage => "Connecting to the testnet... Please be patient... " + RandomBusyMessage.Get();
2222
public override string Description => "Checks the download connectivity of your Codex node.";
2323
public override CommandOption[] Options => [contentOption];
2424

Tools/BiblioTech/Commands/CheckUploadCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public CheckUploadCommand(CodexTwoWayChecker checker)
1818
}
1919

2020
public override string Name => "checkupload";
21-
public override string StartingMessage => RandomBusyMessage.Get();
21+
public override string StartingMessage => "Connecting to the testnet... Please be patient... " + RandomBusyMessage.Get();
2222
public override string Description => "Checks the upload connectivity of your Codex node.";
2323
public override CommandOption[] Options => [cidOption];
2424

Tools/BiblioTech/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private async Task StartDiscordBot()
8888
client = new DiscordSocketClient();
8989
client.Log += ClientLog;
9090

91-
var checkRepo = new CheckRepo(Config);
91+
var checkRepo = new CheckRepo(Log, Config);
9292
var codexWrapper = new CodexWrapper(Log, Config);
9393
var checker = new CodexTwoWayChecker(Log, Config, checkRepo, codexWrapper);
9494
var notifyCommand = new NotifyCommand();

0 commit comments

Comments
 (0)