NeverENG commented on code in PR #36: URL: https://github.com/apache/dubbo-net/pull/36#discussion_r4513683063
########## Registry/ProviderDiscovery.cs: ########## @@ -0,0 +1,236 @@ +/* + * 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. + */ + +using DubboNet.Clients.DataModle; +using DubboNet.Clients.RegistryClient; +using MyCommonHelper; +using org.apache.zookeeper; +using org.apache.zookeeper.data; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net; +using System.Threading; +using System.Threading.Tasks; + +namespace DubboNet.Registry +{ + // Owns this client's Nacos provider subscriptions and in-flight discovery. + // The client owns registry leases and its ZooKeeper watcher; the service driver + // alone owns callable endpoint state. Metadata has a separate subscriber. + internal sealed class ProviderDiscovery : IDisposable + { + private readonly MyZookeeper _zookeeper; + private readonly MyNacos _nacos; + private readonly string _rootPath; + private readonly Watcher _watcher; + private readonly Func<string, Task> _onNacosChanged; + private readonly Action _onRegistryRecovered; + private readonly ConcurrentDictionary<(string Service, bool FullPath), Lazy<Task<DubboServiceEndPointInfos>>> _inFlight = new(); + private int _disposed; + + internal ProviderDiscovery(MyZookeeper zookeeper, MyNacos nacos, string rootPath, + Watcher watcher, Func<string, Task> onNacosChanged, Action onRegistryRecovered) + { + if ((zookeeper is null) == (nacos is null)) + throw new ArgumentException("Exactly one Provider registry is required."); + if (string.IsNullOrWhiteSpace(rootPath)) + throw new ArgumentException("Provider registry root cannot be empty.", nameof(rootPath)); + if (zookeeper != null && watcher is null) + throw new ArgumentNullException(nameof(watcher)); + _zookeeper = zookeeper; + _nacos = nacos; + _rootPath = rootPath; + _watcher = watcher; + _onNacosChanged = onNacosChanged ?? throw new ArgumentNullException(nameof(onNacosChanged)); + _onRegistryRecovered = onRegistryRecovered ?? throw new ArgumentNullException(nameof(onRegistryRecovered)); + } + + internal bool IsDisposed => Volatile.Read(ref _disposed) != 0; + + public void Dispose() + { + if (Interlocked.Exchange(ref _disposed, 1) != 0) return; + // Remove only our Nacos Naming subscriptions. Metadata and other + // clients have distinct subscriber identities on the shared connection. + _nacos?.RemoveSubscriber(this); + // ZooKeeper watches are one-shot and have no per-watcher unsubscribe API; + // the owning client watcher checks IsDisposed before handling callbacks. + } + + private Task OnNacosChangedAsync(string serviceName) => + IsDisposed ? Task.CompletedTask : _onNacosChanged(serviceName); + + internal bool TryGetServiceNameFromProviderPath( + string providerPath, + out string serviceName) + { + serviceName = null; + if (string.IsNullOrEmpty(providerPath) + || !providerPath.StartsWith(_rootPath, StringComparison.Ordinal) + || !providerPath.EndsWith(ZooKeeperRegistryPaths.ProvidersSuffix, StringComparison.Ordinal)) + { + return false; + } + + serviceName = providerPath.Substring( + _rootPath.Length, + providerPath.Length - _rootPath.Length - ZooKeeperRegistryPaths.ProvidersSuffix.Length); + return !string.IsNullOrWhiteSpace(serviceName); + } + + internal async Task<DubboServiceEndPointInfos> GetProviderEndPointsAsync(string serviceName, bool isFullPath = false) Review Comment: 更正前一条回复:为避免 #34 在合并前指向尚不存在的代码,#34 已在 eab4c86 改回当前 main 的真实源码链接。现应先合并 #34,再在本 PR #36 内同步两种语言的路径、方法名和状态归属,完成验证后才合并 #36;两份 PR 描述已更新。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
