Skip to content

Commit

Permalink
Back to sync dns.resolvename
Browse files Browse the repository at this point in the history
Async dns resolve name can lead to the dns timeouts and increase the
time spend for a query, because of thread issues
  • Loading branch information
miloszes committed Sep 30, 2014
1 parent a17854b commit 308c79e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
[Oo]bj/
[Oo]bj/
packages/*/
UpgradeLog.htm
26 changes: 10 additions & 16 deletions Npgsql/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -327,20 +328,13 @@ internal void Open()

public void RawOpen(int timeout)
{
// Keep track of time remaining; Even though there may be multiple timeout-able calls,
// this allows us to still respect the caller's timeout expectation.
var attemptStart = DateTime.Now;
var result = Dns.BeginGetHostAddresses(Host, null, null);

if (!result.AsyncWaitHandle.WaitOne(timeout, true))
{
// Timeout was used up attempting the Dns lookup
throw new TimeoutException(L10N.DnsLookupTimeout);
}

timeout -= Convert.ToInt32((DateTime.Now - attemptStart).TotalMilliseconds);
var sw = new Stopwatch();
sw.Start();

timeout -= (int)sw.ElapsedMilliseconds;

var ips = Dns.EndGetHostAddresses(result);
//do not use the async resolve name because of a lot of dns timeouts in case of paralled.foreach operations
var ips = Dns.GetHostAddresses(Host);
Socket socket = null;
Exception lastSocketException = null;

Expand All @@ -352,11 +346,11 @@ public void RawOpen(int timeout)
_log.Trace("Attempting to connect to " + ips[i]);
var ep = new IPEndPoint(ips[i], Port);
socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
attemptStart = DateTime.Now;
sw.Restart();

try
{
result = socket.BeginConnect(ep, null, null);
var result = socket.BeginConnect(ep, null, null);

if (!result.AsyncWaitHandle.WaitOne(timeout / (ips.Length - i), true))
{
Expand All @@ -371,7 +365,7 @@ public void RawOpen(int timeout)
catch (Exception e)
{
_log.Warn("Failed to connect to " + ips[i]);
timeout -= Convert.ToInt32((DateTime.Now - attemptStart).TotalMilliseconds);
timeout -= (int)sw.ElapsedMilliseconds;
lastSocketException = e;

socket.Close();
Expand Down

0 comments on commit 308c79e

Please sign in to comment.