1 // 2 // 3 // Copyright 2020 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPCPP_XDS_SERVER_BUILDER_H 20 #define GRPCPP_XDS_SERVER_BUILDER_H 21 22 #include <grpc/support/port_platform.h> 23 #include <grpcpp/server_builder.h> 24 25 namespace grpc { 26 27 class XdsServerServingStatusNotifierInterface { 28 public: 29 struct ServingStatusUpdate { 30 grpc::Status status; 31 }; 32 33 virtual ~XdsServerServingStatusNotifierInterface() = default; 34 35 // \a uri contains the listening target associated with the notification. Note 36 // that a single target provided to XdsServerBuilder can get resolved to 37 // multiple listening addresses. 38 // The callback is invoked each time there is an update to the serving status. 39 // The API does not provide any guarantees around duplicate updates. 40 // Status::OK signifies that the server is serving, while a non-OK status 41 // signifies that the server is not serving. 42 virtual void OnServingStatusUpdate(std::string uri, 43 ServingStatusUpdate update) = 0; 44 }; 45 46 class XdsServerBuilder : public grpc::ServerBuilder { 47 public: 48 // NOTE: class experimental_type is not part of the public API of this class 49 // TODO(yashykt): Integrate into public API when this is no longer 50 // experimental. 51 class experimental_type : public grpc::ServerBuilder::experimental_type { 52 public: experimental_type(XdsServerBuilder * builder)53 explicit experimental_type(XdsServerBuilder* builder) 54 : ServerBuilder::experimental_type(builder), builder_(builder) {} 55 56 // EXPERIMENTAL: Sets the drain grace period in ms for older connections 57 // when updates to a Listener is received. set_drain_grace_time(int drain_grace_time_ms)58 void set_drain_grace_time(int drain_grace_time_ms) { 59 builder_->drain_grace_time_ms_ = drain_grace_time_ms; 60 } 61 62 private: 63 XdsServerBuilder* builder_; 64 }; 65 66 // It is the responsibility of the application to make sure that \a notifier 67 // outlasts the life of the server. Notifications will start being made 68 // asynchronously once `BuildAndStart()` has been called. Note that it is 69 // possible for notifications to be made before `BuildAndStart()` returns. set_status_notifier(XdsServerServingStatusNotifierInterface * notifier)70 void set_status_notifier(XdsServerServingStatusNotifierInterface* notifier) { 71 notifier_ = notifier; 72 } 73 74 /// NOTE: The function experimental() is not stable public API. It is a view 75 /// to the experimental components of this class. It may be changed or removed 76 /// at any time. experimental()77 experimental_type experimental() { return experimental_type(this); } 78 79 private: 80 // Called at the beginning of BuildAndStart(). 81 ChannelArguments BuildChannelArgs() override; 82 OnServingStatusUpdate(void * user_data,const char * uri,grpc_serving_status_update update)83 static void OnServingStatusUpdate(void* user_data, const char* uri, 84 grpc_serving_status_update update) { 85 if (user_data == nullptr) return; 86 XdsServerServingStatusNotifierInterface* notifier = 87 static_cast<XdsServerServingStatusNotifierInterface*>(user_data); 88 notifier->OnServingStatusUpdate( 89 uri, {grpc::Status(static_cast<StatusCode>(update.code), 90 update.error_message)}); 91 } 92 93 XdsServerServingStatusNotifierInterface* notifier_ = nullptr; 94 int drain_grace_time_ms_ = -1; 95 }; 96 97 } // namespace grpc 98 99 #endif // GRPCPP_XDS_SERVER_BUILDER_H 100