Skip to content

Commit 3010d2c

Browse files
committed
Remove duplicate connections
1 parent ef7dcbd commit 3010d2c

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

discovery/etcd/etcd.go

+3-16
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (r *SvcDiscoveryRegistryImpl) initializeConnMap() error {
9494
if err != nil {
9595
return err
9696
}
97+
r.connMap = make(map[string][]*grpc.ClientConn)
9798
for _, kv := range resp.Kvs {
9899
prefix, addr := r.splitEndpoint(string(kv.Key))
99100
conn, err := grpc.DialContext(context.Background(), addr, append(r.dialOptions, grpc.WithResolvers(r.resolver))...)
@@ -102,7 +103,6 @@ func (r *SvcDiscoveryRegistryImpl) initializeConnMap() error {
102103
}
103104
r.connMap[prefix] = append(r.connMap[prefix], conn)
104105
}
105-
106106
return nil
107107
}
108108

@@ -213,23 +213,10 @@ func (r *SvcDiscoveryRegistryImpl) keepAliveLease(leaseID clientv3.LeaseID) {
213213
func (r *SvcDiscoveryRegistryImpl) watchServiceChanges() {
214214
watchChan := r.client.Watch(context.Background(), r.rootDirectory, clientv3.WithPrefix())
215215
for range watchChan {
216+
r.mu.RLock()
216217
r.initializeConnMap()
218+
r.mu.RUnlock()
217219
}
218-
219-
//watchChan := r.client.Watch(context.Background(), r.rootDirectory, clientv3.WithPrefix())
220-
//updatedPrefixes := make(map[string]struct{}) // Create a set to track updated prefixes
221-
//for watchResp := range watchChan {
222-
// for _, event := range watchResp.Events {
223-
// prefix, _ := r.splitEndpoint(string(event.Kv.Key))
224-
// if _, alreadyUpdated := updatedPrefixes[prefix]; !alreadyUpdated {
225-
// updatedPrefixes[prefix] = struct{}{} // Mark this prefix as updated
226-
// fmt.Println("refreshConnMap prefix", prefix, event)
227-
// r.refreshConnMap(prefix)
228-
// } else {
229-
// fmt.Println("no refreshConnMap prefix", prefix, event)
230-
// }
231-
// }
232-
//}
233220
}
234221

235222
// refreshConnMap fetches the latest endpoints and updates the local map

0 commit comments

Comments
 (0)