GRPC Core  9.0.0
connectivity_state.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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_LIB_TRANSPORT_CONNECTIVITY_STATE_H
20 #define GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H
21 
23 
24 #include <grpc/grpc.h>
25 
28 #include "src/core/lib/gprpp/map.h"
32 
33 namespace grpc_core {
34 
36 
37 // Enum to string conversion.
39 
40 // Interface for watching connectivity state.
41 // Subclasses must implement the Notify() method.
42 //
43 // Note: Most callers will want to use
44 // AsyncConnectivityStateWatcherInterface instead.
46  : public InternallyRefCounted<ConnectivityStateWatcherInterface> {
47  public:
48  virtual ~ConnectivityStateWatcherInterface() = default;
49 
50  // Notifies the watcher that the state has changed to new_state.
51  virtual void Notify(grpc_connectivity_state new_state) = 0;
52 
53  void Orphan() override { Unref(); }
54 };
55 
56 // An alternative watcher interface that performs notifications via an
57 // asynchronous callback scheduled on the ExecCtx.
58 // Subclasses must implement the OnConnectivityStateChange() method.
61  public:
62  virtual ~AsyncConnectivityStateWatcherInterface() = default;
63 
64  // Schedules a closure on the ExecCtx to invoke
65  // OnConnectivityStateChange() asynchronously.
66  void Notify(grpc_connectivity_state new_state) override final;
67 
68  protected:
69  class Notifier;
70 
71  // If \a combiner is nullptr, then the notification will be scheduled on the
72  // ExecCtx.
73  explicit AsyncConnectivityStateWatcherInterface(Combiner* combiner = nullptr)
74  : combiner_(combiner) {}
75 
76  // Invoked asynchronously when Notify() is called.
77  virtual void OnConnectivityStateChange(grpc_connectivity_state new_state) = 0;
78 
79  private:
80  Combiner* combiner_;
81 };
82 
83 // Tracks connectivity state. Maintains a list of watchers that are
84 // notified whenever the state changes.
85 //
86 // Note that once the state becomes SHUTDOWN, watchers will be notified
87 // and then automatically orphaned (i.e., RemoveWatcher() does not need
88 // to be called).
90  public:
91  ConnectivityStateTracker(const char* name,
93  : name_(name), state_(state) {}
94 
96 
97  // Adds a watcher.
98  // If the current state is different than initial_state, the watcher
99  // will be notified immediately. Otherwise, it will be notified
100  // whenever the state changes.
101  // Not thread safe; access must be serialized with an external lock.
102  void AddWatcher(grpc_connectivity_state initial_state,
104 
105  // Removes a watcher. The watcher will be orphaned.
106  // Not thread safe; access must be serialized with an external lock.
108 
109  // Sets connectivity state.
110  // Not thread safe; access must be serialized with an external lock.
111  void SetState(grpc_connectivity_state state, const char* reason);
112 
113  // Gets the current state.
114  // Thread safe; no need to use an external lock.
116 
117  private:
118  const char* name_;
120  // TODO(roth): Once we can use C++-14 heterogeneous lookups, this can
121  // be a set instead of a map.
124  watchers_;
125 };
126 
127 } // namespace grpc_core
128 
129 #endif /* GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H */
const char * ConnectivityStateName(grpc_connectivity_state state)
Definition: connectivity_state.cc:36
virtual void OnConnectivityStateChange(grpc_connectivity_state new_state)=0
void SetState(grpc_connectivity_state state, const char *reason)
Definition: connectivity_state.cc:146
void RemoveWatcher(ConnectivityStateWatcherInterface *watcher)
Definition: connectivity_state.cc:137
Definition: connectivity_state.h:45
void Orphan() override
Definition: connectivity_state.h:53
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:27
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:68
Definition: connectivity_state.h:59
Definition: orphanable.h:77
Round Robin Policy.
Definition: backend_metric.cc:24
Definition: trace.h:61
Definition: combiner.h:33
AsyncConnectivityStateWatcherInterface(Combiner *combiner=nullptr)
Definition: connectivity_state.h:73
void Notify(grpc_connectivity_state new_state) override final
Definition: connectivity_state.cc:90
ConnectivityStateTracker(const char *name, grpc_connectivity_state state=GRPC_CHANNEL_IDLE)
Definition: connectivity_state.h:91
~ConnectivityStateTracker()
Definition: connectivity_state.cc:99
virtual void Notify(grpc_connectivity_state new_state)=0
void AddWatcher(grpc_connectivity_state initial_state, OrphanablePtr< ConnectivityStateWatcherInterface > watcher)
Definition: connectivity_state.cc:113
grpc_connectivity_state state() const
Definition: connectivity_state.cc:170
channel is idle
Definition: connectivity_state.h:29
Definition: connectivity_state.h:89
TraceFlag grpc_connectivity_state_trace(false, "connectivity_state")
Definition: connectivity_state.h:35