grpclb icon indicating copy to clipboard operation
grpclb copied to clipboard

client in UnRegister is not initialized

Open XuHewen opened this issue 6 years ago • 1 comments

func UnRegister() error {
	stopSignal <- true
	stopSignal = make(chan bool, 1) // just a hack to avoid multi UnRegister deadlock

	var err error
	if _, err := client.Delete(context.Background(), serviceKey); err != nil {
		log.Printf("grpclb: deregister '%s' failed: %s", serviceKey, err.Error())
	} else {
		log.Printf("grpclb: deregister '%s' ok.", serviceKey)
	}

	return err
}
var (
	// Prefix should start and end with no slash
	Prefix     = "etcd3_naming"
	client     etcd3.Client
	serviceKey string

	stopSignal = make(chan bool, 1)
)
func Registry(opt Opt, args ...int) error {
	serviceValue := fmt.Sprintf("%s:%d", opt.Host, opt.Port)
	serviceKey := fmt.Sprintf("%s/%s/%s", Prefix, opt.Name, serviceValue)

	var (
		err    error
		client *etcd3.Client
	)

	if len(args) > 0 {
		client, err = etcd3.New(etcd3.Config{
			Endpoints: strings.Split(opt.Target, ","),
		})
	} else {
		client, err = etcd3.NewFromURL(opt.Target)
	}
......
}

XuHewen avatar May 05 '19 03:05 XuHewen

Aftering commenting this line, function UnRegister can work:

func Registry(opt Opt, args ...int) error {
	serviceValue := fmt.Sprintf("%s:%d", opt.Host, opt.Port)
	serviceKey := fmt.Sprintf("%s/%s/%s", Prefix, opt.Name, serviceValue)

	var (
		err    error
		// client *etcd3.Client
	)

	if len(args) > 0 {
		client, err = etcd3.New(etcd3.Config{
			Endpoints: strings.Split(opt.Target, ","),
		})
	} else {
		client, err = etcd3.NewFromURL(opt.Target)
	}
......
}

XuHewen avatar May 05 '19 03:05 XuHewen