• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICE_CONFIG_SERVICE_CONFIG_IMPL_H
18 #define GRPC_SRC_CORE_SERVICE_CONFIG_SERVICE_CONFIG_IMPL_H
19 
20 #include <grpc/slice.h>
21 #include <grpc/support/port_platform.h>
22 #include <stddef.h>
23 
24 #include <memory>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include "absl/log/check.h"
30 #include "absl/status/statusor.h"
31 #include "absl/strings/string_view.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/slice/slice_internal.h"
34 #include "src/core/service_config/service_config.h"
35 #include "src/core/service_config/service_config_parser.h"
36 #include "src/core/util/json/json.h"
37 #include "src/core/util/ref_counted_ptr.h"
38 #include "src/core/util/validation_errors.h"
39 
40 // The main purpose of the code here is to parse the service config in
41 // JSON form, which will look like this:
42 //
43 // {
44 //   "loadBalancingPolicy": "string",  // optional
45 //   "methodConfig": [  // array of one or more method_config objects
46 //     {
47 //       "name": [  // array of one or more name objects
48 //         {
49 //           "service": "string",  // required
50 //           "method": "string",  // optional
51 //         }
52 //       ],
53 //       // remaining fields are optional.
54 //       // see https://developers.google.com/protocol-buffers/docs/proto3#json
55 //       // for format details.
56 //       "waitForReady": bool,
57 //       "timeout": "duration_string",
58 //       "maxRequestMessageBytes": "int64_string",
59 //       "maxResponseMessageBytes": "int64_string",
60 //     }
61 //   ]
62 // }
63 
64 namespace grpc_core {
65 
66 class ServiceConfigImpl final : public ServiceConfig {
67  public:
68   /// Creates a new service config from parsing \a json_string.
69   static absl::StatusOr<RefCountedPtr<ServiceConfig>> Create(
70       const ChannelArgs& args, absl::string_view json_string);
71 
72   // Alternate forms that are useful in edge cases.
73   static RefCountedPtr<ServiceConfig> Create(const ChannelArgs& args,
74                                              const Json& json,
75                                              absl::string_view json_string,
76                                              ValidationErrors* errors);
77   static RefCountedPtr<ServiceConfig> Create(const ChannelArgs& args,
78                                              const Json& json,
79                                              ValidationErrors* errors);
80 
81   ~ServiceConfigImpl() override;
82 
json_string()83   absl::string_view json_string() const override { return json_string_; }
84 
85   /// Retrieves the global parsed config at index \a index. The
86   /// lifetime of the returned object is tied to the lifetime of the
87   /// ServiceConfig object.
GetGlobalParsedConfig(size_t index)88   ServiceConfigParser::ParsedConfig* GetGlobalParsedConfig(
89       size_t index) override {
90     DCHECK(index < parsed_global_configs_.size());
91     return parsed_global_configs_[index].get();
92   }
93 
94   /// Retrieves the vector of parsed configs for the method identified
95   /// by \a path.  The lifetime of the returned vector and contained objects
96   /// is tied to the lifetime of the ServiceConfig object.
97   const ServiceConfigParser::ParsedConfigVector* GetMethodParsedConfigVector(
98       const grpc_slice& path) const override;
99 
100  private:
101   std::string json_string_;
102   Json json_;
103 
104   ServiceConfigParser::ParsedConfigVector parsed_global_configs_;
105   // A map from the method name to the parsed config vector. Note that we are
106   // using a raw pointer and not a unique pointer so that we can use the same
107   // vector for multiple names.
108   std::unordered_map<grpc_slice, const ServiceConfigParser::ParsedConfigVector*,
109                      SliceHash>
110       parsed_method_configs_map_;
111   // Default method config.
112   const ServiceConfigParser::ParsedConfigVector* default_method_config_vector_ =
113       nullptr;
114   // Storage for all the vectors that are being used in
115   // parsed_method_configs_map_ and default_method_config_vector_.
116   std::vector<ServiceConfigParser::ParsedConfigVector>
117       parsed_method_config_vectors_storage_;
118 };
119 
120 }  // namespace grpc_core
121 
122 #endif  // GRPC_SRC_CORE_SERVICE_CONFIG_SERVICE_CONFIG_IMPL_H
123