Codebase list golang-github-go-kit-kit / 07d2ab7
Adding testable example documention. Fixing typo in ClientOptions mstrong 7 years ago
4 changed file(s) with 75 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
4343 Key string
4444 CaCert string
4545 DialTimeout time.Duration
46 DialKeepAline time.Duration
46 DialKeepAlive time.Duration
4747 HeaderTimeoutPerRequest time.Duration
4848 }
4949
8282 Dial: func(network, addr string) (net.Conn, error) {
8383 dial := &net.Dialer{
8484 Timeout: options.DialTimeout,
85 KeepAlive: options.DialKeepAline,
85 KeepAlive: options.DialKeepAlive,
8686 }
8787 return dial.Dial(network, addr)
8888 },
1212 Key: "",
1313 CaCert: "",
1414 DialTimeout: (2 * time.Second),
15 DialKeepAline: (2 * time.Second),
15 DialKeepAlive: (2 * time.Second),
1616 HeaderTimeoutPerRequest: (2 * time.Second),
1717 }
1818
3939 Key: "",
4040 CaCert: "",
4141 DialTimeout: (2 * time.Second),
42 DialKeepAline: (2 * time.Second),
42 DialKeepAlive: (2 * time.Second),
4343 HeaderTimeoutPerRequest: (2 * time.Second),
4444 })
4545
5959 Key: "blank.key",
6060 CaCert: "blank.cacert",
6161 DialTimeout: (2 * time.Second),
62 DialKeepAline: (2 * time.Second),
62 DialKeepAlive: (2 * time.Second),
6363 HeaderTimeoutPerRequest: (2 * time.Second),
6464 })
6565
0 package etcd
1
2 import (
3 "fmt"
4 "time"
5 "io"
6
7 "github.com/go-kit/kit/log"
8 "github.com/go-kit/kit/endpoint"
9 "golang.org/x/net/context"
10 )
11
12 // Package sd/etcd provides a wrapper around the coroes/etcd key value store (https://github.com/coreos/etcd)
13 // This example assumes the user has an instance of etcd installed and running locally on port 2379
14 func Example() {
15
16 var (
17 prefix = "/services/foosvc/" // known at compile time
18 instance = "1.2.3.4:8080" // taken from runtime or platform, somehow
19 key = prefix + instance
20 value = "http://" + instance // based on our transport
21 )
22
23 client, err := NewClient(context.Background(), []string{"http://:2379"}, ClientOptions{
24 DialTimeout: 2 * time.Second,
25 DialKeepAlive: 2 * time.Second,
26 HeaderTimeoutPerRequest: 2 * time.Second,
27 })
28
29 // Instantiate new instance of *Registrar passing in test data
30 registrar := NewRegistrar(client, Service{
31 Key: key,
32 Value: value,
33 }, log.NewNopLogger())
34 // Register new test data to etcd
35 registrar.Register()
36
37 //Retrieve entries from etcd
38 _, err = client.GetEntries(key)
39 if err != nil {
40 fmt.Println(err)
41 }
42
43 factory := func(string) (endpoint.Endpoint, io.Closer, error) {
44 return endpoint.Nop, nil, nil
45 }
46 subscriber, _ := NewSubscriber(client, prefix, factory, log.NewNopLogger())
47
48 endpoints, err := subscriber.Endpoints()
49 if err != nil {
50 fmt.Printf("err: %v", err)
51 }
52 fmt.Println(len(endpoints)) // hopefully 1
53
54 // Deregister first instance of test data
55 registrar.Deregister()
56
57 endpoints, err = subscriber.Endpoints()
58 if err != nil {
59 fmt.Printf("err: %v", err)
60 }
61 fmt.Println(len(endpoints)) // hopefully 0
62
63 // Verify test data no longer exists in etcd
64 _, err = client.GetEntries(key)
65 if err != nil {
66 fmt.Println(err)
67 }
68 }
2626 Key: "",
2727 CaCert: "",
2828 DialTimeout: (2 * time.Second),
29 DialKeepAline: (2 * time.Second),
29 DialKeepAlive: (2 * time.Second),
3030 HeaderTimeoutPerRequest: (2 * time.Second),
3131 }
3232