-
Notifications
You must be signed in to change notification settings - Fork 5
/
ServicePointMonitor.cs
91 lines (76 loc) · 3.54 KB
/
ServicePointMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
namespace AzurePlot.Lib {
using System.Reflection;
using Console = System.Console;
public class ServicePointMonitor {
private Action<string> _output;
public ServicePointMonitor(Action<string> output) {
_output = output;
}
private void Print() {
foreach(var serviceEndpoint in ListServicePoints()) {
PrintServicePointConnections(serviceEndpoint);
}
}
private IEnumerable<ServicePoint> ListServicePoints(){
var tableField = typeof(ServicePointManager).GetField("s_ServicePointTable", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var table = (Hashtable)tableField.GetValue(null);
var keys = table.Keys.Cast<object>().ToList();
Output("ServicePoint count: {0}, DefaultConnectionLimit: {1}", keys.Count, ServicePointManager.DefaultConnectionLimit);
foreach(var key in keys) {
var val = ((WeakReference)table[key]);
if(val == null) {
continue;
}
var target = val.Target;
if(target==null) {
continue;
}
yield return target as ServicePoint;
}
}
private void Output(string fmt,params object[] args)
{
_output(string.Format(fmt,args));
}
private void PrintServicePointConnections(ServicePoint sp) {
var spType = sp.GetType();
var privateOrPublicInstanceField = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
var connectionGroupField = spType.GetField("m_ConnectionGroupList",privateOrPublicInstanceField);
var value = (Hashtable)connectionGroupField.GetValue(sp);
var connectionGroups = value.Keys.Cast<object>().ToList();
var totalConnections = 0;
Output("ServicePoint: {0} (Connection Limit: {1}, Reported connections: {2})", sp.Address,sp.ConnectionLimit,sp.CurrentConnections);
foreach(var key in connectionGroups) {
var connectionGroup = value[key];
var groupType = connectionGroup.GetType();
var listField = groupType.GetField("m_ConnectionList",privateOrPublicInstanceField);
var listValue = (ArrayList)listField.GetValue(connectionGroup);
//Console.WriteLine("{3} {0}\nConnectionGroup: {1} Count: {2}",sp.Address, key,listValue.Count, DateTime.Now);
Output("{0}", key);
totalConnections+=listValue.Count;
}
Output("ConnectionGroupCount: {0}, Total Connections: {1}", connectionGroups.Count, totalConnections);
}
public static void Start(TimeSpan interval) {
Start(interval,Console.WriteLine);
}
public static void Start(TimeSpan interval, Action<string> output) {
var thread = new Thread(()=> {
while(true) {
var monitor = new ServicePointMonitor(output);
monitor.Print();
Thread.Sleep(interval);
}
});
thread.IsBackground = true;
thread.Start();
}
}
}