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