| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Channels.Framing; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | internal class ConnectionReuseHandler : IConnectionReuseHandler |
| | | 14 | | { |
| | | 15 | | private readonly SemaphoreSlim _connectionPoolSemaphore; |
| | | 16 | | |
| | 71 | 17 | | public ConnectionReuseHandler(ConnectionPoolSettings connectionPoolSettings) |
| | | 18 | | { |
| | 71 | 19 | | _connectionPoolSemaphore = new SemaphoreSlim(connectionPoolSettings.MaxOutboundConnectionsPerEndpoint); |
| | 71 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<bool> ReuseConnectionAsync(FramingConnection connection, CancellationToken cancellationToken) |
| | | 23 | | { |
| | 110 | 24 | | if (cancellationToken.IsCancellationRequested) |
| | | 25 | | { |
| | 5 | 26 | | return false; |
| | | 27 | | } |
| | | 28 | | |
| | 105 | 29 | | if (!_connectionPoolSemaphore.Wait(0)) |
| | | 30 | | { |
| | | 31 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 32 | | //{ |
| | | 33 | | // TraceUtility.TraceEvent(TraceEventType.Warning, |
| | | 34 | | // TraceCode.ServerMaxPooledConnectionsQuotaReached, |
| | | 35 | | // SR.GetString(SR.TraceCodeServerMaxPooledConnectionsQuotaReached, maxPooledConnections), |
| | | 36 | | // new StringTraceRecord("MaxOutboundConnectionsPerEndpoint", maxPooledConnections.ToString(Cultu |
| | | 37 | | // this, null); |
| | | 38 | | //} |
| | | 39 | | |
| | | 40 | | //if (TD.ServerMaxPooledConnectionsQuotaReachedIsEnabled()) |
| | | 41 | | //{ |
| | | 42 | | // TD.ServerMaxPooledConnectionsQuotaReached(); |
| | | 43 | | //} |
| | 0 | 44 | | connection.Logger.ConnectionPoolFull(); |
| | | 45 | | // No space left in the connection pool |
| | 0 | 46 | | return false; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 105 | 51 | | connection.Reset(); |
| | | 52 | | |
| | 105 | 53 | | using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource( |
| | 105 | 54 | | connection.ChannelInitializationCancellationToken, |
| | 105 | 55 | | cancellationToken)) |
| | | 56 | | { |
| | 105 | 57 | | connection.Logger.StartPendingReadOnIdleSocket(); |
| | | 58 | | Debug.Assert(connection.Transport == connection.RawTransport); |
| | 105 | 59 | | var readResult = await connection.Input.ReadAsync(linkedCts.Token); |
| | 37 | 60 | | connection.Logger.EndPendingReadOnIdleSocket(readResult); |
| | 37 | 61 | | if (readResult.IsCompleted) |
| | | 62 | | { |
| | 1 | 63 | | connection.Logger.IdleConnectionClosed(); |
| | 1 | 64 | | connection.Output.Complete(); |
| | 1 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 36 | 68 | | connection.Input.AdvanceTo(readResult.Buffer.Start); // Don't consume any bytes. The pending read is |
| | 36 | 69 | | } |
| | | 70 | | |
| | 36 | 71 | | return true; |
| | | 72 | | } |
| | 67 | 73 | | catch(OperationCanceledException) |
| | | 74 | | { |
| | 67 | 75 | | connection.Logger.IdleConnectionClosed(); |
| | 67 | 76 | | return false; |
| | | 77 | | } |
| | 1 | 78 | | catch (Exception e) |
| | | 79 | | { |
| | 1 | 80 | | connection.Logger.FailureInConnectionReuse(e); |
| | 1 | 81 | | return false; |
| | | 82 | | } |
| | | 83 | | finally |
| | | 84 | | { |
| | 105 | 85 | | _connectionPoolSemaphore.Release(); |
| | | 86 | | } |
| | 110 | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |