• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CORE_EXT_FILTERS_CLIENT_CHANNEL_DYNAMIC_FILTERS_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_DYNAMIC_FILTERS_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <vector>
23 
24 #include "src/core/lib/channel/channel_stack.h"
25 #include "src/core/lib/gpr/time_precise.h"
26 #include "src/core/lib/gprpp/arena.h"
27 #include "src/core/lib/gprpp/ref_counted.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/iomgr/polling_entity.h"
30 
31 namespace grpc_core {
32 
33 class DynamicFilters : public RefCounted<DynamicFilters> {
34  public:
35   // Implements the interface of RefCounted<>.
36   class Call {
37    public:
38     struct Args {
39       RefCountedPtr<DynamicFilters> channel_stack;
40       grpc_polling_entity* pollent;
41       grpc_slice path;
42       gpr_cycle_counter start_time;
43       grpc_millis deadline;
44       Arena* arena;
45       grpc_call_context_element* context;
46       CallCombiner* call_combiner;
47     };
48 
49     Call(Args args, grpc_error** error);
50 
51     // Continues processing a transport stream op batch.
52     void StartTransportStreamOpBatch(grpc_transport_stream_op_batch* batch);
53 
54     // Sets the 'then_schedule_closure' argument for call stack destruction.
55     // Must be called once per call.
56     void SetAfterCallStackDestroy(grpc_closure* closure);
57 
58     // Interface of RefCounted<>.
59     RefCountedPtr<Call> Ref() GRPC_MUST_USE_RESULT;
60     RefCountedPtr<Call> Ref(const DebugLocation& location,
61                             const char* reason) GRPC_MUST_USE_RESULT;
62     // When refcount drops to 0, destroys itself and the associated call stack,
63     // but does NOT free the memory because it's in the call arena.
64     void Unref();
65     void Unref(const DebugLocation& location, const char* reason);
66 
67    private:
68     // Allow RefCountedPtr<> to access IncrementRefCount().
69     template <typename T>
70     friend class RefCountedPtr;
71 
72     // Interface of RefCounted<>.
73     void IncrementRefCount();
74     void IncrementRefCount(const DebugLocation& location, const char* reason);
75 
76     static void Destroy(void* arg, grpc_error* error);
77 
78     RefCountedPtr<DynamicFilters> channel_stack_;
79     grpc_closure* after_call_stack_destroy_ = nullptr;
80   };
81 
82   static RefCountedPtr<DynamicFilters> Create(
83       const grpc_channel_args* args,
84       std::vector<const grpc_channel_filter*> filters);
85 
DynamicFilters(grpc_channel_stack * channel_stack)86   explicit DynamicFilters(grpc_channel_stack* channel_stack)
87       : channel_stack_(channel_stack) {}
88 
89   ~DynamicFilters() override;
90 
91   RefCountedPtr<Call> CreateCall(Call::Args args, grpc_error** error);
92 
93  private:
94   grpc_channel_stack* channel_stack_;
95 };
96 
97 }  // namespace grpc_core
98 
99 #endif  // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_DYNAMIC_FILTERS_H
100