- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConnectionPoolProvider.cs
More file actions
Latest commit
149 lines (138 loc) · 8.7 KB
/
Copy pathConnectionPoolProvider.cs
File metadata and controls
149 lines (138 loc) · 8.7 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2017 Stanislav Muhametsin. All rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
usingCBAM.Abstractions;
usingCBAM.Abstractions.Implementation.NetworkStream;
usingCBAM.SQL.Implementation;
usingCBAM.SQL.PostgreSQL;
usingCBAM.SQL.PostgreSQL.Implementation;
usingIOUtils.Network.Configuration;
usingResourcePooling.Async.Abstractions;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingUtilPack;
namespaceCBAM.SQL.PostgreSQL
{
usingTIntermediateState=ValueTuple<BackendABIHelper,ResizableArray<Byte>,CancellationToken,Stream>;
/// <summary>
/// This is the entrypoint-class for using PostgreSQL connections.
/// Use <see cref="Factory"/> static property to acquire <see cref="AsyncResourceFactory{TResource, TParams}"/> for <see cref="PgSQLConnection"/>, and then use <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/> to obtain <see cref="AsyncResourceFactory{TResource}"/>.
/// This <see cref="AsyncResourceFactory{TResource}"/> with one generic type parameter then has a number of extension methods which create various <see cref="AsyncResourcePool{TResource}"/>s, those pools then provide effective mechanism to actually use <see cref="PgSQLConnection"/>s.
/// </summary>
/// <remarks>
/// This class also (explicitly) implements <see cref="AsyncResourceFactoryProvider"/> interface in order to provide dynamic creation of <see cref="AsyncResourcePool{TResource}"/>s, but this is used in generic scenarios (e.g. MSBuild task, where this class can be given as parameter, and the task dynamically loads this type).
/// </remarks>
publicsealedclassPgSQLConnectionPoolProvider:AbstractAsyncResourceFactoryProvider<PgSQLConnection,PgSQLConnectionCreationInfo>
{
/// <summary>
/// Gets the <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create pools that provide instances of <see cref="PgSQLConnection"/> to use.
/// </summary>
/// <value>The <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create <see cref="PgSQLConnection"/> to use.</value>
/// <remarks>
/// By invoking <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/>, one gets the bound version <see cref="AsyncResourceFactory{TResource}"/>, with only one generic parameter.
/// Instead of directly using <see cref="AsyncResourceFactory{TResource}.CreateAcquireResourceContext"/>, typical scenario would involve creating an instance <see cref="AsyncResourcePool{TResource}"/> by invoking one of various extension methods for <see cref="AsyncResourceFactory{TResource}"/>.
/// </remarks>
publicstaticAsyncResourceFactory<PgSQLConnection,PgSQLConnectionCreationInfo>Factory{get;}=newDefaultAsyncResourceFactory<PgSQLConnection,PgSQLConnectionCreationInfo>( config =>
config.NewFactoryParametrizer<PgSQLConnectionCreationInfo,PgSQLConnectionCreationInfoData,PgSQLConnectionConfiguration,PgSQLInitializationConfiguration,PgSQLProtocolConfiguration,PgSQLPoolingConfiguration>()
.BindPublicConnectionType<PgSQLConnection>()
.CreateStatefulDelegatingConnectionFactory(
newUTF8EncodingInfo(),
(parameters,encodingInfo,stringPool,stringPoolIsDedicated,socketOrNull,stream,token)=>newTIntermediateState(newBackendABIHelper(encodingInfo,stringPool),newResizableArray<Byte>(initialSize:8,exponentialResize:true),token,stream),
async(parameters,encodingInfo,stringPool,stringPoolIsDedicated,state)=>
{
varsslMode=parameters.CreationData?.Connection?.ConnectionSSLMode??ConnectionSSLMode.NotRequired;
varretVal=sslMode==ConnectionSSLMode.Required||sslMode==ConnectionSSLMode.Preferred;
if(retVal)
{
awaitSSLRequestMessage.INSTANCE.SendMessageAsync((state.Item1,state.Item4,state.Item3,state.Item2));
awaitstate.Item4.ReadSpecificAmountAsync(state.Item2.Array,0,1,state.Item3);
retVal=state.Item2.Array[0]==(Byte)'S';
}
returnretVal;
},
()=>newPgSQLException("Server accepted SSL request, but the creation parameters did not have callback to create SSL stream"),
()=>newPgSQLException("Server does not support SSL."),
()=>newPgSQLException("SSL stream creation callback returned null."),
()=>newPgSQLException("Authentication callback given by SSL stream creation callback was null."),
inner =>newPgSQLException("Unable to start SSL client.",inner),
async(parameters,encodingInfo,stringPool,stringPoolIsDedicated,stream,socketOrNull,token,state)=>
{
(varproto,varwarnings)=awaitPostgreSQLProtocol.PerformStartup(
newPgSQLConnectionVendorFunctionalityImpl(),
parameters,
token,
stream,
state.Item1,
state.Item2
#if !NETSTANDARD1_0
,(System.Net.Sockets.Socket)socketOrNull
#endif
);
returnproto;
},
protocol =>newValueTask<PgSQLConnectionImpl>(newPgSQLConnectionImpl(protocol,newPgSQLDatabaseMetaData(protocol))),
(protocol,connection)=>newPgSQLConnectionAcquireInfo(connection,protocol),
(functionality,connection,token,error)=>functionality?.Stream
));
//new PgSQLConnectionFactory( config, new UTF8EncodingInfo() ) );
/// <summary>
/// Creates a new instance of <see cref="PgSQLConnectionPoolProvider"/>.
/// </summary>
/// <remarks>
/// This constructor is not intended to be used directly, but a generic scenarios like MSBuild task dynamically loading this type.
/// </remarks>
publicPgSQLConnectionPoolProvider()
:base(typeof(PgSQLConnectionCreationInfoData))
{
}
/// <summary>
/// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}.TransformFactoryParameters"/> method, by checking that given object is <see cref="PgSQLConnectionCreationInfo"/> or <see cref="PgSQLConnectionCreationInfoData"/>.
/// </summary>
/// <param name="creationParameters">The untyped creation parameters.</param>
/// <returns>The <see cref="PgSQLConnectionCreationInfo"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="creationParameters"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="creationParameters"/> is not <see cref="PgSQLConnectionCreationInfo"/> or <see cref="PgSQLConnectionCreationInfoData"/>.</exception>
protectedoverridePgSQLConnectionCreationInfoTransformFactoryParameters(ObjectcreationParameters)
{
ArgumentValidator.ValidateNotNull(nameof(creationParameters),creationParameters);
PgSQLConnectionCreationInforetVal;
if(creationParametersisPgSQLConnectionCreationInfoDatacreationData)
{
retVal=newPgSQLConnectionCreationInfo(creationData);
}
elseif(creationParametersisPgSQLConnectionCreationInfocreationInfo)
{
retVal=creationInfo;
}
else
{
thrownewArgumentException($"The {nameof(creationParameters)} must be instance of {typeof(PgSQLConnectionCreationInfoData).FullName}.");
}
returnretVal;
}
/// <summary>
/// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}.CreateFactory"/> by returning static property <see cref="Factory"/>.
/// </summary>
/// <returns>The value of <see cref="Factory"/> static property.</returns>
protectedoverrideAsyncResourceFactory<PgSQLConnection,PgSQLConnectionCreationInfo>CreateFactory()
=>Factory;
}
}