Skip to content

Commit 2aa8c23

Browse files
committed
delay random seconds after connection failed
1 parent 2d41e95 commit 2aa8c23

File tree

6 files changed

+54
-10
lines changed

6 files changed

+54
-10
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace SocketIOClient.V2;
4+
5+
public interface IRandom
6+
{
7+
int Next(int max);
8+
}
9+
10+
public class DefaultRandom : IRandom
11+
{
12+
private readonly Random _random = new();
13+
14+
public int Next(int max)
15+
{
16+
return _random.Next(max);
17+
}
18+
}

src/SocketIOClient/V2/DefaultSessionFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
namespace SocketIOClient.V2;
1010

11+
public interface ISessionFactory
12+
{
13+
ISession New(EngineIO eio);
14+
}
15+
1116
public class DefaultSessionFactory : ISessionFactory
1217
{
1318
public ISession New(EngineIO eio)

src/SocketIOClient/V2/ISessionFactory.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/SocketIOClient/V2/SocketIO.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public SocketIO(Uri uri, SocketIOOptions options)
2121
_serverUri = uri;
2222
Options = options;
2323
SessionFactory = new DefaultSessionFactory();
24+
Random = new DefaultRandom();
2425
}
2526

2627
public SocketIO(Uri uri) : this(uri, new SocketIOOptions())
@@ -37,6 +38,7 @@ public SocketIO(Uri uri, SocketIOOptions options)
3738
public int PacketId { get; private set; }
3839
public bool Connected { get; private set; }
3940
public string Id { get; private set; }
41+
public IRandom Random { get; set; }
4042

4143
public string Namespace { get; private set; }
4244

@@ -92,6 +94,8 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
9294
{
9395
throw ex;
9496
}
97+
var delay = Random.Next(Options.ReconnectionDelayMax);
98+
await Task.Delay(delay, CancellationToken.None);
9599
}
96100
}
97101
}

src/SocketIOClient/V2/SocketIOOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class SocketIOOptions
66
{
77
public EngineIO EIO { get; set; }
88
public TimeSpan ConnectionTimeout { get; set; }
9-
public bool Reconnection { get; set; }
10-
public int ReconnectionAttempts { get; set; }
9+
public bool Reconnection { get; set; } = true;
10+
public int ReconnectionAttempts { get; set; } = int.MaxValue;
11+
public int ReconnectionDelayMax { get; set; } = 5000;
1112
}

tests/SocketIOClient.UnitTests/V2/SocketIOTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ public SocketIOTests()
1515
_session = Substitute.For<ISession>();
1616
var sessionFactory = Substitute.For<ISessionFactory>();
1717
sessionFactory.New(Arg.Any<EngineIO>()).Returns(_session);
18+
_random = Substitute.For<IRandom>();
1819
_io = new SocketIOClient.V2.SocketIO("http://localhost:3000")
1920
{
2021
SessionFactory = sessionFactory,
22+
Random = _random,
23+
Options =
24+
{
25+
Reconnection = false,
26+
},
2127
};
2228
}
2329

2430
private readonly SocketIOClient.V2.SocketIO _io;
2531
private readonly ISession _session;
32+
private readonly IRandom _random;
2633

2734
[Fact]
2835
public void NothingCalled_DefaultValues()
@@ -141,6 +148,23 @@ await _io
141148
.ThrowAsync<OperationCanceledException>();
142149
}
143150

151+
[Fact]
152+
public async Task ConnectAsyncCancellationToken_CancelAfter200ms_ThrowConnectionException()
153+
{
154+
_io.Options.Reconnection = true;
155+
_random.Next(Arg.Any<int>()).Returns(10);
156+
157+
await _io
158+
.Invoking(async x =>
159+
{
160+
using var cts = new CancellationTokenSource();
161+
cts.CancelAfter(TimeSpan.FromMilliseconds(100));
162+
await x.ConnectAsync(cts.Token);
163+
})
164+
.Should()
165+
.ThrowAsync<OperationCanceledException>();
166+
}
167+
144168
[Fact]
145169
public async Task EmitAsync_AckEventAction_PacketIdIncrementBy1()
146170
{

0 commit comments

Comments
 (0)