Skip to content

Commit ddf98c9

Browse files
committed
* now will dispose instances of ByteArrayContent & MultipartFormDataContent that inherits HttpContent @ ClientRequester.PostProtoBuf()
* implement `IDisposable` pattern to dispose `System.Timers.Timer` @ ClientRequesterTcs.cs & WithLogTrace.cs * fix `AV1250: Method returns the result of a call to 'Concat', which uses deferred execution` @ `JointRecognizer.RecognizeMatrices()` * suppress some violations of Roslyn analyzer rules * suppress `MA0001` & `MA0002` @ GlobalSuppressions.cs @ c#
1 parent c0378a7 commit ddf98c9

File tree

8 files changed

+37
-12
lines changed

8 files changed

+37
-12
lines changed

c#/GlobalSuppressions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
[assembly: SuppressMessage("Style", "MA0007:Add a comma after the last value")]
4545
[assembly: SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods")]
4646
[assembly: SuppressMessage("Usage", "CC0057:Unused parameters")]
47+
[assembly: SuppressMessage("Usage", "MA0001:StringComparison is missing")]
48+
[assembly: SuppressMessage("Usage", "MA0002:IEqualityComparer<string> or IComparer<string> is missing", Justification = "https://stackoverflow.com/questions/56478995/default-stringcomparer-used-by-dictionarystring-t")]
4749
[assembly: SuppressMessage("Usage", "MA0004:Use Task.ConfigureAwait")]
4850
[assembly: SuppressMessage("Usage", "MA0006:Use String.Equals instead of equality operator")]
4951
[assembly: SuppressMessage("Usage", "MA0015:Specify the parameter name in ArgumentException")]

c#/crawler/src/Helper.cs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static byte[]? SerializedProtoBufWrapperOrNullIfEmpty<T>
2323
contents == null ? null : new() {Value = {contents}};
2424

2525
public static void GetNowTimestamp(out Time now) => now = GetNowTimestamp();
26+
[SuppressMessage("Maintainability", "AV1551:Method overload should call another overload")]
2627
public static Time GetNowTimestamp() => (Time)DateTimeOffset.Now.ToUnixTimeSeconds();
2728
}
2829
public abstract partial class Helper

c#/crawler/src/Tieba/ClientRequester.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ private async Task<HttpResponseMessage> PostJson(
9999
acc += pair.Key + '=' + pair.Value;
100100
return acc;
101101
}) + "tiebaclient!!!";
102+
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
102103
var signMd5 = BitConverter.ToString(MD5.HashData(Encoding.UTF8.GetBytes(sign))).Replace("-", "");
104+
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms
103105
postData.Add(KeyValuePair.Create("sign", signMd5));
104106

