GehaFearless commented on code in PR #1464:
URL: 
https://github.com/apache/incubator-pegasus/pull/1464#discussion_r1186611829


##########
src/runtime/rpc/dns_resolver.cpp:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+
+#include "runtime/rpc/dns_resolver.h"
+
+#include <utility>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#include "utils/fmt_logging.h"
+
+namespace dsn {
+
+void dns_resolver::add_item(const host_port &hp, const rpc_address &addr)
+{
+    utils::auto_write_lock l(_lock);
+    _dsn_cache.insert(std::make_pair(hp, addr));
+}
+
+bool dns_resolver::get_cached_addresses(const host_port &hp, 
std::vector<rpc_address> &addresses)
+{
+    utils::auto_read_lock l(_lock);
+    const auto &found = _dsn_cache.find(hp);
+    if (found == _dsn_cache.end()) {
+        return false;
+    }
+
+    addresses = {found->second};
+    return true;
+}
+
+error_s dns_resolver::resolve_addresses(const host_port &hp, 
std::vector<rpc_address> &addresses)
+{
+    CHECK(addresses.empty(), "invalid addresses, not empty");
+    if (get_cached_addresses(hp, addresses)) {
+        return error_s::ok();
+    }
+
+    std::vector<rpc_address> resolved_addresses;
+    RETURN_NOT_OK(hp.resolve_addresses(resolved_addresses));
+
+    {
+        utils::auto_write_lock l(_lock);
+        if (resolved_addresses.size() > 1) {
+            LOG_WARNING("host_port '{}' resolves to {} different addresses, 
using {}.",
+                        hp,
+                        resolved_addresses.size(),
+                        resolved_addresses[0]);
+        }
+        _dsn_cache.insert(std::make_pair(hp, resolved_addresses[0]));
+    }
+
+    addresses = std::move(resolved_addresses);
+    return error_s::ok();
+}
+
+rpc_address dns_resolver::resolve_address(const host_port &hp)
+{
+    switch (hp.type()) {
+    case HOST_TYPE_GROUP: {
+        rpc_address addr;
+        auto group_address = hp.group_host_port();
+        addr.assign_group(group_address->name());
+
+        for (const auto &hp : group_address->members()) {
+            CHECK_TRUE(addr.group_address()->add(resolve_address(hp)));

Review Comment:
   not need, `rpc_group_address::add()` check it



-- 
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]

Reply via email to