github.com/dubbogo/gost@v1.14.0/database/kv/nacos/naming_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  	"sync/atomic"
    23  )
    24  
    25  import (
    26  	"github.com/nacos-group/nacos-sdk-go/v2/clients"
    27  	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
    28  	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
    29  	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    30  )
    31  
    32  var (
    33  	namingClientPool nacosClientPool
    34  	clientPoolOnce   sync.Once
    35  )
    36  
    37  type nacosClientPool struct {
    38  	sync.Mutex
    39  	namingClient map[string]*NacosNamingClient
    40  }
    41  
    42  type NacosNamingClient struct {
    43  	name        string
    44  	clientLock  sync.Mutex // for Client
    45  	client      naming_client.INamingClient
    46  	config      vo.NacosClientParam //conn config
    47  	valid       uint32
    48  	activeCount uint32
    49  	share       bool
    50  }
    51  
    52  func initNacosClientPool() {
    53  	namingClientPool.namingClient = make(map[string]*NacosNamingClient)
    54  }
    55  
    56  // NewNacosNamingClient create nacos client
    57  func NewNacosNamingClient(name string, share bool, sc []constant.ServerConfig,
    58  	cc constant.ClientConfig) (*NacosNamingClient, error) {
    59  
    60  	namingClient := &NacosNamingClient{
    61  		name:        name,
    62  		activeCount: 0,
    63  		share:       share,
    64  		config:      vo.NacosClientParam{ClientConfig: &cc, ServerConfigs: sc},
    65  	}
    66  	if !share {
    67  		return namingClient, namingClient.newNamingClient()
    68  	}
    69  	clientPoolOnce.Do(initNacosClientPool)
    70  	namingClientPool.Lock()
    71  	defer namingClientPool.Unlock()
    72  	if client, ok := namingClientPool.namingClient[name]; ok {
    73  		client.activeCount++
    74  		return client, nil
    75  	}
    76  
    77  	err := namingClient.newNamingClient()
    78  	if err == nil {
    79  		namingClientPool.namingClient[name] = namingClient
    80  	}
    81  	return namingClient, err
    82  }
    83  
    84  // newNamingClient create NamingClient
    85  func (n *NacosNamingClient) newNamingClient() error {
    86  	client, err := clients.NewNamingClient(n.config)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	n.activeCount++
    91  	atomic.StoreUint32(&n.valid, 1)
    92  	n.client = client
    93  	return nil
    94  }
    95  
    96  // Client Get NacosNamingClient
    97  func (n *NacosNamingClient) Client() naming_client.INamingClient {
    98  	return n.client
    99  }
   100  
   101  // SetClient Set NacosNamingClient
   102  func (n *NacosNamingClient) SetClient(client naming_client.INamingClient) {
   103  	n.clientLock.Lock()
   104  	n.client = client
   105  	n.clientLock.Unlock()
   106  }
   107  
   108  // NacosClientValid Get nacos client valid status
   109  func (n *NacosNamingClient) NacosClientValid() bool {
   110  
   111  	return atomic.LoadUint32(&n.valid) == 1
   112  }
   113  
   114  // Close close client
   115  func (n *NacosNamingClient) Close() {
   116  	namingClientPool.Lock()
   117  	defer namingClientPool.Unlock()
   118  	if n.client == nil {
   119  		return
   120  	}
   121  	n.activeCount--
   122  	if n.share {
   123  		if n.activeCount == 0 {
   124  			n.client = nil
   125  			atomic.StoreUint32(&n.valid, 0)
   126  			delete(namingClientPool.namingClient, n.name)
   127  		}
   128  	} else {
   129  		n.client = nil
   130  		atomic.StoreUint32(&n.valid, 0)
   131  		delete(namingClientPool.namingClient, n.name)
   132  	}
   133  }