Skip to content

Commit

Permalink
Try to fix NATS reconnection publish issue after restarting server. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
yang-xiaodong committed Jun 11, 2024
1 parent 1cd3f40 commit 51453d5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/DotNetCore.CAP.NATS/CAP.NATSOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class NATSOptions
/// Gets or sets the server url/urls used to connect to the NATs server.
/// </summary>
/// <remarks>This may contain username/password information.</remarks>
public string Servers { get; set; } = "nats://localhost:4222";
public string Servers { get; set; } = "nats://127.0.0.1:4222";

/// <summary>
/// connection pool size, default is 10
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetCore.CAP.NATS/DotNetCore.CAP.NATS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NATS.Client" Version="1.1.1" />
<PackageReference Include="NATS.Client" Version="1.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 6 additions & 3 deletions src/DotNetCore.CAP.NATS/IConnectionPool.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ConnectionPool : IConnectionPool, IDisposable
{
private readonly NATSOptions _options;
private readonly ConcurrentQueue<IConnection> _connectionPool;

private readonly ConnectionFactory _connectionFactory;
private int _pCount;
private int _maxSize;
Expand Down Expand Up @@ -54,14 +55,17 @@ public IConnection RentConnection()

public bool Return(IConnection connection)
{
if (Interlocked.Increment(ref _pCount) <= _maxSize)
if (Interlocked.Increment(ref _pCount) <= _maxSize && connection.State == ConnState.CONNECTED)
{
_connectionPool.Enqueue(connection);

return true;
}

connection.Dispose();
if (!connection.IsReconnecting())
{
connection.Dispose();
}

Interlocked.Decrement(ref _pCount);

Expand All @@ -75,7 +79,6 @@ public void Dispose()
while (_connectionPool.TryDequeue(out var context))
{
context.Dispose();

}
}
}
Expand Down

0 comments on commit 51453d5

Please sign in to comment.