1 // 2 // Copyright 2020 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_CLIENT_CHANNEL_DYNAMIC_FILTERS_H 18 #define GRPC_SRC_CORE_CLIENT_CHANNEL_DYNAMIC_FILTERS_H 19 20 #include <grpc/slice.h> 21 #include <grpc/support/port_platform.h> 22 23 #include <utility> 24 #include <vector> 25 26 #include "src/core/filter/blackboard.h" 27 #include "src/core/lib/channel/channel_args.h" 28 #include "src/core/lib/channel/channel_fwd.h" 29 #include "src/core/lib/channel/channel_stack.h" 30 #include "src/core/lib/iomgr/call_combiner.h" 31 #include "src/core/lib/iomgr/closure.h" 32 #include "src/core/lib/iomgr/error.h" 33 #include "src/core/lib/iomgr/polling_entity.h" 34 #include "src/core/lib/resource_quota/arena.h" 35 #include "src/core/lib/transport/transport.h" 36 #include "src/core/util/debug_location.h" 37 #include "src/core/util/ref_counted.h" 38 #include "src/core/util/ref_counted_ptr.h" 39 #include "src/core/util/time.h" 40 #include "src/core/util/time_precise.h" 41 42 namespace grpc_core { 43 44 class DynamicFilters final : public RefCounted<DynamicFilters> { 45 public: 46 // Implements the interface of RefCounted<>. 47 class Call { 48 public: 49 struct Args { 50 RefCountedPtr<DynamicFilters> channel_stack; 51 grpc_polling_entity* pollent; 52 grpc_slice path; 53 gpr_cycle_counter start_time; 54 Timestamp deadline; 55 Arena* arena; 56 CallCombiner* call_combiner; 57 }; 58 59 Call(Args args, grpc_error_handle* error); 60 61 // Continues processing a transport stream op batch. 62 void StartTransportStreamOpBatch(grpc_transport_stream_op_batch* batch); 63 64 // Sets the 'then_schedule_closure' argument for call stack destruction. 65 // Must be called once per call. 66 void SetAfterCallStackDestroy(grpc_closure* closure); 67 68 // Interface of RefCounted<>. 69 GRPC_MUST_USE_RESULT RefCountedPtr<Call> Ref(); 70 GRPC_MUST_USE_RESULT RefCountedPtr<Call> Ref(const DebugLocation& location, 71 const char* reason); 72 // When refcount drops to 0, destroys itself and the associated call stack, 73 // but does NOT free the memory because it's in the call arena. 74 void Unref(); 75 void Unref(const DebugLocation& location, const char* reason); 76 77 private: 78 // Allow RefCountedPtr<> to access IncrementRefCount(). 79 template <typename T> 80 friend class RefCountedPtr; 81 82 // Interface of RefCounted<>. 83 void IncrementRefCount(); 84 void IncrementRefCount(const DebugLocation& location, const char* reason); 85 86 static void Destroy(void* arg, grpc_error_handle error); 87 88 RefCountedPtr<DynamicFilters> channel_stack_; 89 grpc_closure* after_call_stack_destroy_ = nullptr; 90 }; 91 92 static RefCountedPtr<DynamicFilters> Create( 93 const ChannelArgs& args, std::vector<const grpc_channel_filter*> filters, 94 const Blackboard* old_blackboard, Blackboard* new_blackboard); 95 DynamicFilters(RefCountedPtr<grpc_channel_stack> channel_stack)96 explicit DynamicFilters(RefCountedPtr<grpc_channel_stack> channel_stack) 97 : channel_stack_(std::move(channel_stack)) {} 98 99 RefCountedPtr<Call> CreateCall(Call::Args args, grpc_error_handle* error); 100 channel_stack()101 grpc_channel_stack* channel_stack() const { return channel_stack_.get(); } 102 103 private: 104 RefCountedPtr<grpc_channel_stack> channel_stack_; 105 }; 106 107 } // namespace grpc_core 108 109 #endif // GRPC_SRC_CORE_CLIENT_CHANNEL_DYNAMIC_FILTERS_H 110