wwbmmm commented on code in PR #3767:
URL: https://github.com/apache/brpc/pull/3767#discussion_r4727154661


##########
src/brpc/server.cpp:
##########
@@ -686,24 +687,120 @@ int Server::AddBuiltinServices() {
     return 0;
 }
 
-BUTIL_FORCE_INLINE bool is_http_protocol(const char* name) {
-    if (name[0] != 'h') {
-        return false;
+BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
+    return strcmp(name, "rdma_handshake") == 0;
+}
+
+// Restful services are served through http/h2 requests. Empty maps may be
+// left by RemoveService(), so check the number of mappings instead of the
+// existence of the map.
+static bool HasRestfulServices(const Server::ServiceMap& services,
+                               const RestfulMap* global_restful_map) {
+    if (global_restful_map != nullptr && global_restful_map->size() != 0) {
+        return true;
+    }
+    for (Server::ServiceMap::const_iterator it = services.begin();
+         it != services.end(); ++it) {
+        if (it->second.restful_map != nullptr &&
+            it->second.restful_map->size() != 0) {
+            return true;
+        }
     }
-    return strcmp(name, "http") == 0 || strcmp(name, "h2") == 0;
+    return false;
 }
 
-BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
-    return strcmp(name, "rdma_handshake") == 0;
+static std::string JoinWithAnd(const std::vector<std::string>& strs) {
+    std::string out;
+    for (size_t i = 0; i < strs.size(); ++i) {
+        if (i != 0) {
+            out.append(i + 1 == strs.size() ? " and " : ", ");
+        }
+        out.append(strs[i]);
+    }
+    return out;
 }
 
-Acceptor* Server::BuildAcceptor() {
+static void ParseEnabledProtocols(const std::string& enabled_protocols,
+                                  std::unordered_set<std::string>* whitelist) {
+    for (butil::StringSplitter sp(enabled_protocols.c_str(), ' '); sp; ++sp) {
+        whitelist->insert(std::string(sp.field(), sp.length()));
+    }
+}
+
+// Returns true if `a' and `b' list the same set of protocols, in which case 
the
+// handlers built from one of them also match the other.
+static bool SameEnabledProtocols(const std::string& a, const std::string& b) {
+    std::unordered_set<std::string> wa;
+    std::unordered_set<std::string> wb;
+    ParseEnabledProtocols(a, &wa);
+    ParseEnabledProtocols(b, &wb);
+    return wa == wb;
+}
+
+int Server::ValidateEnabledProtocols(const ServerOptions& options) const {
     std::unordered_set<std::string> whitelist;
-    for (butil::StringSplitter sp(_options.enabled_protocols.c_str(), ' ');
-         sp; ++sp) {
-        std::string protocol(sp.field(), sp.length());
-        whitelist.insert(protocol);
+    ParseEnabledProtocols(options.enabled_protocols, &whitelist);
+    if (whitelist.empty()) {
+        return 0;
+    }
+    const bool http_in_whitelist =
+        whitelist.count("http") != 0 || whitelist.count("h2") != 0;
+    std::vector<Protocol> protocols;
+    ListProtocols(&protocols);
+    for (size_t i = 0; i < protocols.size(); ++i) {
+        if (protocols[i].process_request != nullptr) {
+            whitelist.erase(protocols[i].name);
+        }
+    }
+    if (!whitelist.empty()) {
+        std::ostringstream err;
+        err << "ServerOptions.enabled_protocols has unknown protocols=`";
+        for (const auto& protocol : whitelist) {
+            err << protocol << ' ';
+        }
+        err << '\'';
+        LOG(ERROR) << err.str();
+        errno = EINVAL;
+        return -1;
+    }
+    if (http_in_whitelist) {
+        return 0;
+    }
+    std::vector<std::string> features;
+    std::vector<std::string> fixes;
+    if (_builtin_service_count > 0) {
+        // Builtin services added by a previous Start() stay registered, so the
+        // configuration cannot get rid of them anymore.
+        features.push_back("builtin services are already registered");
+        fixes.push_back("create a new Server with "
+                        "ServerOptions.has_builtin_services=false");
+    } else if (options.has_builtin_services) {
+        features.push_back("ServerOptions.has_builtin_services is true");
+        fixes.push_back("set ServerOptions.has_builtin_services=false");
+    }
+    if (HasRestfulServices(_fullname_service_map, _global_restful_map)) {
+        features.push_back("restful services are added");
+        fixes.push_back("remove the restful mappings");
+    }
+    if (options.http_master_service != nullptr) {
+        features.push_back("ServerOptions.http_master_service is set");
+        fixes.push_back("clear ServerOptions.http_master_service");
+    }
+    if (features.empty()) {
+        return 0;
     }
+    LOG(ERROR) << "ServerOptions.enabled_protocols=`"
+               << options.enabled_protocols << "' enables neither http nor "
+               << "h2, but " << JoinWithAnd(features)
+               << ". They are served through http/h2, add `http' (or `h2') to "
+               << "enabled_protocols, or " << JoinWithAnd(fixes) << '.';

Review Comment:
   [replied by brpc-oncall robot] Fixed: when builtin services are already 
registered and `has_builtin_services` is now false, the diagnostic no longer 
suggests adding `http` (it cannot pass the consistency check) and only suggests 
creating a new `Server`. `registered_builtin_services_hint_to_use_new_server` 
asserts that `add \`http'` is absent from the message.



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