| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package etcdv3 |
| |
| import ( |
| "sync" |
| "time" |
| ) |
| |
| import ( |
| etcd "github.com/dubbogo/gost/database/kv/etcd/v3" |
| gxtime "github.com/dubbogo/gost/time" |
| |
| perrors "github.com/pkg/errors" |
| ) |
| |
| import ( |
| "github.com/apache/dubbo-go/common" |
| "github.com/apache/dubbo-go/common/constant" |
| "github.com/apache/dubbo-go/common/logger" |
| ) |
| |
| type clientFacade interface { |
| Client() *etcd.Client |
| SetClient(*etcd.Client) |
| ClientLock() *sync.Mutex |
| WaitGroup() *sync.WaitGroup //for wait group control, etcd client listener & etcd client container |
| Done() chan struct{} //for etcd client control |
| RestartCallBack() bool |
| common.Node |
| } |
| |
| // HandleClientRestart keeps the connection between client and server |
| func HandleClientRestart(r clientFacade) { |
| var ( |
| err error |
| failTimes int |
| ) |
| defer r.WaitGroup().Done() |
| LOOP: |
| for { |
| select { |
| case <-r.Done(): |
| logger.Warnf("(ETCDV3ProviderRegistry)reconnectETCDV3 goroutine exit now...") |
| return |
| case <-r.Client().Done(): |
| r.ClientLock().Lock() |
| clientName := etcd.RegistryETCDV3Client |
| timeout, _ := time.ParseDuration(r.GetURL().GetParam(constant.REGISTRY_TIMEOUT_KEY, constant.DEFAULT_REG_TIMEOUT)) |
| endpoints := r.Client().GetEndPoints() |
| r.Client().Close() |
| r.SetClient(nil) |
| r.ClientLock().Unlock() |
| |
| // try to connect to etcd, |
| failTimes = 0 |
| for { |
| select { |
| case <-r.Done(): |
| logger.Warnf("(ETCDV3ProviderRegistry)reconnectETCDRegistry goroutine exit now...") |
| break LOOP |
| case <-gxtime.After(timeSecondDuration(failTimes * ConnDelay)): // avoid connect frequent |
| } |
| err = ValidateClient( |
| r, |
| etcd.WithName(clientName), |
| etcd.WithEndpoints(endpoints...), |
| etcd.WithTimeout(timeout), |
| ) |
| logger.Infof("ETCDV3ProviderRegistry.validateETCDV3Client(etcd Addr{%s}) = error{%#v}", |
| endpoints, perrors.WithStack(err)) |
| if err == nil && r.RestartCallBack() { |
| break |
| } |
| failTimes++ |
| if MaxFailTimes <= failTimes { |
| failTimes = MaxFailTimes |
| } |
| } |
| // re-register all services |
| case <-r.Client().GetCtx().Done(): |
| r.RestartCallBack() |
| time.Sleep(10 * time.Millisecond) |
| } |
| } |
| } |
| |
| func timeSecondDuration(sec int) time.Duration { |
| return time.Duration(sec) * time.Second |
| } |