func New()

in store/engine/consul/consul.go [74:133]


func New(id string, cfg *Config) (*Consul, error) {
	if len(id) == 0 {
		return nil, errors.New("id must NOT be a empty string")
	}

	if len(cfg.Addrs) == 0 {
		return nil, errors.New("Consul address must be provided")
	}

	clientConfig := &api.Config{
		Address: cfg.Addrs[0],
	}

	if cfg.TLS.Enable {
		clientConfig.Scheme = configSchemeWithTLS
		tlsConfig := api.TLSConfig{
			CertFile: cfg.TLS.CertFile,
			KeyFile:  cfg.TLS.KeyFile,
			CAFile:   cfg.TLS.CAFile,
		}
		clientConfig.TLSConfig = tlsConfig
	}

	client, err := api.NewClient(clientConfig)
	if err != nil {
		return nil, err
	}

	electPath := defaultElectPath
	if cfg.ElectPath != "" {
		electPath = cfg.ElectPath
	}

	watchPlanParams := map[string]interface{}{
		"type": "key",
		"key":  electPath,
	}

	watchPlan, err := watch.Parse(watchPlanParams)
	if err != nil {
		return nil, err
	}

	c := &Consul{
		myID:           id,
		electPath:      electPath,
		client:         client,
		watchPlan:      watchPlan,
		leaderChangeCh: make(chan bool),
		lockReleaseCh:  make(chan bool),
		electionCh:     make(chan bool),
		quitCh:         make(chan bool),
	}
	c.watchPlan.Handler = c.watchHandler
	c.isReady.Store(false)
	c.wg.Add(2)
	go c.electLoop()
	go c.runWatch()
	return c, nil
}