1 // 2 // Copyright 2021 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_SERVER_SERVER_CONFIG_SELECTOR_H 18 #define GRPC_SRC_CORE_SERVER_SERVER_CONFIG_SELECTOR_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <memory> 23 24 #include "absl/status/statusor.h" 25 #include "absl/strings/string_view.h" 26 #include "src/core/lib/transport/metadata_batch.h" 27 #include "src/core/service_config/service_config.h" 28 #include "src/core/service_config/service_config_parser.h" 29 #include "src/core/util/dual_ref_counted.h" 30 #include "src/core/util/ref_counted.h" 31 #include "src/core/util/ref_counted_ptr.h" 32 #include "src/core/util/useful.h" 33 34 namespace grpc_core { 35 36 // ServerConfigSelector allows for choosing the service config to apply to a 37 // server-side call based on the received initial metadata. 38 class ServerConfigSelector : public RefCounted<ServerConfigSelector> { 39 public: 40 // Configuration to apply to an incoming call 41 struct CallConfig { 42 const ServiceConfigParser::ParsedConfigVector* method_configs = nullptr; 43 RefCountedPtr<ServiceConfig> service_config; 44 }; 45 46 ~ServerConfigSelector() override = default; 47 48 // Returns the CallConfig to apply to a call based on the incoming \a metadata 49 virtual absl::StatusOr<CallConfig> GetCallConfig( 50 grpc_metadata_batch* metadata) = 0; 51 }; 52 53 // ServerConfigSelectorProvider allows for subscribers to watch for updates on 54 // ServerConfigSelector. It is propagated via channel args. 55 class ServerConfigSelectorProvider 56 : public DualRefCounted<ServerConfigSelectorProvider> { 57 public: 58 class ServerConfigSelectorWatcher { 59 public: 60 virtual ~ServerConfigSelectorWatcher() = default; 61 virtual void OnServerConfigSelectorUpdate( 62 absl::StatusOr<RefCountedPtr<ServerConfigSelector>> update) = 0; 63 }; 64 65 ~ServerConfigSelectorProvider() override = default; 66 // Only a single watcher is allowed at present 67 virtual absl::StatusOr<RefCountedPtr<ServerConfigSelector>> Watch( 68 std::unique_ptr<ServerConfigSelectorWatcher> watcher) = 0; 69 virtual void CancelWatch() = 0; 70 ChannelArgName()71 static absl::string_view ChannelArgName() { 72 return "grpc.internal.server_config_selector_provider"; 73 } ChannelArgsCompare(const ServerConfigSelectorProvider * a,const ServerConfigSelectorProvider * b)74 static int ChannelArgsCompare(const ServerConfigSelectorProvider* a, 75 const ServerConfigSelectorProvider* b) { 76 return QsortCompare(a, b); 77 } 78 }; 79 80 } // namespace grpc_core 81 82 #endif // GRPC_SRC_CORE_SERVER_SERVER_CONFIG_SELECTOR_H 83