Skip to content

Refactor Confirmation Pipe and HeartBeat start #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion RabbitMQ.Stream.Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public int Write(Span<byte> span)

public class Client : IClient
{
private bool isClosed = false;
private bool isClosed = true;

private readonly TimeSpan defaultTimeout = TimeSpan.FromSeconds(10);

Expand Down Expand Up @@ -165,6 +165,11 @@ private Client(ClientParameters parameters)
IsClosed = false;
}

private void StartHeartBeat()
{
_heartBeatHandler.Start();
}

public delegate Task ConnectionCloseHandler(string reason);

public event ConnectionCloseHandler ConnectionClosed;
Expand Down Expand Up @@ -228,6 +233,8 @@ await client.Publish(new TuneRequest(0,
client.ConnectionProperties = open.ConnectionProperties;

client.correlationId = 100;
// start heart beat only when the client is connected
client.StartHeartBeat();
return client;
}

Expand Down
7 changes: 6 additions & 1 deletion RabbitMQ.Stream.Client/HeartBeatHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public HeartBeatHandler(Func<ValueTask<bool>> sendHeartbeatFunc,
// }
if (heartbeat > 0)
{
_timer.Enabled = true;
_timer.Enabled = false;
_timer.Interval = heartbeat * 1000;
_timer.Elapsed += (_, _) =>
{
Expand Down Expand Up @@ -72,6 +72,11 @@ internal void Close()
_timer.Close();
}

internal void Start()
{
_timer.Enabled = true;
}

internal bool IsActive()
{
return _timer.Enabled;
Expand Down
4 changes: 3 additions & 1 deletion RabbitMQ.Stream.Client/Reliable/ReliableProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private ReliableProducer(ReliableProducerConfig reliableProducerConfig)
reliableProducerConfig.ConfirmationHandler,
reliableProducerConfig.TimeoutMessageAfter
);
_confirmationPipe.Start();
}

public static async Task<ReliableProducer> CreateReliableProducer(ReliableProducerConfig reliableProducerConfig)
Expand Down Expand Up @@ -114,6 +113,9 @@ protected override async Task GetNewReliable(bool boot)
// Init the publishing id
Interlocked.Exchange(ref _publishingId,
await _producer.GetLastPublishingId());

// confirmation Pipe can start only if the producer is ready
_confirmationPipe.Start();
}
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void HeartBeatRaiseClose()
return null;
},
1);

hBeatHandler.Start();
var r = testPassed.Task.Wait(6_000);
Assert.True(r);
Assert.False(hBeatHandler.IsActive());
Expand Down