GRPC C++  1.39.1
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 <map>
25 #include <memory>
26 
27 #include "absl/status/status.h"
28 
29 #include <grpc/grpc.h>
30 
37 
38 namespace grpc_core {
39 
41 
42 // Enum to string conversion.
44 
45 // Interface for watching connectivity state.
46 // Subclasses must implement the Notify() method.
47 //
48 // Note: Most callers will want to use
49 // AsyncConnectivityStateWatcherInterface instead.
51  : public InternallyRefCounted<ConnectivityStateWatcherInterface> {
52  public:
53  ~ConnectivityStateWatcherInterface() override = default;
54 
55  // Notifies the watcher that the state has changed to new_state.
56  virtual void Notify(grpc_connectivity_state new_state,
57  const absl::Status& status) = 0;
58 
59  void Orphan() override { Unref(); }
60 };
61 
62 // An alternative watcher interface that performs notifications via an
63 // asynchronous callback scheduled on the ExecCtx.
64 // Subclasses must implement the OnConnectivityStateChange() method.
67  public:
69 
70  // Schedules a closure on the ExecCtx to invoke
71  // OnConnectivityStateChange() asynchronously.
72  void Notify(grpc_connectivity_state new_state,
73  const absl::Status& status) final;
74 
75  protected:
76  class Notifier;
77 
78  // If \a work_serializer is nullptr, then the notification will be scheduled
79  // on the ExecCtx.
81  std::shared_ptr<WorkSerializer> work_serializer = nullptr)
82  : work_serializer_(std::move(work_serializer)) {}
83 
84  // Invoked asynchronously when Notify() is called.
86  const absl::Status& status) = 0;
87 
88  private:
89  std::shared_ptr<WorkSerializer> work_serializer_;
90 };
91 
92 // Tracks connectivity state. Maintains a list of watchers that are
93 // notified whenever the state changes.
94 //
95 // Note that once the state becomes SHUTDOWN, watchers will be notified
96 // and then automatically orphaned (i.e., RemoveWatcher() does not need
97 // to be called).
99  public:
101  const char* name, grpc_connectivity_state state = GRPC_CHANNEL_IDLE,
102  const absl::Status& status = absl::Status())
103  : name_(name), state_(state), status_(status) {}
104 
106 
107  // Adds a watcher.
108  // If the current state is different than initial_state, the watcher
109  // will be notified immediately. Otherwise, it will be notified
110  // whenever the state changes.
111  // Not thread safe; access must be serialized with an external lock.
112  void AddWatcher(grpc_connectivity_state initial_state,
114 
115  // Removes a watcher. The watcher will be orphaned.
116  // Not thread safe; access must be serialized with an external lock.
118 
119  // Sets connectivity state.
120  // Not thread safe; access must be serialized with an external lock.
122  const char* reason);
123 
124  // Gets the current state.
125  // Thread safe; no need to use an external lock.
127 
128  // Get the current status.
129  // Not thread safe; access must be serialized with an external lock.
130  absl::Status status() const { return status_; }
131 
132  private:
133  const char* name_;
135  absl::Status status_;
136  // TODO(roth): Once we can use C++-14 heterogeneous lookups, this can
137  // be a set instead of a map.
140  watchers_;
141 };
142 
143 } // namespace grpc_core
144 
145 #endif /* GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H */
ClusterWatcher * watcher
Definition: cds.cc:112
Definition: connectivity_state.h:66
virtual void OnConnectivityStateChange(grpc_connectivity_state new_state, const absl::Status &status)=0
AsyncConnectivityStateWatcherInterface(std::shared_ptr< WorkSerializer > work_serializer=nullptr)
Definition: connectivity_state.h:80
void Notify(grpc_connectivity_state new_state, const absl::Status &status) final
Definition: connectivity_state.cc:93
Definition: connectivity_state.h:98
void RemoveWatcher(ConnectivityStateWatcherInterface *watcher)
Definition: connectivity_state.cc:141
ConnectivityStateTracker(const char *name, grpc_connectivity_state state=GRPC_CHANNEL_IDLE, const absl::Status &status=absl::Status())
Definition: connectivity_state.h:100
void SetState(grpc_connectivity_state state, const absl::Status &status, const char *reason)
Definition: connectivity_state.cc:150
grpc_connectivity_state state() const
Definition: connectivity_state.cc:176
~ConnectivityStateTracker()
Definition: connectivity_state.cc:103
void AddWatcher(grpc_connectivity_state initial_state, OrphanablePtr< ConnectivityStateWatcherInterface > watcher)
Definition: connectivity_state.cc:117
absl::Status status() const
Definition: connectivity_state.h:130
Definition: connectivity_state.h:51
virtual void Notify(grpc_connectivity_state new_state, const absl::Status &status)=0
void Orphan() override
Definition: connectivity_state.h:59
Definition: orphanable.h:76
Definition: trace.h:61
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:27
@ GRPC_CHANNEL_IDLE
channel is idle
Definition: connectivity_state.h:29
::google::protobuf::util::Status Status
Definition: config_protobuf.h:91
Round Robin Policy.
Definition: backend_metric.cc:26
TraceFlag grpc_connectivity_state_trace(false, "connectivity_state")
Definition: connectivity_state.h:40
const char * ConnectivityStateName(grpc_connectivity_state state)
Definition: connectivity_state.cc:36
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:67
Definition: async_unary_call.h:398