Skip to content

Commit

Permalink
Add Healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa committed Oct 2, 2024
1 parent 1957051 commit 62d3399
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ protos

*.sln.DotSettings.user
output
.vs
17 changes: 16 additions & 1 deletion src/Qdrant.Client/QdrantClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.InteropServices;
using Google.Protobuf.Collections;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Qdrant.Client.Grpc;
Expand Down Expand Up @@ -93,7 +94,6 @@ private QdrantClient(
_collectionsClient = grpcClient.Collections;
_pointsClient = grpcClient.Points;
_snapshotsClient = grpcClient.Snapshots;

_grpcTimeout = grpcTimeout;
_logger = loggerFactory?.CreateLogger("Qdrant.Client") ?? NullLogger.Instance;
}
Expand Down Expand Up @@ -4288,6 +4288,21 @@ public async Task<DeleteShardKeyResponse> DeleteShardKeyAsync(

#endregion Cluster management

/// <summary>
/// Asynchronously checks the health of the Qdrant service.
/// </summary>
/// The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None" />.
/// <returns>
/// A task that represents the asynchronous operation. The task result contains
/// a <see cref="HealthCheckReply"/> indicating the health status of the Qdrant service.
/// </returns>
/// <exception cref="RpcException">occurs when server is unavailable</exception>
public async Task<HealthCheckReply> HealthAsync(CancellationToken cancellationToken = default) =>
await _grpcClient.Qdrant.HealthCheckAsync(
new HealthCheckRequest(),
deadline: _grpcTimeout == default ? null : DateTime.UtcNow.Add(_grpcTimeout),
cancellationToken: cancellationToken).ConfigureAwait(false);

private static void Populate<T>(RepeatedField<T> repeatedField, ReadOnlyMemory<T> memory)
{
if (MemoryMarshal.TryGetArray(memory, out var segment) &&
Expand Down
31 changes: 31 additions & 0 deletions tests/Qdrant.Client.Tests/HealthCheckTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using FluentAssertions;
using Grpc.Core;
using Xunit;

namespace Qdrant.Client
{
public class HealthCheckTests : IClassFixture<QdrantFixture>
{
private readonly QdrantClient _client;

public HealthCheckTests(QdrantFixture qdrantFixture) => _client = qdrantFixture.CreateClient();

[Fact]
public async Task Server_Should_Be_Healthy()
{
var response = await _client.HealthAsync();

response.Title.Should().NotBeNullOrEmpty();
response.Version.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task Server_Should_Be_UnHealthy()
{
var client = new QdrantClient("localhost", 9999);
;

await Assert.ThrowsAsync<RpcException>(async () => await client.HealthAsync());
}

}
}

0 comments on commit 62d3399

Please sign in to comment.