From b2c773d26f6126517526919a31093d919302e688 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Fri, 29 Mar 2019 17:06:09 -0700 Subject: [PATCH] Global Registry for Service Config Parsers --- .../filters/client_channel/service_config.cc | 2 ++ .../filters/client_channel/service_config.h | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/core/ext/filters/client_channel/service_config.cc b/src/core/ext/filters/client_channel/service_config.cc index bbf671d979e..e2d23010c8a 100644 --- a/src/core/ext/filters/client_channel/service_config.cc +++ b/src/core/ext/filters/client_channel/service_config.cc @@ -33,6 +33,8 @@ namespace grpc_core { +int ServiceConfig::registered_parsers_count = 0; + RefCountedPtr ServiceConfig::Create(const char* json) { UniquePtr service_config_json(gpr_strdup(json)); UniquePtr json_string(gpr_strdup(json)); diff --git a/src/core/ext/filters/client_channel/service_config.h b/src/core/ext/filters/client_channel/service_config.h index d9063479e32..b211d732d3f 100644 --- a/src/core/ext/filters/client_channel/service_config.h +++ b/src/core/ext/filters/client_channel/service_config.h @@ -54,6 +54,15 @@ namespace grpc_core { +/// This is the base class that all service config parsers MUST use to store +/// parsed service config data. +class ServiceConfigParsedObject { + public: + virtual ~ServiceConfigParsedObject() = default; + + GRPC_ABSTRACT_BASE_CLASS; +}; + class ServiceConfig : public RefCounted { public: /// Creates a new service config from parsing \a json_string. @@ -96,6 +105,26 @@ class ServiceConfig : public RefCounted { static RefCountedPtr MethodConfigTableLookup( const SliceHashTable>& table, const grpc_slice& path); + /// Retrieves the parsed object at index \a index. + ServiceConfigParsedObject* GetParsedServiceConfigObject(int index) { + GPR_DEBUG_ASSERT(index < registered_parsers_count); + return parsed_service_config_objects[index].get(); + } + + typedef UniquePtr (*ServiceConfigParser)( + const char* service_config_json); + + /// Globally register a service config parser. On successful registration, it + /// returns the index at which the parser was registered. On failure, -1 is + /// returned. Each new service config update will go through all the + /// registered parser. Each parser is responsible for reading the service + /// config json and returning a parsed object. This parsed object can later be + /// retrieved using the same index that was returned at registration time. + static int RegisterParser(ServiceConfigParser func) { + registered_parsers[registered_parsers_count] = func; + return registered_parsers_count++; + } + private: // So New() can call our private ctor. template @@ -120,9 +149,16 @@ class ServiceConfig : public RefCounted { grpc_json* json, CreateValue create_value, typename SliceHashTable>::Entry* entries, size_t* idx); + static constexpr int kMaxParsers = 32; + static int registered_parsers_count; + static ServiceConfigParser registered_parsers[kMaxParsers]; + UniquePtr service_config_json_; UniquePtr json_string_; // Underlying storage for json_tree. grpc_json* json_tree_; + + InlinedVector, kMaxParsers> + parsed_service_config_objects; }; //