xds_server_builder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef GRPCPP_XDS_SERVER_BUILDER_H
  19. #define GRPCPP_XDS_SERVER_BUILDER_H
  20. #include <grpc/support/port_platform.h>
  21. #include <grpcpp/server_builder.h>
  22. namespace grpc {
  23. class XdsServerServingStatusNotifierInterface {
  24. public:
  25. struct ServingStatusUpdate {
  26. grpc::Status status;
  27. };
  28. virtual ~XdsServerServingStatusNotifierInterface() = default;
  29. // \a uri contains the listening target associated with the notification. Note
  30. // that a single target provided to XdsServerBuilder can get resolved to
  31. // multiple listening addresses.
  32. // The callback is invoked each time there is an update to the serving status.
  33. // The API does not provide any guarantees around duplicate updates.
  34. // Status::OK signifies that the server is serving, while a non-OK status
  35. // signifies that the server is not serving.
  36. virtual void OnServingStatusUpdate(TString uri,
  37. ServingStatusUpdate update) = 0;
  38. };
  39. class XdsServerBuilder : public grpc::ServerBuilder {
  40. public:
  41. // NOTE: class experimental_type is not part of the public API of this class
  42. // TODO(yashykt): Integrate into public API when this is no longer
  43. // experimental.
  44. class experimental_type : public grpc::ServerBuilder::experimental_type {
  45. public:
  46. explicit experimental_type(XdsServerBuilder* builder)
  47. : ServerBuilder::experimental_type(builder), builder_(builder) {}
  48. // EXPERIMENTAL: Sets the drain grace period in ms for older connections
  49. // when updates to a Listener is received.
  50. void set_drain_grace_time(int drain_grace_time_ms) {
  51. builder_->drain_grace_time_ms_ = drain_grace_time_ms;
  52. }
  53. private:
  54. XdsServerBuilder* builder_;
  55. };
  56. // It is the responsibility of the application to make sure that \a notifier
  57. // outlasts the life of the server. Notifications will start being made
  58. // asynchronously once `BuildAndStart()` has been called. Note that it is
  59. // possible for notifications to be made before `BuildAndStart()` returns.
  60. void set_status_notifier(XdsServerServingStatusNotifierInterface* notifier) {
  61. notifier_ = notifier;
  62. }
  63. /// NOTE: The function experimental() is not stable public API. It is a view
  64. /// to the experimental components of this class. It may be changed or removed
  65. /// at any time.
  66. experimental_type experimental() { return experimental_type(this); }
  67. private:
  68. // Called at the beginning of BuildAndStart().
  69. ChannelArguments BuildChannelArgs() override {
  70. ChannelArguments args = ServerBuilder::BuildChannelArgs();
  71. if (drain_grace_time_ms_ >= 0) {
  72. args.SetInt(GRPC_ARG_SERVER_CONFIG_CHANGE_DRAIN_GRACE_TIME_MS,
  73. drain_grace_time_ms_);
  74. }
  75. grpc_channel_args c_channel_args = args.c_channel_args();
  76. grpc_server_config_fetcher* fetcher = grpc_server_config_fetcher_xds_create(
  77. {OnServingStatusUpdate, notifier_}, &c_channel_args);
  78. if (fetcher != nullptr) set_fetcher(fetcher);
  79. return args;
  80. }
  81. static void OnServingStatusUpdate(void* user_data, const char* uri,
  82. grpc_serving_status_update update) {
  83. if (user_data == nullptr) return;
  84. XdsServerServingStatusNotifierInterface* notifier =
  85. static_cast<XdsServerServingStatusNotifierInterface*>(user_data);
  86. notifier->OnServingStatusUpdate(
  87. uri, {grpc::Status(static_cast<StatusCode>(update.code),
  88. update.error_message)});
  89. }
  90. XdsServerServingStatusNotifierInterface* notifier_ = nullptr;
  91. int drain_grace_time_ms_ = -1;
  92. };
  93. } // namespace grpc
  94. #endif // GRPCPP_XDS_SERVER_BUILDER_H