1 // 2 // Copyright 2015 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef GRPC_SRC_CORE_LIB_SERVICE_CONFIG_SERVICE_CONFIG_IMPL_H 18 #define GRPC_SRC_CORE_LIB_SERVICE_CONFIG_SERVICE_CONFIG_IMPL_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <stddef.h> 23 24 #include <memory> 25 #include <string> 26 #include <unordered_map> 27 #include <vector> 28 29 #include "absl/status/statusor.h" 30 #include "absl/strings/string_view.h" 31 32 #include <grpc/slice.h> 33 #include <grpc/support/log.h> 34 35 #include "src/core/lib/channel/channel_args.h" 36 #include "src/core/lib/gprpp/ref_counted_ptr.h" 37 #include "src/core/lib/gprpp/validation_errors.h" 38 #include "src/core/lib/json/json.h" 39 #include "src/core/lib/service_config/service_config.h" 40 #include "src/core/lib/service_config/service_config_parser.h" 41 #include "src/core/lib/slice/slice_internal.h" 42 43 // The main purpose of the code here is to parse the service config in 44 // JSON form, which will look like this: 45 // 46 // { 47 // "loadBalancingPolicy": "string", // optional 48 // "methodConfig": [ // array of one or more method_config objects 49 // { 50 // "name": [ // array of one or more name objects 51 // { 52 // "service": "string", // required 53 // "method": "string", // optional 54 // } 55 // ], 56 // // remaining fields are optional. 57 // // see https://developers.google.com/protocol-buffers/docs/proto3#json 58 // // for format details. 59 // "waitForReady": bool, 60 // "timeout": "duration_string", 61 // "maxRequestMessageBytes": "int64_string", 62 // "maxResponseMessageBytes": "int64_string", 63 // } 64 // ] 65 // } 66 67 namespace grpc_core { 68 69 class ServiceConfigImpl final : public ServiceConfig { 70 public: 71 /// Creates a new service config from parsing \a json_string. 72 static absl::StatusOr<RefCountedPtr<ServiceConfig>> Create( 73 const ChannelArgs& args, absl::string_view json_string); 74 75 // Alternate forms that are useful in edge cases. 76 static RefCountedPtr<ServiceConfig> Create(const ChannelArgs& args, 77 const Json& json, 78 absl::string_view json_string, 79 ValidationErrors* errors); 80 static RefCountedPtr<ServiceConfig> Create(const ChannelArgs& args, 81 const Json& json, 82 ValidationErrors* errors); 83 84 ~ServiceConfigImpl() override; 85 json_string()86 absl::string_view json_string() const override { return json_string_; } 87 88 /// Retrieves the global parsed config at index \a index. The 89 /// lifetime of the returned object is tied to the lifetime of the 90 /// ServiceConfig object. GetGlobalParsedConfig(size_t index)91 ServiceConfigParser::ParsedConfig* GetGlobalParsedConfig( 92 size_t index) override { 93 GPR_DEBUG_ASSERT(index < parsed_global_configs_.size()); 94 return parsed_global_configs_[index].get(); 95 } 96 97 /// Retrieves the vector of parsed configs for the method identified 98 /// by \a path. The lifetime of the returned vector and contained objects 99 /// is tied to the lifetime of the ServiceConfig object. 100 const ServiceConfigParser::ParsedConfigVector* GetMethodParsedConfigVector( 101 const grpc_slice& path) const override; 102 103 private: 104 std::string json_string_; 105 Json json_; 106 107 ServiceConfigParser::ParsedConfigVector parsed_global_configs_; 108 // A map from the method name to the parsed config vector. Note that we are 109 // using a raw pointer and not a unique pointer so that we can use the same 110 // vector for multiple names. 111 std::unordered_map<grpc_slice, const ServiceConfigParser::ParsedConfigVector*, 112 SliceHash> 113 parsed_method_configs_map_; 114 // Default method config. 115 const ServiceConfigParser::ParsedConfigVector* default_method_config_vector_ = 116 nullptr; 117 // Storage for all the vectors that are being used in 118 // parsed_method_configs_map_ and default_method_config_vector_. 119 std::vector<ServiceConfigParser::ParsedConfigVector> 120 parsed_method_config_vectors_storage_; 121 }; 122 123 } // namespace grpc_core 124 125 #endif // GRPC_SRC_CORE_LIB_SERVICE_CONFIG_SERVICE_CONFIG_IMPL_H 126