dubbo.apache.org/dubbo-go/v3@v3.1.1/config_center/nacos/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 nacos
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  )
    24  
    25  import (
    26  	nacosClient "github.com/dubbogo/gost/database/kv/nacos"
    27  	"github.com/dubbogo/gost/log/logger"
    28  
    29  	perrors "github.com/pkg/errors"
    30  )
    31  
    32  import (
    33  	"dubbo.apache.org/dubbo-go/v3/remoting/nacos"
    34  )
    35  
    36  // NacosClient Nacos configClient
    37  type NacosClient struct {
    38  	name         string
    39  	NacosAddrs   []string
    40  	sync.Mutex   // for Client
    41  	configClient *nacosClient.NacosConfigClient
    42  	exit         chan struct{}
    43  	Timeout      time.Duration
    44  	once         sync.Once
    45  	onceClose    func()
    46  }
    47  
    48  // Client Get Client
    49  func (n *NacosClient) Client() *nacosClient.NacosConfigClient {
    50  	return n.configClient
    51  }
    52  
    53  // SetClient Set configClient
    54  func (n *NacosClient) SetClient(configClient *nacosClient.NacosConfigClient) {
    55  	n.Lock()
    56  	n.configClient = configClient
    57  	n.Unlock()
    58  }
    59  
    60  // ValidateNacosClient Validate nacos configClient , if null then create it
    61  func ValidateNacosClient(container nacosClientFacade) error {
    62  	if container == nil {
    63  		return perrors.Errorf("container can not be null")
    64  	}
    65  	url := container.GetURL()
    66  	if container.NacosClient() == nil || container.NacosClient().Client() == nil {
    67  		// in dubbo ,every registry only connect one node ,so this is []string{r.Address}
    68  		newClient, err := nacos.NewNacosConfigClientByUrl(url)
    69  		if err != nil {
    70  			logger.Errorf("ValidateNacosClient(name{%s}, nacos address{%v} = error{%v}", url.Location, err)
    71  			return perrors.WithMessagef(err, "newNacosClient(address:%+v)", url.Location)
    72  		}
    73  		container.SetNacosClient(newClient)
    74  	}
    75  	return perrors.WithMessagef(nil, "newNacosClient(address:%+v)", url.PrimitiveURL)
    76  }
    77  
    78  // Done Get nacos configClient exit signal
    79  func (n *NacosClient) Done() <-chan struct{} {
    80  	return n.exit
    81  }
    82  
    83  func (n *NacosClient) stop() bool {
    84  	select {
    85  	case <-n.exit:
    86  		return true
    87  	default:
    88  		n.once.Do(n.onceClose)
    89  	}
    90  
    91  	return false
    92  }
    93  
    94  // NacosClientValid Get nacos configClient valid status
    95  func (n *NacosClient) NacosClientValid() bool {
    96  	select {
    97  	case <-n.exit:
    98  		return false
    99  	default:
   100  	}
   101  
   102  	valid := true
   103  	n.Lock()
   104  	if n.Client() == nil {
   105  		valid = false
   106  	}
   107  	n.Unlock()
   108  
   109  	return valid
   110  }
   111  
   112  // Close Close nacos configClient , then set null
   113  func (n *NacosClient) Close() {
   114  	if n == nil {
   115  		return
   116  	}
   117  
   118  	n.stop()
   119  	n.SetClient(nil)
   120  }