1 // Copyright 2022 gRPC authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GRPC_SRC_CORE_EXT_FILTERS_CHANNEL_IDLE_LEGACY_CHANNEL_IDLE_FILTER_H 16 #define GRPC_SRC_CORE_EXT_FILTERS_CHANNEL_IDLE_LEGACY_CHANNEL_IDLE_FILTER_H 17 18 #include <grpc/impl/connectivity_state.h> 19 #include <grpc/support/port_platform.h> 20 21 #include <memory> 22 23 #include "absl/status/status.h" 24 #include "absl/status/statusor.h" 25 #include "src/core/ext/filters/channel_idle/idle_filter_state.h" 26 #include "src/core/lib/channel/channel_args.h" 27 #include "src/core/lib/channel/channel_fwd.h" 28 #include "src/core/lib/channel/channel_stack.h" 29 #include "src/core/lib/channel/promise_based_filter.h" 30 #include "src/core/lib/promise/activity.h" 31 #include "src/core/lib/promise/arena_promise.h" 32 #include "src/core/lib/transport/connectivity_state.h" 33 #include "src/core/lib/transport/transport.h" 34 #include "src/core/util/orphanable.h" 35 #include "src/core/util/ref_counted_ptr.h" 36 #include "src/core/util/single_set_ptr.h" 37 #include "src/core/util/time.h" 38 39 namespace grpc_core { 40 41 Duration GetClientIdleTimeout(const ChannelArgs& args); 42 43 class LegacyChannelIdleFilter : public ChannelFilter { 44 public: LegacyChannelIdleFilter(grpc_channel_stack * channel_stack,Duration client_idle_timeout)45 LegacyChannelIdleFilter(grpc_channel_stack* channel_stack, 46 Duration client_idle_timeout) 47 : channel_stack_(channel_stack), 48 client_idle_timeout_(client_idle_timeout) {} 49 50 ~LegacyChannelIdleFilter() override = default; 51 52 LegacyChannelIdleFilter(const LegacyChannelIdleFilter&) = delete; 53 LegacyChannelIdleFilter& operator=(const LegacyChannelIdleFilter&) = delete; 54 LegacyChannelIdleFilter(LegacyChannelIdleFilter&&) = default; 55 LegacyChannelIdleFilter& operator=(LegacyChannelIdleFilter&&) = default; 56 57 // Construct a promise for one call. 58 ArenaPromise<ServerMetadataHandle> MakeCallPromise( 59 CallArgs call_args, NextPromiseFactory next_promise_factory) override; 60 61 bool StartTransportOp(grpc_transport_op* op) override; 62 63 protected: 64 using SingleSetActivityPtr = 65 SingleSetPtr<Activity, typename ActivityPtr::deleter_type>; 66 channel_stack()67 grpc_channel_stack* channel_stack() { return channel_stack_; }; 68 69 virtual void Shutdown(); 70 void CloseChannel(absl::string_view reason); 71 72 void IncreaseCallCount(); 73 void DecreaseCallCount(); 74 75 private: 76 void StartIdleTimer(); 77 78 struct CallCountDecreaser { operatorCallCountDecreaser79 void operator()(LegacyChannelIdleFilter* filter) const { 80 filter->DecreaseCallCount(); 81 } 82 }; 83 84 // The channel stack to which we take refs for pending callbacks. 85 grpc_channel_stack* channel_stack_; 86 Duration client_idle_timeout_; 87 std::shared_ptr<IdleFilterState> idle_filter_state_{ 88 std::make_shared<IdleFilterState>(false)}; 89 90 SingleSetActivityPtr activity_; 91 }; 92 93 class LegacyClientIdleFilter final : public LegacyChannelIdleFilter { 94 public: 95 static const grpc_channel_filter kFilter; 96 TypeName()97 static absl::string_view TypeName() { return "client_idle"; } 98 99 static absl::StatusOr<std::unique_ptr<LegacyClientIdleFilter>> Create( 100 const ChannelArgs& args, ChannelFilter::Args filter_args); 101 102 using LegacyChannelIdleFilter::LegacyChannelIdleFilter; 103 }; 104 105 class LegacyMaxAgeFilter final : public LegacyChannelIdleFilter { 106 public: 107 static const grpc_channel_filter kFilter; 108 struct Config; 109 TypeName()110 static absl::string_view TypeName() { return "max_age"; } 111 112 static absl::StatusOr<std::unique_ptr<LegacyMaxAgeFilter>> Create( 113 const ChannelArgs& args, ChannelFilter::Args filter_args); 114 115 LegacyMaxAgeFilter(grpc_channel_stack* channel_stack, 116 const Config& max_age_config); 117 118 void PostInit() override; 119 120 private: 121 class ConnectivityWatcher : public AsyncConnectivityStateWatcherInterface { 122 public: ConnectivityWatcher(LegacyMaxAgeFilter * filter)123 explicit ConnectivityWatcher(LegacyMaxAgeFilter* filter) 124 : channel_stack_(filter->channel_stack()->Ref()), filter_(filter) {} 125 ~ConnectivityWatcher() override = default; 126 OnConnectivityStateChange(grpc_connectivity_state new_state,const absl::Status &)127 void OnConnectivityStateChange(grpc_connectivity_state new_state, 128 const absl::Status&) override { 129 if (new_state == GRPC_CHANNEL_SHUTDOWN) filter_->Shutdown(); 130 } 131 132 private: 133 RefCountedPtr<grpc_channel_stack> channel_stack_; 134 LegacyMaxAgeFilter* filter_; 135 }; 136 137 void Shutdown() override; 138 139 SingleSetActivityPtr max_age_activity_; 140 Duration max_connection_age_; 141 Duration max_connection_age_grace_; 142 }; 143 144 } // namespace grpc_core 145 146 #endif // GRPC_SRC_CORE_EXT_FILTERS_CHANNEL_IDLE_LEGACY_CHANNEL_IDLE_FILTER_H 147