dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/zookeeper/client.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package zookeeper
    19  
    20  import (
    21  	"strings"
    22  )
    23  
    24  import (
    25  	gxzookeeper "github.com/dubbogo/gost/database/kv/zk"
    26  	"github.com/dubbogo/gost/log/logger"
    27  
    28  	perrors "github.com/pkg/errors"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  )
    34  
    35  const (
    36  	ConnDelay    = 3 // connection delay interval
    37  	MaxFailTimes = 3 // max fail times
    38  )
    39  
    40  // ValidateZookeeperClient validates client and sets options
    41  func ValidateZookeeperClient(container ZkClientFacade, zkName string) error {
    42  	lock := container.ZkClientLock()
    43  	url := container.GetURL()
    44  
    45  	lock.Lock()
    46  	defer lock.Unlock()
    47  
    48  	if container.ZkClient() == nil {
    49  		// in dubbo, every registry only connect one node, so this is []string{r.Address}
    50  		timeout := url.GetParamDuration(constant.ConfigTimeoutKey, constant.DefaultRegTimeout)
    51  
    52  		zkAddresses := strings.Split(url.Location, ",")
    53  		logger.Infof("[Zookeeper Client] New zookeeper client with name = %s, zkAddress = %s, timeout = %s", zkName, url.Location, timeout.String())
    54  		newClient, cltErr := gxzookeeper.NewZookeeperClient(zkName, zkAddresses, true, gxzookeeper.WithZkTimeOut(timeout))
    55  		if cltErr != nil {
    56  			logger.Warnf("newZookeeperClient(name{%s}, zk address{%v}, timeout{%d}) = error{%v}",
    57  				zkName, url.Location, timeout.String(), cltErr)
    58  			return perrors.WithMessagef(cltErr, "newZookeeperClient(address:%+v)", url.Location)
    59  		}
    60  		container.SetZkClient(newClient)
    61  	}
    62  	return nil
    63  }