1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
@@ -24,6 +24,22 @@ public sealed class HealthCheckRegistration
24
24
{
25
25
private Func < IServiceProvider , IHealthCheck > _factory ;
26
26
private string _name ;
27
+ private TimeSpan _timeout ;
28
+
29
+ /// <summary>
30
+ /// Creates a new <see cref="T:Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckRegistration" /> for an existing <see cref="T:Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck" /> instance.
31
+ /// </summary>
32
+ /// <param name="name">The health check name.</param>
33
+ /// <param name="instance">The <see cref="T:Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck" /> instance.</param>
34
+ /// <param name="failureStatus">
35
+ /// The <see cref="T:Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus" /> that should be reported upon failure of the health check. If the provided value
36
+ /// is <c>null</c>, then <see cref="F:Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Unhealthy" /> will be reported.
37
+ /// </param>
38
+ /// <param name="tags">A list of tags that can be used for filtering health checks.</param>
39
+ public HealthCheckRegistration ( string name , IHealthCheck instance , HealthStatus ? failureStatus , IEnumerable < string > tags )
40
+ : this ( name , instance , failureStatus , tags , default )
41
+ {
42
+ }
27
43
28
44
/// <summary>
29
45
/// Creates a new <see cref="HealthCheckRegistration"/> for an existing <see cref="IHealthCheck"/> instance.
@@ -35,7 +51,8 @@ public sealed class HealthCheckRegistration
35
51
/// is <c>null</c>, then <see cref="HealthStatus.Unhealthy"/> will be reported.
36
52
/// </param>
37
53
/// <param name="tags">A list of tags that can be used for filtering health checks.</param>
38
- public HealthCheckRegistration ( string name , IHealthCheck instance , HealthStatus ? failureStatus , IEnumerable < string > tags )
54
+ /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
55
+ public HealthCheckRegistration ( string name , IHealthCheck instance , HealthStatus ? failureStatus , IEnumerable < string > tags , TimeSpan ? timeout )
39
56
{
40
57
if ( name == null )
41
58
{
@@ -47,10 +64,16 @@ public HealthCheckRegistration(string name, IHealthCheck instance, HealthStatus?
47
64
throw new ArgumentNullException ( nameof ( instance ) ) ;
48
65
}
49
66
67
+ if ( timeout <= TimeSpan . Zero && timeout != System . Threading . Timeout . InfiniteTimeSpan )
68
+ {
69
+ throw new ArgumentOutOfRangeException ( nameof ( timeout ) ) ;
70
+ }
71
+
50
72
Name = name ;
51
73
FailureStatus = failureStatus ?? HealthStatus . Unhealthy ;
52
74
Tags = new HashSet < string > ( tags ?? Array . Empty < string > ( ) , StringComparer . OrdinalIgnoreCase ) ;
53
75
Factory = ( _ ) => instance ;
76
+ Timeout = timeout ?? System . Threading . Timeout . InfiniteTimeSpan ;
54
77
}
55
78
56
79
/// <summary>
@@ -68,6 +91,27 @@ public HealthCheckRegistration(
68
91
Func < IServiceProvider , IHealthCheck > factory ,
69
92
HealthStatus ? failureStatus ,
70
93
IEnumerable < string > tags )
94
+ : this ( name , factory , failureStatus , tags , default )
95
+ {
96
+ }
97
+
98
+ /// <summary>
99
+ /// Creates a new <see cref="HealthCheckRegistration"/> for an existing <see cref="IHealthCheck"/> instance.
100
+ /// </summary>
101
+ /// <param name="name">The health check name.</param>
102
+ /// <param name="factory">A delegate used to create the <see cref="IHealthCheck"/> instance.</param>
103
+ /// <param name="failureStatus">
104
+ /// The <see cref="HealthStatus"/> that should be reported when the health check reports a failure. If the provided value
105
+ /// is <c>null</c>, then <see cref="HealthStatus.Unhealthy"/> will be reported.
106
+ /// </param>
107
+ /// <param name="tags">A list of tags that can be used for filtering health checks.</param>
108
+ /// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
109
+ public HealthCheckRegistration (
110
+ string name ,
111
+ Func < IServiceProvider , IHealthCheck > factory ,
112
+ HealthStatus ? failureStatus ,
113
+ IEnumerable < string > tags ,
114
+ TimeSpan ? timeout )
71
115
{
72
116
if ( name == null )
73
117
{
@@ -79,10 +123,16 @@ public HealthCheckRegistration(
79
123
throw new ArgumentNullException ( nameof ( factory ) ) ;
80
124
}
81
125
126
+ if ( timeout <= TimeSpan . Zero && timeout != System . Threading . Timeout . InfiniteTimeSpan )
127
+ {
128
+ throw new ArgumentOutOfRangeException ( nameof ( timeout ) ) ;
129
+ }
130
+
82
131
Name = name ;
83
132
FailureStatus = failureStatus ?? HealthStatus . Unhealthy ;
84
133
Tags = new HashSet < string > ( tags ?? Array . Empty < string > ( ) , StringComparer . OrdinalIgnoreCase ) ;
85
134
Factory = factory ;
135
+ Timeout = timeout ?? System . Threading . Timeout . InfiniteTimeSpan ;
86
136
}
87
137
88
138
/// <summary>
@@ -107,6 +157,23 @@ public Func<IServiceProvider, IHealthCheck> Factory
107
157
/// </summary>
108
158
public HealthStatus FailureStatus { get ; set ; }
109
159
160
+ /// <summary>
161
+ /// Gets or sets the timeout used for the test.
162
+ /// </summary>
163
+ public TimeSpan Timeout
164
+ {
165
+ get => _timeout ;
166
+ set
167
+ {
168
+ if ( value <= TimeSpan . Zero && value != System . Threading . Timeout . InfiniteTimeSpan )
169
+ {
170
+ throw new ArgumentOutOfRangeException ( nameof ( value ) ) ;
171
+ }
172
+
173
+ _timeout = value ;
174
+ }
175
+ }
176
+
110
177
/// <summary>
111
178
/// Gets or sets the health check name.
112
179
/// </summary>
0 commit comments