// SetTLS sets the tls configuration given a tls.Config scheme func setTLS(cfg *etcd.Config, tls *tls.Config, addrs []string) { entries := store.CreateEndpoints(addrs, "https") cfg.Endpoints = entries // Set transport t := http.Transport{ Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, TLSClientConfig: tls, } cfg.Transport = &t }
// New creates a new Etcd client given a list // of endpoints and an optional tls config func New(addrs []string, options *store.Config) (store.Store, error) { s := &Etcd{} var ( entries []string err error ) entries = store.CreateEndpoints(addrs, "http") cfg := &etcd.Config{ Endpoints: entries, Transport: etcd.DefaultTransport, HeaderTimeoutPerRequest: 3 * time.Second, } // Set options if options != nil { if options.TLS != nil { setTLS(cfg, options.TLS, addrs) } if options.ConnectionTimeout != 0 { setTimeout(cfg, options.ConnectionTimeout) } } c, err := etcd.New(*cfg) if err != nil { log.Fatal(err) } s.client = etcd.NewKeysAPI(c) // Periodic Cluster Sync go func() { for { if err := c.AutoSync(context.Background(), periodicSync); err != nil { return } } }() return s, nil }