Skip to content

Instantly share code, notes, and snippets.

@ascjones
Created January 9, 2015 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ascjones/d215fdd1e8dfe4e8da06 to your computer and use it in GitHub Desktop.
Save ascjones/d215fdd1e8dfe4e8da06 to your computer and use it in GitHub Desktop.
Connect to EventStore Cluster
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