NewClient returns Client with a connection to the named machines. It will return an error if a connection to the cluster cannot be made.
(ctx context.Context, machines []string, options ClientOptions)
| 87 | // NewClient returns Client with a connection to the named machines. It will |
| 88 | // return an error if a connection to the cluster cannot be made. |
| 89 | func NewClient(ctx context.Context, machines []string, options ClientOptions) (Client, error) { |
| 90 | if options.DialTimeout == 0 { |
| 91 | options.DialTimeout = 3 * time.Second |
| 92 | } |
| 93 | if options.DialKeepAlive == 0 { |
| 94 | options.DialKeepAlive = 3 * time.Second |
| 95 | } |
| 96 | |
| 97 | var err error |
| 98 | var tlscfg *tls.Config |
| 99 | |
| 100 | if options.Cert != "" && options.Key != "" { |
| 101 | tlsInfo := transport.TLSInfo{ |
| 102 | CertFile: options.Cert, |
| 103 | KeyFile: options.Key, |
| 104 | TrustedCAFile: options.CACert, |
| 105 | } |
| 106 | tlscfg, err = tlsInfo.ClientConfig() |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | cli, err := clientv3.New(clientv3.Config{ |
| 113 | Context: ctx, |
| 114 | Endpoints: machines, |
| 115 | DialTimeout: options.DialTimeout, |
| 116 | DialKeepAliveTime: options.DialKeepAlive, |
| 117 | DialOptions: options.DialOptions, |
| 118 | TLS: tlscfg, |
| 119 | Username: options.Username, |
| 120 | Password: options.Password, |
| 121 | }) |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | |
| 126 | return &client{ |
| 127 | cli: cli, |
| 128 | ctx: ctx, |
| 129 | kv: clientv3.NewKV(cli), |
| 130 | }, nil |
| 131 | } |
| 132 | |
| 133 | func (c *client) LeaseID() int64 { return int64(c.leaseID) } |
| 134 |
no outgoing calls
searching dependent graphs…