Skip to content

Commit 1c6e8e7

Browse files
committed
一部の配信サイトで一定時間内に一定回数以上の自動再接続を行わないようにした
1 parent 38b0135 commit 1c6e8e7

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

SitePluginCommon/AutoReconnection/ConnectionManager.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@
66

77
namespace SitePluginCommon.AutoReconnection
88
{
9+
public class ReconnectionCounter
10+
{
11+
Queue<DateTime> _list = new Queue<DateTime>();
12+
/// <summary>
13+
///
14+
/// </summary>
15+
/// <returns>一定時間内のAdd回数が一定回数以下だったらtrue</returns>
16+
public bool Add(DateTime now)
17+
{
18+
const int n = 5;
19+
const int range = 60 * 1000;
20+
_list.Enqueue(now);
21+
if (_list.Count <= n)
22+
{
23+
return true;
24+
}
25+
//直近のn個より前のデータを全て消す
26+
for (int i = _list.Count - n; i > 0; i--)
27+
{
28+
_list.Dequeue();
29+
}
30+
//今あるn個のデータが全て基準時刻移行のものだったらfalseを返す
31+
var baseTime = now.AddMilliseconds(-range);
32+
var b = _list.All(h => h > baseTime);
33+
return !b;
34+
}
35+
public void Reset()
36+
{
37+
_list.Clear();
38+
}
39+
}
940
/// <summary>
1041
/// 複数のIProviderを管理
1142
/// </summary>
@@ -40,8 +71,10 @@ public async Task<ProviderFinishReason> ConnectAsync(IEnumerable<IProvider> grou
4071
{
4172
throw new ArgumentException();
4273
}
74+
_reconnectionCounterDict.Clear();
4375
foreach (var provider in group)
4476
{
77+
_reconnectionCounterDict.Add(provider, new ReconnectionCounter());
4578
provider.Start();
4679
}
4780
var workingProviders = new List<IProvider>(group);
@@ -65,8 +98,11 @@ public async Task<ProviderFinishReason> ConnectAsync(IEnumerable<IProvider> grou
6598
if (!provider.Master.IsFinished)
6699
{
67100
//エラーによる終了が一定時間に一定回数発生したら継続しないようにしたい。
68-
provider.Start();
69-
workingProviders.Add(provider);
101+
if (_reconnectionCounterDict[provider].Add(DateTime.Now))
102+
{
103+
provider.Start();
104+
workingProviders.Add(provider);
105+
}
70106
}
71107
}
72108
}
@@ -98,5 +134,6 @@ public ConnectionManager(ILogger logger)
98134
{
99135
_logger = logger;
100136
}
137+
Dictionary<IProvider, ReconnectionCounter> _reconnectionCounterDict = new Dictionary<IProvider, ReconnectionCounter>();
101138
}
102139
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
using SitePluginCommon.AutoReconnection;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace SitePluginCommonTests
10+
{
11+
internal class AutoReconnectionTests
12+
{
13+
[Test]
14+
public void Test()
15+
{
16+
var counter = new ReconnectionCounter();
17+
Assert.IsTrue(counter.Add(DateTime.Now));
18+
Assert.IsTrue(counter.Add(DateTime.Now));
19+
Assert.IsTrue(counter.Add(DateTime.Now));
20+
Assert.IsTrue(counter.Add(DateTime.Now));
21+
Assert.IsTrue(counter.Add(DateTime.Now));
22+
Assert.IsFalse(counter.Add(DateTime.Now));
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)