Remove locking. Unforseen deadlock
SuccessfulCode/SuccessfulCodeTools/master This commit looks good

This commit is contained in:
2020-02-26 09:59:17 +00:00
parent 6f83e96055
commit e2556fae85
@@ -17,7 +17,6 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
{ {
public int? OutgoingPort { get; set; } public int? OutgoingPort { get; set; }
public KeyValueCollection<string, string> DefaultHeaders = new KeyValueCollection<string, string>(); public KeyValueCollection<string, string> DefaultHeaders = new KeyValueCollection<string, string>();
private static readonly ConcurrentDictionary<int, object> _dictionary = new ConcurrentDictionary<int, object>();
public CustomHttpClient(int? outgoingPort = null) public CustomHttpClient(int? outgoingPort = null)
{ {
OutgoingPort = outgoingPort; OutgoingPort = outgoingPort;
@@ -30,8 +29,7 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
message.Headers.AddRange(defaultHeaders); message.Headers.AddRange(defaultHeaders);
var outBoundPort = outgoingPort ?? OutgoingPort ?? 0; var outBoundPort = outgoingPort ?? OutgoingPort ?? 0;
var semphoreLock = (SemaphoreSlim)_dictionary.GetOrAdd(outBoundPort, e => new SemaphoreSlim(1));
semphoreLock.Wait();
var ipLocalEndPoint = new IPEndPoint(IPAddress.Any, outBoundPort); var ipLocalEndPoint = new IPEndPoint(IPAddress.Any, outBoundPort);
using (var tcp = new TcpClient(ipLocalEndPoint)) using (var tcp = new TcpClient(ipLocalEndPoint))
{ {
@@ -60,7 +58,6 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
tcp.Client.Disconnect(true); tcp.Client.Disconnect(true);
stream.Close(); stream.Close();
tcp.Client.Close(); tcp.Client.Close();
semphoreLock.Release();
return new CustomHttpResponseMessage(memory); return new CustomHttpResponseMessage(memory);
} }
} }