GRPC C++  1.39.1
subchannel_interface.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 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 
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_INTERFACE_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_INTERFACE_H
21 
23 
26 
29 
30 namespace grpc_core {
31 
32 // The interface for subchannels that is exposed to LB policy implementations.
33 class SubchannelInterface : public RefCounted<SubchannelInterface> {
34  public:
36  public:
38 
39  // Will be invoked whenever the subchannel's connectivity state
40  // changes. There will be only one invocation of this method on a
41  // given watcher instance at any given time.
43  grpc_connectivity_state new_state) = 0;
44 
45  // TODO(roth): Remove this as soon as we move to EventManager-based
46  // polling.
48  };
49 
50  explicit SubchannelInterface(const char* trace = nullptr)
51  : RefCounted<SubchannelInterface>(trace) {}
52 
53  ~SubchannelInterface() override = default;
54 
55  // Returns the current connectivity state of the subchannel.
57 
58  // Starts watching the subchannel's connectivity state.
59  // The first callback to the watcher will be delivered when the
60  // subchannel's connectivity state becomes a value other than
61  // initial_state, which may happen immediately.
62  // Subsequent callbacks will be delivered as the subchannel's state
63  // changes.
64  // The watcher will be destroyed either when the subchannel is
65  // destroyed or when CancelConnectivityStateWatch() is called.
66  // There can be only one watcher of a given subchannel. It is not
67  // valid to call this method a second time without first cancelling
68  // the previous watcher using CancelConnectivityStateWatch().
69  virtual void WatchConnectivityState(
70  grpc_connectivity_state initial_state,
71  std::unique_ptr<ConnectivityStateWatcherInterface> watcher) = 0;
72 
73  // Cancels a connectivity state watch.
74  // If the watcher has already been destroyed, this is a no-op.
77 
78  // Attempt to connect to the backend. Has no effect if already connected.
79  // If the subchannel is currently in backoff delay due to a previously
80  // failed attempt, the new connection attempt will not start until the
81  // backoff delay has elapsed.
82  virtual void AttemptToConnect() = 0;
83 
84  // Resets the subchannel's connection backoff state. If AttemptToConnect()
85  // has been called since the subchannel entered TRANSIENT_FAILURE state,
86  // starts a new connection attempt immediately; otherwise, a new connection
87  // attempt will be started as soon as AttemptToConnect() is called.
88  virtual void ResetBackoff() = 0;
89 
90  // TODO(roth): Need a better non-grpc-specific abstraction here.
91  virtual const grpc_channel_args* channel_args() = 0;
92 };
93 
94 // A class that delegates to another subchannel, to be used in cases
95 // where an LB policy needs to wrap a subchannel.
97  public:
99  : wrapped_subchannel_(std::move(subchannel)) {}
100 
102  return wrapped_subchannel_;
103  }
104 
106  return wrapped_subchannel_->CheckConnectivityState();
107  }
109  grpc_connectivity_state initial_state,
110  std::unique_ptr<ConnectivityStateWatcherInterface> watcher) override {
111  return wrapped_subchannel_->WatchConnectivityState(initial_state,
112  std::move(watcher));
113  }
116  return wrapped_subchannel_->CancelConnectivityStateWatch(watcher);
117  }
118  void AttemptToConnect() override { wrapped_subchannel_->AttemptToConnect(); }
119  void ResetBackoff() override { wrapped_subchannel_->ResetBackoff(); }
120  const grpc_channel_args* channel_args() override {
121  return wrapped_subchannel_->channel_args();
122  }
123 
124  private:
125  RefCountedPtr<SubchannelInterface> wrapped_subchannel_;
126 };
127 
128 } // namespace grpc_core
129 
130 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_INTERFACE_H */
ClusterWatcher * watcher
Definition: cds.cc:112
Definition: subchannel_interface.h:96
void ResetBackoff() override
Definition: subchannel_interface.h:119
const grpc_channel_args * channel_args() override
Definition: subchannel_interface.h:120
void AttemptToConnect() override
Definition: subchannel_interface.h:118
void WatchConnectivityState(grpc_connectivity_state initial_state, std::unique_ptr< ConnectivityStateWatcherInterface > watcher) override
Definition: subchannel_interface.h:108
RefCountedPtr< SubchannelInterface > wrapped_subchannel() const
Definition: subchannel_interface.h:101
void CancelConnectivityStateWatch(ConnectivityStateWatcherInterface *watcher) override
Definition: subchannel_interface.h:114
grpc_connectivity_state CheckConnectivityState() override
Definition: subchannel_interface.h:105
DelegatingSubchannel(RefCountedPtr< SubchannelInterface > subchannel)
Definition: subchannel_interface.h:98
Definition: ref_counted.h:282
Definition: ref_counted_ptr.h:35
virtual void OnConnectivityStateChange(grpc_connectivity_state new_state)=0
Definition: subchannel_interface.h:33
virtual void WatchConnectivityState(grpc_connectivity_state initial_state, std::unique_ptr< ConnectivityStateWatcherInterface > watcher)=0
SubchannelInterface(const char *trace=nullptr)
Definition: subchannel_interface.h:50
~SubchannelInterface() override=default
virtual void CancelConnectivityStateWatch(ConnectivityStateWatcherInterface *watcher)=0
virtual grpc_connectivity_state CheckConnectivityState()=0
virtual const grpc_channel_args * channel_args()=0
virtual void AttemptToConnect()=0
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:27
Round Robin Policy.
Definition: backend_metric.cc:26
Definition: async_unary_call.h:398
struct grpc_pollset_set grpc_pollset_set
Definition: pollset_set.h:31
RefCountedPtr< SubchannelInterface > subchannel
Definition: ring_hash.cc:213
An array of arguments that can be passed around.
Definition: grpc_types.h:132