summaryrefslogtreecommitdiff
path: root/grpc/src/core/ext/filters/client_channel/dynamic_filters.h
blob: 08a7f4985eb11ec21d5e42a6ff7589b78a69e034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_DYNAMIC_FILTERS_H
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_DYNAMIC_FILTERS_H

#include <grpc/support/port_platform.h>

#include <vector>

#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/gpr/time_precise.h"
#include "src/core/lib/gprpp/arena.h"
#include "src/core/lib/gprpp/ref_counted.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/polling_entity.h"

namespace grpc_core {

class DynamicFilters : public RefCounted<DynamicFilters> {
 public:
  // Implements the interface of RefCounted<>.
  class Call {
   public:
    struct Args {
      RefCountedPtr<DynamicFilters> channel_stack;
      grpc_polling_entity* pollent;
      grpc_slice path;
      gpr_cycle_counter start_time;
      grpc_millis deadline;
      Arena* arena;
      grpc_call_context_element* context;
      CallCombiner* call_combiner;
    };

    Call(Args args, grpc_error_handle* error);

    // Continues processing a transport stream op batch.
    void StartTransportStreamOpBatch(grpc_transport_stream_op_batch* batch);

    // Sets the 'then_schedule_closure' argument for call stack destruction.
    // Must be called once per call.
    void SetAfterCallStackDestroy(grpc_closure* closure);

    // Interface of RefCounted<>.
    RefCountedPtr<Call> Ref() GRPC_MUST_USE_RESULT;
    RefCountedPtr<Call> Ref(const DebugLocation& location,
                            const char* reason) GRPC_MUST_USE_RESULT;
    // When refcount drops to 0, destroys itself and the associated call stack,
    // but does NOT free the memory because it's in the call arena.
    void Unref();
    void Unref(const DebugLocation& location, const char* reason);

   private:
    // Allow RefCountedPtr<> to access IncrementRefCount().
    template <typename T>
    friend class RefCountedPtr;

    // Interface of RefCounted<>.
    void IncrementRefCount();
    void IncrementRefCount(const DebugLocation& location, const char* reason);

    static void Destroy(void* arg, grpc_error_handle error);

    RefCountedPtr<DynamicFilters> channel_stack_;
    grpc_closure* after_call_stack_destroy_ = nullptr;
  };

  static RefCountedPtr<DynamicFilters> Create(
      const grpc_channel_args* args,
      std::vector<const grpc_channel_filter*> filters);

  explicit DynamicFilters(grpc_channel_stack* channel_stack)
      : channel_stack_(channel_stack) {}

  ~DynamicFilters() override;

  RefCountedPtr<Call> CreateCall(Call::Args args, grpc_error_handle* error);

 private:
  grpc_channel_stack* channel_stack_;
};

}  // namespace grpc_core

#endif  // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_DYNAMIC_FILTERS_H