diff --git a/sd/etcd/client.go b/sd/etcd/client.go index 6a1ecb4..3e52f24 100644 --- a/sd/etcd/client.go +++ b/sd/etcd/client.go @@ -44,7 +44,7 @@ Key string CaCert string DialTimeout time.Duration - DialKeepAline time.Duration + DialKeepAlive time.Duration HeaderTimeoutPerRequest time.Duration } @@ -83,7 +83,7 @@ Dial: func(network, addr string) (net.Conn, error) { dial := &net.Dialer{ Timeout: options.DialTimeout, - KeepAlive: options.DialKeepAline, + KeepAlive: options.DialKeepAlive, } return dial.Dial(network, addr) }, diff --git a/sd/etcd/client_test.go b/sd/etcd/client_test.go index 1735b77..1caeb59 100644 --- a/sd/etcd/client_test.go +++ b/sd/etcd/client_test.go @@ -13,7 +13,7 @@ Key: "", CaCert: "", DialTimeout: (2 * time.Second), - DialKeepAline: (2 * time.Second), + DialKeepAlive: (2 * time.Second), HeaderTimeoutPerRequest: (2 * time.Second), } @@ -40,7 +40,7 @@ Key: "", CaCert: "", DialTimeout: (2 * time.Second), - DialKeepAline: (2 * time.Second), + DialKeepAlive: (2 * time.Second), HeaderTimeoutPerRequest: (2 * time.Second), }) @@ -60,7 +60,7 @@ Key: "blank.key", CaCert: "blank.cacert", DialTimeout: (2 * time.Second), - DialKeepAline: (2 * time.Second), + DialKeepAlive: (2 * time.Second), HeaderTimeoutPerRequest: (2 * time.Second), }) diff --git a/sd/etcd/example_test.go b/sd/etcd/example_test.go new file mode 100644 index 0000000..6272720 --- /dev/null +++ b/sd/etcd/example_test.go @@ -0,0 +1,69 @@ +package etcd + +import ( + "fmt" + "time" + "io" + + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/endpoint" + "golang.org/x/net/context" +) + +// Package sd/etcd provides a wrapper around the coroes/etcd key value store (https://github.com/coreos/etcd) +// This example assumes the user has an instance of etcd installed and running locally on port 2379 +func Example() { + + var ( + prefix = "/services/foosvc/" // known at compile time + instance = "1.2.3.4:8080" // taken from runtime or platform, somehow + key = prefix + instance + value = "http://" + instance // based on our transport + ) + + client, err := NewClient(context.Background(), []string{"http://:2379"}, ClientOptions{ + DialTimeout: 2 * time.Second, + DialKeepAlive: 2 * time.Second, + HeaderTimeoutPerRequest: 2 * time.Second, + }) + + // Instantiate new instance of *Registrar passing in test data + registrar := NewRegistrar(client, Service{ + Key: key, + Value: value, + }, log.NewNopLogger()) + // Register new test data to etcd + registrar.Register() + + //Retrieve entries from etcd + _, err = client.GetEntries(key) + if err != nil { + fmt.Println(err) + } + + factory := func(string) (endpoint.Endpoint, io.Closer, error) { + return endpoint.Nop, nil, nil + } + subscriber, _ := NewSubscriber(client, prefix, factory, log.NewNopLogger()) + + endpoints, err := subscriber.Endpoints() + if err != nil { + fmt.Printf("err: %v", err) + } + fmt.Println(len(endpoints)) // hopefully 1 + + // Deregister first instance of test data + registrar.Deregister() + + endpoints, err = subscriber.Endpoints() + if err != nil { + fmt.Printf("err: %v", err) + } + fmt.Println(len(endpoints)) // hopefully 0 + + // Verify test data no longer exists in etcd + _, err = client.GetEntries(key) + if err != nil { + fmt.Println(err) + } +} diff --git a/sd/etcd/integration_test.go b/sd/etcd/integration_test.go index 72027e0..aab8672 100644 --- a/sd/etcd/integration_test.go +++ b/sd/etcd/integration_test.go @@ -27,7 +27,7 @@ Key: "", CaCert: "", DialTimeout: (2 * time.Second), - DialKeepAline: (2 * time.Second), + DialKeepAlive: (2 * time.Second), HeaderTimeoutPerRequest: (2 * time.Second), }