Skip to content
31 changes: 28 additions & 3 deletions Consul/Health.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,21 @@ public Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, boo
/// <returns>A query result containing the service members matching the provided service ID, tag, and health status, or a query result with a null response if no service members matched the filters provided</returns>
public Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default)
{
return Service(service, tag, passingOnly, q, null, ct);
return Service(service, tag, passingOnly, q, null, "serviceHealth", ct);
}

/// <summary>
/// Connect is equivalent to Service except that it will only return services which are Connect-enabled and will return the connection address for Connect clients to use which may be a proxy in front of the named service. If passingOnly is true only instances where both the service and any proxy are healthy will be returned.
/// </summary>
/// <param name="service">The service ID</param>
/// <param name="tag">The service member tag</param>
/// <param name="passingOnly">Only return if the health check is in the Passing state</param>
/// <param name="q">Customized query options</param>
/// <param name="ct">Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing</param>
/// <returns>A query result containing the service members matching the provided service ID, tag, and health status, or a query result with a null response if no service members matched the filters provided</returns>
public Task<QueryResult<ServiceEntry[]>> Connect(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default)
{
return Service(service, tag, passingOnly, q, null, "connectHealth", ct);
}

/// <summary>
Expand All @@ -284,11 +298,22 @@ public Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, boo
/// <param name="passingOnly">Only return if the health check is in the Passing state</param>
/// <param name="q">Customized query options</param>
/// <param name="filter">Specifies the expression used to filter the queries results prior to returning the data</param>
/// <param name="healthType">Specifies the type of health check to query for</param>
/// <param name="ct">Cancellation token for long poll request. If set, OperationCanceledException will be thrown if the request is cancelled before completing</param>
/// <returns>A query result containing the service members matching the provided service ID, tag, and health status, or a query result with a null response if no service members matched the filters provided</returns>
public Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, CancellationToken ct = default)
public Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, string healthType, CancellationToken ct = default)
{
var req = _client.Get<ServiceEntry[]>(string.Format("/v1/health/service/{0}", service), q, filter);
string path;
switch (healthType)
{
case "connectHealth":
path = "/v1/health/connect/{0}";
break;
default:
path = "/v1/health/service/{0}";
break;
}
var req = _client.Get<ServiceEntry[]>(string.Format(path, service), q, filter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that the documentation mentions that the connect and service endpoints are nearly identical, but I don't think it's a good idea to rely on that. I would rather split this into two separate implementations, one for connect and one for service endpoints.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okei noted, so instead of implementing the switch case, I should implement as a separate entity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okei noted, so instead of implementing the switch case, I should implement as a separate entity?

yes, I think it is going to be better that way.

if (!string.IsNullOrEmpty(tag))
{
req.Params["tag"] = tag;
Expand Down
4 changes: 3 additions & 1 deletion Consul/Interfaces/IHealthEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public interface IHealthEndpoint
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, string healthType, CancellationToken ct = default);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not ok, to change the public method signature in a way that makes it incompatible with previous versions.
If a new parameter is needed, then the new method overload needs to be added (without modifying the existing one).

Copy link
Contributor Author

@JocelynVelarde JocelynVelarde Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright so, leave the method signature as it was and just add a new one with the new parameter.

// Task<QueryResult<ServiceEntry[]>> Service(string service, string tag, bool passingOnly, QueryOptions q, Filter filter, CancellationToken ct = default);
Task<QueryResult<ServiceEntry[]>> Connect(string service, string tag, bool passingOnly, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<HealthCheck[]>> State(HealthStatus status, CancellationToken ct = default);
Task<QueryResult<HealthCheck[]>> State(HealthStatus status, QueryOptions q, CancellationToken ct = default);
}
Expand Down