Skip to content

Commit 096c5fd

Browse files
committed
remove useHttps from Kestrel. refactor HttpListener
1 parent ce9e81d commit 096c5fd

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

src/Prometheus.Client.MetricServer/IMetricServer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ public interface IMetricServer
1414
/// Stop server
1515
/// </summary>
1616
void Stop();
17+
18+
/// <summary>
19+
/// Server is Running?
20+
/// </summary>
21+
bool IsRunning { get; }
1722
}
1823
}

src/Prometheus.Client.MetricServer/MetricServer.HttpListener.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,84 @@ namespace Prometheus.Client.MetricServer
1515
public class MetricServer : BaseMetricServer, IMetricServer
1616
{
1717
private readonly HttpListener _httpListener = new HttpListener();
18-
private Thread _bgThread;
19-
private bool _isListening;
2018

2119
/// <summary>
2220
/// Constructor
2321
/// </summary>
24-
public MetricServer(int port, bool useHttps = false)
22+
public MetricServer(int port)
23+
: this(port, false)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Constructor
29+
/// </summary>
30+
public MetricServer(int port, bool useHttps)
2531
: this(Consts.DefaultHost, port, Consts.DefaultUrl, null, null, useHttps)
2632
{
2733
}
2834

2935
/// <summary>
3036
/// Constructor
3137
/// </summary>
32-
public MetricServer(string host, int port, bool useHttps = false)
38+
public MetricServer(string host, int port)
39+
: this(host, port, false)
40+
{
41+
}
42+
43+
/// <summary>
44+
/// Constructor
45+
/// </summary>
46+
public MetricServer(string host, int port, bool useHttps)
3347
: this(host, port, Consts.DefaultUrl, null, null, useHttps)
3448
{
3549
}
3650

3751
/// <summary>
3852
/// Constructor
3953
/// </summary>
40-
public MetricServer(string host, int port, string url, bool useHttps = false)
54+
public MetricServer(string host, int port, string url, bool useHttps)
4155
: this(host, port, url, null, null, useHttps)
4256
{
4357
}
4458

4559
/// <summary>
4660
/// Constructor
4761
/// </summary>
48-
public MetricServer(string hostname, int port, string url, IEnumerable<IOnDemandCollector> standardCollectors = null, ICollectorRegistry registry = null, bool useHttps = false)
62+
public MetricServer(string hostname, int port, string url, IEnumerable<IOnDemandCollector> standardCollectors, ICollectorRegistry registry, bool useHttps)
4963
: base(standardCollectors, registry)
5064
{
51-
var s = useHttps ? "s" : "";
52-
_httpListener.Prefixes.Add($"http{s}://{hostname}:{port}/{url}");
65+
_httpListener.Prefixes.Add($"http{(useHttps ? "s" : "")}://{hostname}:{port}/{url}");
5366
}
5467

5568
/// <inheritdoc />
5669
public void Start()
5770
{
58-
_bgThread = new Thread(StartListen)
71+
var bgThread = new Thread(StartListen)
5972
{
6073
IsBackground = true,
6174
Name = "MetricServer"
6275
};
63-
_bgThread.Start();
76+
bgThread.Start();
6477
}
6578

79+
/// <inheritdoc />
80+
public bool IsRunning { get; private set; }
81+
6682
/// <inheritdoc />
6783
public void Stop()
6884
{
69-
_isListening = false;
85+
IsRunning = false;
7086
_httpListener.Stop();
7187
_httpListener.Close();
7288
}
7389

7490
private void StartListen()
7591
{
7692
_httpListener.Start();
77-
_isListening = true;
93+
IsRunning = true;
7894

79-
while (_isListening)
95+
while (IsRunning)
8096
{
8197
try
8298
{

src/Prometheus.Client.MetricServer/MetricServer.Kestrel.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,71 +23,62 @@ public class MetricServer : BaseMetricServer, IMetricServer
2323
private readonly string _hostName;
2424
private readonly int _port;
2525
private readonly string _url;
26-
private readonly bool _useHttps;
2726
private IWebHost _host;
28-
29-
27+
3028
/// <summary>
3129
/// Constructor
3230
/// </summary>
3331
public MetricServer(int port)
34-
: this(Consts.DefaultHost, port, Consts.DefaultUrl, null, null, null, false)
32+
: this(Consts.DefaultHost, port, Consts.DefaultUrl, null, null, null)
3533
{
3634
}
3735

3836
/// <summary>
3937
/// Constructor
4038
/// </summary>
4139
public MetricServer(string host, int port)
42-
: this(host, port, Consts.DefaultUrl, null, null, null, false)
40+
: this(host, port, Consts.DefaultUrl, null, null, null)
4341
{
4442
}
4543

4644
/// <summary>
4745
/// Constructor
4846
/// </summary>
49-
public MetricServer(int port, X509Certificate2 certificate, bool useHttps)
50-
: this(Consts.DefaultHost, port, Consts.DefaultUrl, null, null, certificate, useHttps)
47+
public MetricServer(int port, X509Certificate2 certificate)
48+
: this(Consts.DefaultHost, port, Consts.DefaultUrl, null, null, certificate)
5149
{
5250
}
5351

5452
/// <summary>
5553
/// Constructor
5654
/// </summary>
57-
public MetricServer(string host, int port, X509Certificate2 certificate, bool useHttps)
58-
: this(host, port, Consts.DefaultUrl, null, null, certificate, useHttps)
55+
public MetricServer(string host, int port, X509Certificate2 certificate)
56+
: this(host, port, Consts.DefaultUrl, null, null, certificate)
5957
{
6058
}
6159

6260
/// <summary>
6361
/// Constructor
6462
/// </summary>
6563
public MetricServer(string host, int port, string url, IEnumerable<IOnDemandCollector> standardCollectors, ICollectorRegistry registry)
66-
: this(host, port, url, standardCollectors, registry, null, false)
64+
: this(host, port, url, standardCollectors, registry, null)
6765
{
6866
}
6967

7068

7169
/// <summary>
7270
/// Constructor
7371
/// </summary>
74-
public MetricServer(string host, int port, string url, IEnumerable<IOnDemandCollector> standardCollectors, ICollectorRegistry registry, X509Certificate2 certificate,
75-
bool useHttps)
72+
public MetricServer(string host, int port, string url, IEnumerable<IOnDemandCollector> standardCollectors, ICollectorRegistry registry, X509Certificate2 certificate)
7673
: base(standardCollectors, registry)
7774
{
78-
if (useHttps && certificate == null)
79-
throw new ArgumentNullException(nameof(certificate), $"{nameof(certificate)} is required when using https");
80-
81-
_useHttps = useHttps;
8275
_certificate = certificate;
8376
_port = port;
8477
_hostName = host;
8578
_url = url;
8679
}
8780

88-
/// <summary>
89-
/// Server is Running?
90-
/// </summary>
81+
/// <inheritdoc />
9182
public bool IsRunning => _host != null;
9283

9384
/// <inheritdoc />
@@ -106,16 +97,16 @@ public void Start()
10697
.UseKestrel(options =>
10798
{
10899
#if NETSTANDARD13
109-
if (_useHttps)
100+
if (_certificate != null)
110101
options.UseHttps(_certificate);
111102
#endif
112103

113104
#if NETSTANDARD20
114-
if (_useHttps)
105+
if (_certificate != null)
115106
options.Listen(IPAddress.Any, _port, listenOptions => { listenOptions.UseHttps(_certificate); });
116107
#endif
117108
})
118-
.UseUrls($"http{(_useHttps ? "s" : "")}://{_hostName}:{_port}")
109+
.UseUrls($"http{(_certificate != null ? "s" : "")}://{_hostName}:{_port}")
119110
.ConfigureServices(services => { services.AddSingleton<IStartup>(new Startup(Registry, _url)); })
120111
.UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).GetTypeInfo().Assembly.FullName)
121112
.Build();

0 commit comments

Comments
 (0)