Created
January 9, 2015 16:15
-
-
Save ascjones/d215fdd1e8dfe4e8da06 to your computer and use it in GitHub Desktop.
Connect to EventStore Cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module EventStoreHelpers = | |
open System | |
open System.Net | |
open EventStore.ClientAPI | |
let inline private (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x) | |
let private dnsLookup (host : string) = | |
let hostEntry = Dns.GetHostEntry(host) | |
let nonLoopback = | |
hostEntry.AddressList | |
|> Seq.tryFind (fun a -> | |
not (a = IPAddress.Loopback) && | |
not (a = IPAddress.IPv6Loopback) && | |
not a.IsIPv6LinkLocal) | |
match nonLoopback with | |
| Some ip -> ip | |
| None -> IPAddress.Loopback | |
let createClusterConnection (endpoints : string seq) username pass = | |
let credentials = new SystemData.UserCredentials(username, pass) | |
let ipEndpoints = | |
endpoints | |
|> Seq.map (fun ep -> | |
let parts = ep.Split(':') | |
let hostPart,portPart = parts.[0],parts.[1] | |
let ip = dnsLookup hostPart | |
let port = Int32.Parse(portPart) | |
new IPEndPoint(ip, port)) | |
|> Seq.toArray | |
let connSettings = | |
!> ConnectionSettings.Create() | |
.SetDefaultUserCredentials(credentials) | |
let clusterSettings = | |
!> ClusterSettings.Create() | |
.DiscoverClusterViaGossipSeeds() | |
.SetGossipSeedEndPoints(ipEndpoints) | |
let conn = EventStoreConnection.Create(connSettings, clusterSettings) | |
conn.ConnectAsync () |> ignore | |
conn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment