This repository was archived by the owner on Feb 16, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathEventStoreConnection.cs
More file actions
231 lines (214 loc) · 14.5 KB
/
Copy pathEventStoreConnection.cs
File metadata and controls
231 lines (214 loc) · 14.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System.Net;
using System.Linq;
using System.Net.Sockets;
using EventStore.ClientAPI.Common.Utils;
using EventStore.ClientAPI.Internal;
using System;
using EventStore.ClientAPI.SystemData;
namespace EventStore.ClientAPI
{
/// <summary>
/// Contains factory methods for building connections to an Event Store server.
/// </summary>
public static class EventStoreConnection
{
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> to single node using default <see cref="ConnectionSettings"/>
/// </summary>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <param name="uri">The Uri to connect to. It can be tcp:// to point to a single node or discover:// to discover nodes</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(Uri uri, string connectionName = null)
{
return Create(ConnectionSettings.Default, uri, connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> to single node using default <see cref="ConnectionSettings"/> provided via a connectionstring
/// </summary>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <param name="connectionString">The connection string to for this connection.</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(string connectionString, string connectionName = null)
{
return Create(connectionString, null, connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> to single node using default <see cref="ConnectionSettings"/> provided via a connectionstring
/// </summary>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <param name="builder">Pre-populated settings builder, optional. If not specified, a new builder will be created.</param>
/// <param name="connectionString">The connection string to for this connection.</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(string connectionString, ConnectionSettingsBuilder builder, string connectionName = null)
{
var settings = ConnectionString.GetConnectionSettings(connectionString, builder);
var uri = GetUriFromConnectionString(connectionString);
if (uri == null && (settings.GossipSeeds == null || settings.GossipSeeds.Length == 0))
{
throw new Exception(string.Format("Did not find ConnectTo or GossipSeeds in the connection string.\n'{0}'", connectionString));
}
if (uri != null && settings.GossipSeeds != null && settings.GossipSeeds.Length > 0)
{
throw new NotSupportedException(string.Format("Setting ConnectTo as well as GossipSeeds on the connection string is currently not supported.\n{0}", connectionString));
}
return Create(settings, uri, connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> using the gossip seeds specified in the <paramref name="connectionSettings"/>
/// </summary>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <param name="connectionSettings">The <see cref="ConnectionSettings"/> to apply to the new connection</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(ConnectionSettings connectionSettings, string connectionName = null)
{
if (connectionSettings.GossipSeeds == null || connectionSettings.GossipSeeds.Length == 0)
throw new ArgumentException("No gossip seeds specified", nameof(connectionSettings));
return Create(connectionSettings, (Uri)null, connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/>
/// </summary>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <param name="connectionSettings">The <see cref="ConnectionSettings"/> to apply to the new connection. If null the default settings will be used and the <paramref name="uri"/> must not be null</param>
/// <param name="uri">The Uri to connect to. It can be tcp:// to point to a single node or discover:// to discover nodes via dns or null to connect using the gossip seeds from the <paramref name="connectionSettings"/></param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
/// <remarks>You must pass a uri or set gossip seeds in the connection settings.</remarks>
public static IEventStoreConnection Create(ConnectionSettings connectionSettings, Uri uri,
string connectionName = null)
{
connectionSettings = connectionSettings ?? ConnectionSettings.Default;
if (uri != null)
{
var scheme = uri.Scheme.ToLower();
var credential = GetCredentialFromUri(uri);
if (credential != null)
{
connectionSettings = new ConnectionSettings(connectionSettings.Log, connectionSettings.VerboseLogging,
connectionSettings.MaxQueueSize, connectionSettings.MaxConcurrentItems,
connectionSettings.MaxRetries, connectionSettings.MaxReconnections,
connectionSettings.RequireMaster, connectionSettings.ReconnectionDelay,
connectionSettings.QueueTimeout, connectionSettings.OperationTimeout,
connectionSettings.OperationTimeoutCheckPeriod, credential, connectionSettings.UseSslConnection,
connectionSettings.TargetHost,
connectionSettings.ValidateServer, connectionSettings.FailOnNoServerResponse,
connectionSettings.HeartbeatInterval, connectionSettings.HeartbeatTimeout,
connectionSettings.ClientConnectionTimeout, connectionSettings.ClusterDns,
connectionSettings.GossipSeeds, connectionSettings.MaxDiscoverAttempts,
connectionSettings.ExternalGossipPort, connectionSettings.GossipTimeout,
connectionSettings.NodePreference);
}
if (scheme == "discover")
{
var clusterSettings = new ClusterSettings(uri.Host, connectionSettings.MaxDiscoverAttempts, uri.Port,
connectionSettings.GossipTimeout, connectionSettings.NodePreference);
Ensure.NotNull(connectionSettings, "connectionSettings");
Ensure.NotNull(clusterSettings, "clusterSettings");
var endPointDiscoverer = new ClusterDnsEndPointDiscoverer(connectionSettings.Log,
clusterSettings.ClusterDns,
clusterSettings.MaxDiscoverAttempts,
clusterSettings.ExternalGossipPort,
clusterSettings.GossipSeeds,
clusterSettings.GossipTimeout,
clusterSettings.NodePreference);
return new EventStoreNodeConnection(connectionSettings, clusterSettings, endPointDiscoverer,
connectionName);
}
if (scheme == "tcp")
{
return new EventStoreNodeConnection(connectionSettings, null,
new SingleEndpointDiscoverer(uri, connectionSettings.UseSslConnection), connectionName);
}
throw new Exception(string.Format("Unknown scheme for connection '{0}'", scheme));
}
if (connectionSettings.GossipSeeds != null && connectionSettings.GossipSeeds.Length > 0)
{
var clusterSettings = new ClusterSettings(connectionSettings.GossipSeeds,
connectionSettings.MaxDiscoverAttempts,
connectionSettings.GossipTimeout,
connectionSettings.NodePreference);
Ensure.NotNull(connectionSettings, "connectionSettings");
Ensure.NotNull(clusterSettings, "clusterSettings");
var endPointDiscoverer = new ClusterDnsEndPointDiscoverer(connectionSettings.Log,
clusterSettings.ClusterDns,
clusterSettings.MaxDiscoverAttempts,
clusterSettings.ExternalGossipPort,
clusterSettings.GossipSeeds,
clusterSettings.GossipTimeout,
clusterSettings.NodePreference);
return new EventStoreNodeConnection(connectionSettings, clusterSettings, endPointDiscoverer,
connectionName);
}
throw new Exception($"Must specify uri or gossip seeds");
}
private static UserCredentials GetCredentialFromUri(Uri uri)
{
if (uri == null || string.IsNullOrEmpty(uri.UserInfo)) return null;
var pieces = uri.UserInfo.Split(':');
if (pieces.Length != 2) throw new Exception(string.Format("Unable to parse user information '{0}'", uri.UserInfo));
return new UserCredentials(pieces[0], pieces[1]);
}
private static Uri GetUriFromConnectionString(string connectionString)
{
var connto = ConnectionString.GetConnectionStringInfo(connectionString)
.FirstOrDefault(x => x.Key.ToUpperInvariant() == "CONNECTTO").Value;
return connto == null ? null : new Uri(connto);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> to single node using default <see cref="ConnectionSettings"/>
/// </summary>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <param name="tcpEndPoint">The <see cref="IPEndPoint"/> to connect to.</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(IPEndPoint tcpEndPoint, string connectionName = null)
{
return Create(ConnectionSettings.Default, tcpEndPoint, connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> to single node using specific <see cref="ConnectionSettings"/>
/// </summary>
/// <param name="connectionSettings">The <see cref="ConnectionSettings"/> to apply to the new connection</param>
/// <param name="tcpEndPoint">The <see cref="IPEndPoint"/> to connect to.</param>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(ConnectionSettings connectionSettings, IPEndPoint tcpEndPoint, string connectionName = null)
{
Ensure.NotNull(connectionSettings, "settings");
Ensure.NotNull(tcpEndPoint, "tcpEndPoint");
return new EventStoreNodeConnection(connectionSettings, null, new StaticEndPointDiscoverer(tcpEndPoint, connectionSettings.UseSslConnection), connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> to EventStore cluster
/// using specific <see cref="ConnectionSettings"/> and <see cref="ClusterSettings"/>
/// </summary>
/// <param name="connectionSettings">The <see cref="ConnectionSettings"/> to apply to the new connection</param>
/// <param name="clusterSettings">The <see cref="ClusterSettings"/> that determine cluster behavior.</param>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(ConnectionSettings connectionSettings, ClusterSettings clusterSettings, string connectionName = null)
{
Ensure.NotNull(connectionSettings, "connectionSettings");
Ensure.NotNull(clusterSettings, "clusterSettings");
var endPointDiscoverer = new ClusterDnsEndPointDiscoverer(connectionSettings.Log,
clusterSettings.ClusterDns,
clusterSettings.MaxDiscoverAttempts,
clusterSettings.ExternalGossipPort,
clusterSettings.GossipSeeds,
clusterSettings.GossipTimeout,
clusterSettings.NodePreference);
return new EventStoreNodeConnection(connectionSettings, clusterSettings, endPointDiscoverer, connectionName);
}
/// <summary>
/// Creates a new <see cref="IEventStoreConnection"/> using specific <see cref="ConnectionSettings"/> and a custom-defined <see cref="IEndPointDiscoverer"/>
/// </summary>
/// <param name="connectionSettings">The <see cref="ConnectionSettings"/> to apply to the new connection</param>
/// <param name="endPointDiscoverer">The custom-defined <see cref="IEndPointDiscoverer"/> to use for node discovery</param>
/// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
/// <returns>a new <see cref="IEventStoreConnection"/></returns>
public static IEventStoreConnection Create(ConnectionSettings connectionSettings, IEndPointDiscoverer endPointDiscoverer, string connectionName = null)
{
Ensure.NotNull(connectionSettings, "settings");
Ensure.NotNull(endPointDiscoverer, "endPointDiscoverer");
return new EventStoreNodeConnection(connectionSettings, null, endPointDiscoverer, connectionName);
}
}
}