pingCheck method

  1. @override
Future<HealthIndicatorCheck> pingCheck(
  1. RequestContext _
)
override

Returns a map with the status of the specific component. Throws an exception if the component is critically failing.

Implementation

@override
Future<HealthIndicatorCheck> pingCheck(RequestContext _) async {
  final client = HttpClient();
  try {
    // Initiate request and enforce timeout
    final request = await client.getUrl(Uri.parse(url)).timeout(timeout);

    // Await response and enforce timeout
    final response = await request.close().timeout(timeout);

    if (response.statusCode == expectedStatusCode) {
      return (
        status: HealthStatus.up,
        details: {'statusCode': response.statusCode},
      );
    } else {
      return (
        status: HealthStatus.down,
        details: {'error': 'Unexpected status code: ${response.statusCode}'},
      );
    }
  } catch (e) {
    return (
      status: HealthStatus.down,
      details: {
        'error': e is TimeoutException
            ? 'Connection timed out'
            : e.toString(),
      },
    );
  } finally {
    client.close(); // Prevent memory leaks
  }
}