Skip to content

Commit 462a5f4

Browse files
committed
Send the DISCARD ALL and the SET commands just before the user's first packet is sent, and don't block the thread waiting for responses that are to be discarded. IsValid (ping) doesn't do a ping anymore (temporarily disabled).
1 parent c62aa5f commit 462a5f4

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

‎Npgsql/Npgsql/NpgsqlCommand.PrepareExecute.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ namespace Npgsql
4646
/// </summary>
4747
public sealed partial class NpgsqlCommand : DbCommand, ICloneable
4848
{
49+
internal static void ExecuteBlindDontWaitForResponse(NpgsqlConnector connector, string command, int numQueries, int numCommands)
50+
{
51+
ExecuteBlindDontWaitForResponse(connector, new NpgsqlQuery(command), numQueries, numCommands);
52+
}
53+
54+
/// <summary>
55+
/// Internal query method to run when the wire shouldn't be flushed and
56+
/// the response should be discarded later.
57+
/// </summary>
58+
internal static void ExecuteBlindDontWaitForResponse(NpgsqlConnector connector, NpgsqlQuery query, int numQueries, int numCommands)
59+
{
60+
connector.Query(query);
61+
connector.pendingCompletePacketsToIgnore += numQueries;
62+
connector.pendingReadyForQueryPacketsToIgnore += numCommands;
63+
}
64+
4965
/// <summary>
5066
/// Internal query shortcut for use in cases where the number
5167
/// of affected rows is of no interest.

‎Npgsql/Npgsql/NpgsqlConnector.cs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,18 @@ internal class NpgsqlConnector
166166
// expect a ready_for_query response.
167167
private bool _requireReadyForQuery = true;
168168

169+
internal int pendingCompletePacketsToIgnore = 0;
170+
internal int pendingReadyForQueryPacketsToIgnore = 0;
171+
169172
private readonly Dictionary<string, NpgsqlParameterStatus> _serverParameters =
170173
new Dictionary<string, NpgsqlParameterStatus>(StringComparer.InvariantCultureIgnoreCase);
171174

172175
// For IsValid test
173176
private readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
174177

175178
private string initQueries;
179+
private int numInitQueries = 0;
180+
private int numInitCommands = 0;
176181

177182
#if WINDOWS && UNMANAGED
178183

@@ -373,6 +378,7 @@ internal Boolean IsValid()
373378
{
374379
try
375380
{
381+
/*
376382
// Here we use a fake NpgsqlCommand, just to send the test query string.
377383
378384
// Get random test value.
@@ -397,7 +403,8 @@ internal Boolean IsValid()
397403
{
398404
return false;
399405
}
400-
406+
*/
407+
401408
this.RequireReadyForQuery = true;
402409
}
403410
catch
@@ -408,6 +415,18 @@ internal Boolean IsValid()
408415
return true;
409416
}
410417

418+
internal void ResetSession()
419+
{
420+
if (SupportsDiscard)
421+
{
422+
NpgsqlCommand.ExecuteBlindDontWaitForResponse(this, NpgsqlQuery.DiscardAll, 1, 1);
423+
}
424+
if (numInitCommands > 0)
425+
{
426+
NpgsqlCommand.ExecuteBlindDontWaitForResponse(this, initQueries, numInitQueries, numInitCommands);
427+
}
428+
}
429+
411430
/// <summary>
412431
/// This method is responsible for releasing all resources associated with this Connector.
413432
/// </summary>
@@ -429,7 +448,7 @@ internal void ReleaseResources()
429448

430449
internal void ReleaseWithDiscard()
431450
{
432-
NpgsqlCommand.ExecuteBlind(this, NpgsqlQuery.DiscardAll);
451+
//NpgsqlCommand.ExecuteBlind(this, NpgsqlQuery.DiscardAll);
433452

434453
// The initial connection parameters will be restored via IsValid() when get connector from pool later
435454
}
@@ -821,14 +840,18 @@ internal void Open()
821840
if (SupportsExtraFloatDigits3)
822841
{
823842
sbInitQueries.WriteLine("SET extra_float_digits=3;");
843+
numInitQueries++;
824844
}
825845

826846
if (SupportsSslRenegotiationLimit)
827847
{
828848
sbInitQueries.WriteLine("SET ssl_renegotiation_limit=0;");
849+
numInitQueries++;
829850
}
830851

831852
initQueries = sbInitQueries.ToString();
853+
if (numInitQueries > 0)
854+
numInitCommands = 1;
832855

833856
NpgsqlCommand.ExecuteBlind(this, initQueries);
834857

‎Npgsql/Npgsql/NpgsqlConnectorPool.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,11 @@ private NpgsqlConnector GetPooledConnector(NpgsqlConnection Connection)
357357

358358
} while (Connector != null && !Connector.IsValid());
359359

360-
if (Connector != null) return Connector;
360+
if (Connector != null)
361+
{
362+
Connector.ResetSession();
363+
return Connector;
364+
}
361365

362366
lock (Queue)
363367
{

‎Npgsql/Npgsql/NpgsqlState.BackendResponse.cs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ protected IEnumerable<IServerResponseObject> ProcessBackendResponses(NpgsqlConne
310310
throw new NpgsqlException(errors);
311311
}
312312

313-
yield break;
313+
if (context.pendingReadyForQueryPacketsToIgnore == 0)
314+
{
315+
yield break;
316+
}
317+
else
318+
{
319+
context.pendingReadyForQueryPacketsToIgnore--;
320+
}
321+
break;
314322

315323
case BackEndMessageCode.BackendKeyData:
316324

@@ -330,7 +338,15 @@ protected IEnumerable<IServerResponseObject> ProcessBackendResponses(NpgsqlConne
330338

331339
case BackEndMessageCode.CompletedResponse:
332340
PGUtil.ReadInt32(stream);
333-
yield return new CompletedResponse(stream);
341+
if (context.pendingCompletePacketsToIgnore == 0)
342+
{
343+
yield return new CompletedResponse(stream);
344+
}
345+
else
346+
{
347+
new CompletedResponse(stream);
348+
context.pendingCompletePacketsToIgnore--;
349+
}
334350
break;
335351
case BackEndMessageCode.ParseComplete:
336352
NpgsqlEventLog.LogMsg(resman, "Log_ProtocolMessage", LogLevel.Debug, "ParseComplete");

0 commit comments

Comments
 (0)