GRPC Core  18.0.0
service_config.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_H
19 
21 
22 #include <unordered_map>
23 
24 #include "absl/container/inlined_vector.h"
25 
28 
33 #include "src/core/lib/json/json.h"
35 
36 // The main purpose of the code here is to parse the service config in
37 // JSON form, which will look like this:
38 //
39 // {
40 // "loadBalancingPolicy": "string", // optional
41 // "methodConfig": [ // array of one or more method_config objects
42 // {
43 // "name": [ // array of one or more name objects
44 // {
45 // "service": "string", // required
46 // "method": "string", // optional
47 // }
48 // ],
49 // // remaining fields are optional.
50 // // see https://developers.google.com/protocol-buffers/docs/proto3#json
51 // // for format details.
52 // "waitForReady": bool,
53 // "timeout": "duration_string",
54 // "maxRequestMessageBytes": "int64_string",
55 // "maxResponseMessageBytes": "int64_string",
56 // }
57 // ]
58 // }
59 
60 namespace grpc_core {
61 
62 // TODO(roth): Consider stripping this down further to the completely minimal
63 // interface requied to be exposed as part of the resolver API.
64 class ServiceConfig : public RefCounted<ServiceConfig> {
65  public:
69  absl::string_view json_string,
71 
72  ServiceConfig(const grpc_channel_args* args, std::string json_string,
74  ~ServiceConfig() override;
75 
76  const std::string& json_string() const { return json_string_; }
77 
82  GPR_DEBUG_ASSERT(index < parsed_global_configs_.size());
83  return parsed_global_configs_[index].get();
84  }
85 
90  const grpc_slice& path) const;
91 
92  private:
93  // Helper functions for parsing the method configs.
94  grpc_error_handle ParsePerMethodParams(const grpc_channel_args* args);
95  grpc_error_handle ParseJsonMethodConfig(const grpc_channel_args* args,
96  const Json& json);
97 
98  // Returns a path string for the JSON name object specified by json.
99  // Sets *error on error.
100  static std::string ParseJsonMethodName(const Json& json,
102 
103  std::string json_string_;
104  Json json_;
105 
106  absl::InlinedVector<std::unique_ptr<ServiceConfigParser::ParsedConfig>,
108  parsed_global_configs_;
109  // A map from the method name to the parsed config vector. Note that we are
110  // using a raw pointer and not a unique pointer so that we can use the same
111  // vector for multiple names.
112  std::unordered_map<grpc_slice, const ServiceConfigParser::ParsedConfigVector*,
113  SliceHash>
114  parsed_method_configs_map_;
115  // Default method config.
116  const ServiceConfigParser::ParsedConfigVector* default_method_config_vector_ =
117  nullptr;
118  // Storage for all the vectors that are being used in
119  // parsed_method_configs_table_.
120  absl::InlinedVector<std::unique_ptr<ServiceConfigParser::ParsedConfigVector>,
121  32>
122  parsed_method_config_vectors_storage_;
123 };
124 
125 } // namespace grpc_core
126 
127 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_H */
Definition: json.h:38
Definition: ref_counted.h:282
Definition: ref_counted_ptr.h:35
Definition: service_config.h:64
ServiceConfig(const grpc_channel_args *args, std::string json_string, Json json, grpc_error_handle *error)
Definition: service_config.cc:43
static RefCountedPtr< ServiceConfig > Create(const grpc_channel_args *args, absl::string_view json_string, grpc_error_handle *error)
Creates a new service config from parsing json_string.
Definition: service_config.cc:33
~ServiceConfig() override
Definition: service_config.cc:66
ServiceConfigParser::ParsedConfig * GetGlobalParsedConfig(size_t index)
Retrieves the global parsed config at index index.
Definition: service_config.h:81
const std::string & json_string() const
Definition: service_config.h:76
const ServiceConfigParser::ParsedConfigVector * GetMethodParsedConfigVector(const grpc_slice &path) const
Retrieves the vector of parsed configs for the method identified by path.
Definition: service_config.cc:207
This is the base class that all service config parsers MUST use to store parsed service config data.
Definition: service_config_parser.h:39
static constexpr int kNumPreallocatedParsers
Definition: service_config_parser.h:68
absl::InlinedVector< std::unique_ptr< ParsedConfig >, kNumPreallocatedParsers > ParsedConfigVector
Definition: service_config_parser.h:71
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:101
struct grpc_slice grpc_slice
Definition: slice.h:28
grpc_error_handle error
Definition: lame_client.cc:54
Round Robin Policy.
Definition: backend_metric.cc:26
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: slice_internal.h:353
Definition: error_internal.h:41
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60