105107
return await Post(async http =>
@@ -123,15 +125,20 @@ private async Task<HttpResponseMessage> PostProtoBuf<TRequest>(
123125
commonParamSetter(requestParam, new() {ClientVersion = clientVersion, ClientType = 2});
124126

125127
// https://github.com/dotnet/runtime/issues/22996 http://test.greenbytes.de/tech/tc2231
126-
var protoBufFile = new ByteArrayContent(requestParam.ToByteArray());
128+
using var protoBufFile = new ByteArrayContent(requestParam.ToByteArray());
127129
protoBufFile.Headers.Add("Content-Disposition", "form-data; name=\"data\"; filename=\"file\"");
128-
var content = new MultipartFormDataContent {protoBufFile};
130+
#pragma warning disable IDE0028 // Simplify collection initialization
131+
using var content = new MultipartFormDataContent();
132+
#pragma warning restore IDE0028 // Simplify collection initialization
133+
content.Add(protoBufFile);
129134

130135
// https://stackoverflow.com/questions/30926645/httpcontent-boundary-double-quotes
131136
var boundary = content.Headers.ContentType?.Parameters.First(header => header.Name == "boundary");
132137
if (boundary != null) boundary.Value = boundary.Value?.Replace("\"", "");
133138

139+
#pragma warning disable CC0008 // Use object initializer
134140
using var request = new HttpRequestMessage(HttpMethod.Post, url);
141+
#pragma warning restore CC0008 // Use object initializer
135142
request.Content = content;
136143
_ = request.Headers.UserAgent.TryParseAdd($"bdtb for Android {clientVersion}");
137144
request.Headers.Add("x_bd_data_type", "protobuf");

c#/crawler/src/Tieba/ClientRequesterTcs.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace tbm.Crawler.Tieba;
22

3-
public class ClientRequesterTcs : WithLogTrace
3+
public sealed class ClientRequesterTcs : WithLogTrace
44
{
55
private readonly ILogger<ClientRequesterTcs> _logger;
66
private readonly IConfigurationSection _config;
@@ -41,6 +41,12 @@ private double MaxRps
4141
}
4242
}
4343

44+
public override void Dispose()
45+
{
46+
_timer.Dispose();
47+
base.Dispose();
48+
}
49+
4450
public void Increase() => MaxRps = Math.Min(
4551
_config.GetValue("LimitRps:1", 1000),
4652
MaxRps + _config.GetValue("DeltaRps:0", 0.01));

c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public abstract class BaseSaver<TPost, TBaseRevision>(
1212
where TBaseRevision : class, IRevision
1313
{
1414
protected delegate void PostSaveEventHandler();
15+
[SuppressMessage("Design", "MA0046:Use EventHandler<T> to declare events")]
1516
protected event PostSaveEventHandler PostSaveEvent = () => { };
1617

1718
public virtual FieldChangeIgnoranceDelegates UserFieldChangeIgnorance =>

c#/crawler/src/WithLogTrace.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
namespace tbm.Crawler;
22

3-
public abstract class WithLogTrace
3+
#pragma warning disable S3881 // "IDisposable" should be implemented correctly
4+
public abstract class WithLogTrace : IDisposable
5+
#pragma warning restore S3881 // "IDisposable" should be implemented correctly
46
{
57
private readonly IConfigurationSection _config;
6-
private readonly Timer _timerLogTrace = new() {Enabled = true};
8+
private readonly Timer _timer = new() {Enabled = true};
79

810
protected WithLogTrace(IConfiguration config, string section)
911
{
1012
_config = config.GetSection(section).GetSection("LogTrace");
11-
_timerLogTrace.Interval = _config.GetValue("LogIntervalMs", 1000);
12-
_timerLogTrace.Elapsed += (_, _) => LogTrace();
13+
_timer.Interval = _config.GetValue("LogIntervalMs", 1000);
14+
_timer.Elapsed += (_, _) => LogTrace();
15+
}
16+
17+
public virtual void Dispose()
18+
{
19+
GC.SuppressFinalize(this);
20+
_timer.Dispose();
1321
}
1422

1523
protected abstract void LogTrace();
1624

1725
protected bool ShouldLogTrace()
1826
{
1927
if (!_config.GetValue("Enabled", false)) return false;
20-
_timerLogTrace.Interval = _config.GetValue("LogIntervalMs", 1000);
28+
_timer.Interval = _config.GetValue("LogIntervalMs", 1000);
2129
return true;
2230
}
2331
}

c#/imagePipeline/src/Consumer/OcrConsumer.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ protected override IEnumerable<ImageId> ConsumeInternal(
3131
return KeyValuePair.Create(new ImageKey(i.ImageId, i.FrameIndex), mat);
3232
}).ToList();
3333
var recognizedEithers = _recognizer
34-
.RecognizeMatrices(matricesKeyByImageKey.Rights().ToDictionary(), stoppingToken)
35-
.ToList();
34+
.RecognizeMatrices(matricesKeyByImageKey.Rights().ToDictionary(), stoppingToken);
3635
var recognizedFailedImagesId = recognizedEithers.Lefts().ToList();
3736
var recognizedResults = recognizedEithers
3837

c#/imagePipeline/src/Ocr/JointRecognizer.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class JointRecognizer(
3434
public async Task InitializePaddleOcr(CancellationToken stoppingToken = default) =>
3535
await _paddleOcrRecognizerAndDetector.Initialize(stoppingToken);
3636

37-
public IEnumerable<Either<ImageId, IRecognitionResult>> RecognizeMatrices
37+
public List<Either<ImageId, IRecognitionResult>> RecognizeMatrices
3838
(Dictionary<ImageKey, Mat> matricesKeyByImageKey, CancellationToken stoppingToken = default)
3939
{
4040
var recognizedEithersViaPaddleOcr = _paddleOcrRecognizerAndDetector
@@ -63,7 +63,8 @@ public IEnumerable<Either<ImageId, IRecognitionResult>> RecognizeMatrices
6363
.Lefts()
6464
.Concat(detectedEithers.Lefts())
6565
.Concat(recognizedEithersViaTesseract.Lefts())
66-
.Select(Either<ImageId, IRecognitionResult>.Left));
66+
.Select(Either<ImageId, IRecognitionResult>.Left))
67+
.ToList();
6768
}
6869

6970
public Dictionary<ImageKey, string> GetRecognizedTextLines

0 commit comments

Comments
 (0)