Skip to content

Commit 49b6e82

Browse files
committed
Increase Iqdb timeout; async tweaks
1 parent 1b76d09 commit 49b6e82

File tree

4 files changed

+73
-40
lines changed

4 files changed

+73
-40
lines changed

SmartImage.Lib/Engines/Impl/Search/Ascii2DEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected override string[] ErrorBodyMessages
4747

4848
public const string MAIN_URL = "https://ascii2d.net/search/url/";
4949

50-
public Ascii2DEngine() : base(ALT_URL)
50+
public Ascii2DEngine() : base(MAIN_URL)
5151
{
5252
Timeout = TimeSpan.FromSeconds(30);
5353
MaxSize = 10_000_000;

SmartImage.Lib/Engines/Impl/Search/IqdbEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected IqdbEngine(string b) : base(b)
3838
{
3939
MaxSize = MAX_FILE_SIZE; // NOTE: assuming IQDB uses kilobytes instead of kibibytes
4040

41-
Timeout = TimeSpan.FromSeconds(25);
41+
Timeout = TimeSpan.FromSeconds(90);
4242
}
4343

4444
private const int MAX_FILE_SIZE = 8_388_608;

SmartImage.Lib/SearchClient.cs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
209209
}
210210
catch (Exception e) {
211211
s_logger.LogError(e, "Run search error");
212-
212+
213213
Debugger.Break();
214214
}
215215

@@ -304,21 +304,13 @@ public async ValueTask LoadEnginesAsync(CancellationToken token = default)
304304

305305
Engines = BaseSearchEngine.GetSelectedEngines(Config.SearchEngines).ToFrozenSet();
306306

307-
if (Config.ReadCookies) {
308-
309-
try {
310-
Config.CookiesProvider = ICookiesProvider.GetProvider();
307+
ValueTask<bool> readCookies;
308+
ValueTask<bool> loadFlareSolverr;
309+
Task applyConfig;
311310

312-
await ((BrowserCookiesProvider) Config.CookiesProvider).OpenAsync();
313-
}
314-
catch (Exception e) {
315-
s_logger.LogError(e, "Error reading cookies");
316-
Config.ReadCookies = false;
317-
Config.CookiesProvider.Dispose();
318-
}
319-
}
311+
readCookies = Config.TryReadCookiesAsync();
320312

321-
await Parallel.ForEachAsync(Engines, token, async (bse, cancellationToken) =>
313+
applyConfig = Parallel.ForEachAsync(Engines, token, async (bse, cancellationToken) =>
322314
{
323315
if (bse is ISearchConfigReceiver cfg) {
324316
s_logger.LogTrace("Applying config to {Engine}", bse.Name);
@@ -327,26 +319,11 @@ await Parallel.ForEachAsync(Engines, token, async (bse, cancellationToken) =>
327319
});
328320

329321

330-
if (Config.FlareSolverr && !FlareSolverrClient.Value.IsInitialized) {
331-
332-
var ok = await FlareSolverrClient.Value.ApplyConfigAsync(Config, token);
322+
loadFlareSolverr = Config.TryLoadFlareSolverrAsync(token);
333323

334-
if (!ok) {
335-
Debugger.Break();
336-
}
337-
else {
338-
// Ensure FlareSolverr
339-
340-
try {
341-
var idx = await FlareSolverrClient.Value.Clearance.Solverr.GetIndexAsync();
342-
}
343-
catch (Exception e) {
344-
s_logger.LogError(e, "FlareSolverr error");
345-
Config.FlareSolverr = false;
346-
FlareSolverrClient.Value.Dispose();
347-
}
348-
}
349-
}
324+
await readCookies;
325+
await applyConfig;
326+
await loadFlareSolverr;
350327

351328
// CookiesManager.Instance.Dispose();
352329

SmartImage.Lib/SearchConfig.cs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
using Kantan.Model.MemberIndex;
88
using Kantan.Utilities;
99
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.Logging;
11+
using SmartImage.Lib.Clients;
1012
using SmartImage.Lib.Cookies;
1113
using SmartImage.Lib.Engines;
1214
using SmartImage.Lib.Engines.Impl.Search;
1315
using SmartImage.Lib.Engines.Impl.Upload;
1416
using SmartImage.Lib.Results.Data;
17+
using SmartImage.Lib.Utilities.Diagnostics;
1518
using Configuration = System.Configuration.Configuration;
1619
using ConfigurationManager = System.Configuration.ConfigurationManager;
1720
using ConfigurationSection = System.Configuration.ConfigurationSection;
@@ -21,7 +24,7 @@ namespace SmartImage.Lib;
2124
public sealed class SearchConfig : INotifyPropertyChanged
2225
{
2326

24-
#region Defaults
27+
#region Defaults
2528

2629
/// <summary>
2730
/// Default value for <see cref="SearchEngines"/>
@@ -57,7 +60,7 @@ public sealed class SearchConfig : INotifyPropertyChanged
5760

5861
public const UploadEngineOptions UPLOAD_ENGINE_DEFAULT = UploadEngineOptions.Pomf;
5962

60-
#endregion
63+
#endregion
6164

6265
/// <summary>
6366
/// Engines used to search.
@@ -196,16 +199,69 @@ public UploadEngineOptions UploadEngine
196199

197200
public static readonly SearchConfig Default = new();
198201

202+
private static readonly ILogger s_logger = AppSupport.Factory.CreateLogger(nameof(SearchClient));
203+
204+
public static readonly Configuration Configuration =
205+
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
206+
199207
public SearchConfig()
200208
{
201209
PropertyChanged += static (sender, args) =>
202210
{
203-
Trace.WriteLine($"Changed {args.PropertyName}", nameof(SearchConfig));
211+
//
212+
s_logger.LogTrace("Changed {PropName}", args.PropertyName);
204213
};
205214
}
206215

207-
public static readonly Configuration Configuration =
208-
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
216+
217+
internal async ValueTask<bool> TryReadCookiesAsync()
218+
{
219+
var ok = false;
220+
221+
if (ReadCookies) {
222+
try {
223+
CookiesProvider = ICookiesProvider.GetProvider();
224+
225+
await ((BrowserCookiesProvider) CookiesProvider).OpenAsync();
226+
ok = true;
227+
}
228+
catch (Exception e) {
229+
s_logger.LogError(e, "Error reading cookies");
230+
ReadCookies = ok;
231+
CookiesProvider.Dispose();
232+
}
233+
}
234+
235+
return ok;
236+
}
237+
238+
internal async ValueTask<bool> TryLoadFlareSolverrAsync(CancellationToken token)
239+
{
240+
bool ok = false;
241+
242+
if (this.FlareSolverr && !FlareSolverrClient.Value.IsInitialized) {
243+
244+
ok = await FlareSolverrClient.Value.ApplyConfigAsync(this, token);
245+
246+
if (!ok) {
247+
Debugger.Break();
248+
}
249+
else {
250+
// Ensure FlareSolverr
251+
252+
try {
253+
var idx = await FlareSolverrClient.Value.Clearance.Solverr.GetIndexAsync();
254+
}
255+
catch (Exception e) {
256+
s_logger.LogError(e, "FlareSolverr error");
257+
this.FlareSolverr = ok;
258+
FlareSolverrClient.Value.Dispose();
259+
}
260+
}
261+
}
262+
263+
return ok;
264+
}
209265

210266
private bool Set<T>(T s = default, [CMN] string name = default)
211267
{

0 commit comments

Comments
 (0)