• 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 #include <grpc/support/port_platform.h>
18 
19 #include "src/core/ext/filters/client_channel/service_config_parser.h"
20 
21 #include <grpc/support/log.h>
22 
23 namespace grpc_core {
24 
25 namespace {
26 typedef absl::InlinedVector<std::unique_ptr<ServiceConfigParser::Parser>,
27                             ServiceConfigParser::kNumPreallocatedParsers>
28     ServiceConfigParserList;
29 ServiceConfigParserList* g_registered_parsers;
30 }  // namespace
31 
Init()32 void ServiceConfigParser::Init() {
33   GPR_ASSERT(g_registered_parsers == nullptr);
34   g_registered_parsers = new ServiceConfigParserList();
35 }
36 
Shutdown()37 void ServiceConfigParser::Shutdown() {
38   delete g_registered_parsers;
39   g_registered_parsers = nullptr;
40 }
41 
RegisterParser(std::unique_ptr<Parser> parser)42 size_t ServiceConfigParser::RegisterParser(std::unique_ptr<Parser> parser) {
43   g_registered_parsers->push_back(std::move(parser));
44   return g_registered_parsers->size() - 1;
45 }
46 
47 ServiceConfigParser::ParsedConfigVector
ParseGlobalParameters(const grpc_channel_args * args,const Json & json,grpc_error ** error)48 ServiceConfigParser::ParseGlobalParameters(const grpc_channel_args* args,
49                                            const Json& json,
50                                            grpc_error** error) {
51   ParsedConfigVector parsed_global_configs;
52   std::vector<grpc_error*> error_list;
53   for (size_t i = 0; i < g_registered_parsers->size(); i++) {
54     grpc_error* parser_error = GRPC_ERROR_NONE;
55     auto parsed_config = (*g_registered_parsers)[i]->ParseGlobalParams(
56         args, json, &parser_error);
57     if (parser_error != GRPC_ERROR_NONE) {
58       error_list.push_back(parser_error);
59     }
60     parsed_global_configs.push_back(std::move(parsed_config));
61   }
62   if (!error_list.empty()) {
63     *error = GRPC_ERROR_CREATE_FROM_VECTOR("Global Params", &error_list);
64   }
65   return parsed_global_configs;
66 }
67 
68 ServiceConfigParser::ParsedConfigVector
ParsePerMethodParameters(const grpc_channel_args * args,const Json & json,grpc_error ** error)69 ServiceConfigParser::ParsePerMethodParameters(const grpc_channel_args* args,
70                                               const Json& json,
71                                               grpc_error** error) {
72   ParsedConfigVector parsed_method_configs;
73   std::vector<grpc_error*> error_list;
74   for (size_t i = 0; i < g_registered_parsers->size(); i++) {
75     grpc_error* parser_error = GRPC_ERROR_NONE;
76     auto parsed_config = (*g_registered_parsers)[i]->ParsePerMethodParams(
77         args, json, &parser_error);
78     if (parser_error != GRPC_ERROR_NONE) {
79       error_list.push_back(parser_error);
80     }
81     parsed_method_configs.push_back(std::move(parsed_config));
82   }
83   if (!error_list.empty()) {
84     *error = GRPC_ERROR_CREATE_FROM_VECTOR("methodConfig", &error_list);
85   }
86   return parsed_method_configs;
87 }
88 
89 }  // namespace grpc_core
90