dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/etcdv3/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 etcdv3 19 20 import ( 21 gxetcd "github.com/dubbogo/gost/database/kv/etcd/v3" 22 "github.com/dubbogo/gost/log/logger" 23 24 perrors "github.com/pkg/errors" 25 ) 26 27 // ValidateClient validates client and sets options 28 func ValidateClient(container clientFacade, opts ...gxetcd.Option) error { 29 options := &gxetcd.Options{} 30 for _, opt := range opts { 31 opt(options) 32 } 33 lock := container.ClientLock() 34 lock.Lock() 35 defer lock.Unlock() 36 37 // new Client 38 if container.Client() == nil { 39 newClient, err := gxetcd.NewClient(options.Name, options.Endpoints, options.Timeout, options.Heartbeat) 40 if err != nil { 41 logger.Warnf("new etcd client (name{%s}, etcd addresses{%v}, timeout{%d}) = error{%v}", 42 options.Name, options.Endpoints, options.Timeout, err) 43 return perrors.WithMessagef(err, "new client (address:%+v)", options.Endpoints) 44 } 45 container.SetClient(newClient) 46 } 47 48 // Client lose connection with etcd server 49 if container.Client().GetRawClient() == nil { 50 newClient, err := gxetcd.NewClient(options.Name, options.Endpoints, options.Timeout, options.Heartbeat) 51 if err != nil { 52 logger.Warnf("new etcd client (name{%s}, etcd addresses{%v}, timeout{%d}) = error{%v}", 53 options.Name, options.Endpoints, options.Timeout, err) 54 return perrors.WithMessagef(err, "new client (address:%+v)", options.Endpoints) 55 } 56 container.SetClient(newClient) 57 } 58 59 return nil 60 } 61 62 // nolint 63 func NewServiceDiscoveryClient(opts ...gxetcd.Option) *gxetcd.Client { 64 options := &gxetcd.Options{ 65 Heartbeat: 1, // default heartbeat 66 } 67 for _, opt := range opts { 68 opt(options) 69 } 70 71 newClient, err := gxetcd.NewClient(options.Name, options.Endpoints, options.Timeout, options.Heartbeat) 72 if err != nil { 73 logger.Errorf("new etcd client (name{%s}, etcd addresses{%v}, timeout{%d}) = error{%v}", 74 options.Name, options.Endpoints, options.Timeout, err) 75 } 76 return newClient 77 }