You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

xds_server_builder.h 5.4 kB

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