• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PARSER_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <memory>
23 
24 #include "absl/container/inlined_vector.h"
25 
26 #include <grpc/impl/codegen/grpc_types.h>
27 
28 #include "src/core/lib/iomgr/error.h"
29 #include "src/core/lib/json/json.h"
30 
31 namespace grpc_core {
32 
33 // Service config parser registry.
34 // See service_config.h for more information.
35 class ServiceConfigParser {
36  public:
37   /// This is the base class that all service config parsers MUST use to store
38   /// parsed service config data.
39   class ParsedConfig {
40    public:
41     virtual ~ParsedConfig() = default;
42   };
43 
44   /// This is the base class that all service config parsers should derive from.
45   class Parser {
46    public:
47     virtual ~Parser() = default;
48 
ParseGlobalParams(const grpc_channel_args *,const Json &,grpc_error_handle * error)49     virtual std::unique_ptr<ParsedConfig> ParseGlobalParams(
50         const grpc_channel_args*, const Json& /* json */,
51         grpc_error_handle* error) {
52       // Avoid unused parameter warning on debug-only parameter
53       (void)error;
54       GPR_DEBUG_ASSERT(error != nullptr);
55       return nullptr;
56     }
57 
ParsePerMethodParams(const grpc_channel_args *,const Json &,grpc_error_handle * error)58     virtual std::unique_ptr<ParsedConfig> ParsePerMethodParams(
59         const grpc_channel_args*, const Json& /* json */,
60         grpc_error_handle* error) {
61       // Avoid unused parameter warning on debug-only parameter
62       (void)error;
63       GPR_DEBUG_ASSERT(error != nullptr);
64       return nullptr;
65     }
66   };
67 
68   static constexpr int kNumPreallocatedParsers = 4;
69   typedef absl::InlinedVector<std::unique_ptr<ParsedConfig>,
70                               kNumPreallocatedParsers>
71       ParsedConfigVector;
72 
73   static void Init();
74   static void Shutdown();
75 
76   /// Globally register a service config parser. On successful registration, it
77   /// returns the index at which the parser was registered. On failure, -1 is
78   /// returned. Each new service config update will go through all the
79   /// registered parser. Each parser is responsible for reading the service
80   /// config json and returning a parsed config. This parsed config can later be
81   /// retrieved using the same index that was returned at registration time.
82   static size_t RegisterParser(std::unique_ptr<Parser> parser);
83 
84   static ParsedConfigVector ParseGlobalParameters(const grpc_channel_args* args,
85                                                   const Json& json,
86                                                   grpc_error_handle* error);
87 
88   static ParsedConfigVector ParsePerMethodParameters(
89       const grpc_channel_args* args, const Json& json,
90       grpc_error_handle* error);
91 };
92 
93 }  // namespace grpc_core
94 
95 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H */
96