当前位置: 首页>>代码示例>>Golang>>正文


Golang Client.SetDialTimeout方法代码示例

本文整理汇总了Golang中github.com/coreos/go-etcd/etcd.Client.SetDialTimeout方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.SetDialTimeout方法的具体用法?Golang Client.SetDialTimeout怎么用?Golang Client.SetDialTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/coreos/go-etcd/etcd.Client的用法示例。


在下文中一共展示了Client.SetDialTimeout方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewEtcdClient

func NewEtcdClient(addr string, dialTimeout time.Duration) (*EtcdClient, error) {
	var c *api.Client
	/*
		var err error
		if cert != "" && key != "" {
			c, err = etcd.NewTLSClient(machines, cert, key, caCert)
			if err != nil {
				return &Client{c}, err
			}
		} else {
			c = etcd.NewClient(machines)
		}
	*/

	// machine addresses
	machines := []string{addr}

	// create custom client
	c = api.NewClient(machines)
	if !c.SetCluster(machines) {
		return nil, errors.New("cannot connect to etcd cluster: " + addr)
	}

	// configure dial timeout
	c.SetDialTimeout(dialTimeout)

	return &EtcdClient{addr: addr, client: c}, nil
}
开发者ID:haoyixin,项目名称:parkeeper,代码行数:28,代码来源:etcdclient.go

示例2: NewEtcdClient

// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
	var c *goetcd.Client
	var err error
	if cert != "" && key != "" {
		c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = goetcd.NewClient(machines)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)

	maxConnectAttempts := 10
	for attempt := 1; attempt <= maxConnectAttempts; attempt++ {
		success := c.SetCluster(machines)
		if success {
			break
			return &Client{c}, nil
		}

		if attempt == maxConnectAttempts {
			break
			return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
		}
		log.Info(fmt.Sprintf("[Attempt: %d] Attempting access to etcd after 5 second sleep", attempt))
		time.Sleep(5 * time.Second)
	}

	return &Client{c}, nil
}
开发者ID:Lineberty,项目名称:confd,代码行数:34,代码来源:client.go

示例3: NewEtcdClient

// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
	var c *etcd.Client
	if cert != "" && key != "" {
		c, err := etcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = etcd.NewClient(machines)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)
	success := c.SetCluster(machines)
	if !success {
		return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
	}
	return &Client{c}, nil
}
开发者ID:radartools,项目名称:confd,代码行数:20,代码来源:client.go

示例4: NewEtcdClient

// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
//func NewEtcdClient(machines []string, cert, key string, caCert string) (*Client, error) {
func NewEtcdClient(machines []string, cert, key string, caCert string, basicAuth bool, username string, password string) (*Client, error) {
	var c *goetcd.Client
	var err error
	if cert != "" && key != "" {
		c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = goetcd.NewClient(machines)
	}
	if basicAuth {
		c.SetCredentials(username, password)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)
	success := c.SetCluster(machines)
	if !success {
		return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
	}
	return &Client{c}, nil
}
开发者ID:tanbokan,项目名称:confd,代码行数:25,代码来源:client.go

示例5: NewEtcdClient

// NewEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func NewEtcdClient(machines []string, cert, key string, caCert string, noDiscover bool) (*Client, error) {
	var c *goetcd.Client
	var err error
	machines = prependSchemeToMachines(machines)
	if cert != "" && key != "" {
		c, err = goetcd.NewTLSClient(machines, cert, key, caCert)
		if err != nil {
			return &Client{c}, err
		}
	} else {
		c = goetcd.NewClient(machines)
	}
	// Configure the DialTimeout, since 1 second is often too short
	c.SetDialTimeout(time.Duration(3) * time.Second)

	// If noDiscover is not set, we should locate the whole etcd cluster.
	if !noDiscover {
		success := c.SetCluster(machines)
		if !success {
			return &Client{c}, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
		}
	}
	return &Client{c}, nil
}
开发者ID:projectcalico,项目名称:confd,代码行数:26,代码来源:client.go

示例6: main

func main() {

	var (
		nodes string

		taskList string
		tasks    []string

		confPath string
		confData *lib.RunConfig

		cliVars string
		cliData map[string]interface{}

		verbose bool
		taskDir string
		tmplDir string

		etcdClient *etcd.Client

		base *cobra.Command

		err error
	)

	base = &cobra.Command{
		Use:   "atom",
		Short: "atom",
		Long:  "atom",
		Run: func(cmd *cobra.Command, args []string) {

			confData, err = lib.ParseAtomConfig(confPath)
			if err != nil {
				log.Printf("Config read error: %s", err)
				if (taskDir == "") || (tmplDir == "") {
					return
				} else {
					confData = &lib.RunConfig{
						TaskDir: taskDir,
						TmplDir: tmplDir,
						Verbose: verbose,
					}
				}
			}

			if err := yaml.Unmarshal([]byte(cliVars), &cliData); err != nil {
				log.Printf("Error: %s", err)
			}

			if nodes == "" {
				etcdClient = nil
			} else {
				etcdClient = etcd.NewClient([]string{nodes})
				etcdClient.SetDialTimeout(time.Duration(3) * time.Second)
				if !etcdClient.SyncCluster() {
					log.Fatal("Couldn't sync with etcd cluster.")
				}
				etcdClient.SetConsistency(etcd.STRONG_CONSISTENCY)
			}

			for _, name := range strings.Split(taskList, ",") {
				if name == "" {
					continue
				}
				tasks = append(tasks, name)
			}

			if len(tasks) <= 0 {
				tasks = nil
			}

			atom := lib.NewAtom(confData, &cliData, etcdClient)
			atom.Run(tasks...)

		},
	}

	base.Flags().StringVarP(&taskList, "run", "r", "", "tasks to run")

	base.PersistentFlags().StringVarP(&cliVars, "data", "d", "", "cli data (string, yaml)")
	base.PersistentFlags().StringVarP(&confPath, "conf", "c", "/etc/atom/atom.yml", "atom.yml")
	base.PersistentFlags().StringVarP(&nodes, "nodes", "n", "", "etcd nodes")
	base.PersistentFlags().StringVarP(&taskDir, "task", "t", "", "task directory")
	base.PersistentFlags().StringVarP(&tmplDir, "template", "m", "", "template directory")
	base.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose")

	base.Execute()
}
开发者ID:psev,项目名称:atom,代码行数:88,代码来源:main.go


注:本文中的github.com/coreos/go-etcd/etcd.Client.SetDialTimeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。