blob: dd12ff561114211c57b1afebc24f18764ae4a808 [file] [log] [blame]
Craig Tiller48cb07c2015-07-15 16:16:15 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/surface/channel.h"
35
36#include <grpc/support/alloc.h>
37#include <grpc/support/log.h>
38
39#include "src/core/channel/client_channel.h"
40#include "src/core/iomgr/alarm.h"
41#include "src/core/surface/completion_queue.h"
42
43grpc_connectivity_state grpc_channel_check_connectivity_state(
44 grpc_channel *channel, int try_to_connect) {
45 /* forward through to the underlying client channel */
46 grpc_channel_element *client_channel_elem =
47 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
48 if (client_channel_elem->filter != &grpc_client_channel_filter) {
49 gpr_log(GPR_ERROR,
50 "grpc_channel_check_connectivity_state called on something that is "
51 "not a client channel, but '%s'",
52 client_channel_elem->filter->name);
53 return GRPC_CHANNEL_FATAL_FAILURE;
54 }
55 return grpc_client_channel_check_connectivity_state(client_channel_elem,
56 try_to_connect);
57}
58
59typedef enum {
60 WAITING,
61 CALLING_BACK,
62 CALLING_BACK_AND_FINISHED,
63 CALLED_BACK
64} callback_phase;
65
66typedef struct {
67 gpr_mu mu;
68 callback_phase phase;
69 int success;
70 grpc_iomgr_closure on_complete;
71 grpc_alarm alarm;
72 grpc_connectivity_state state;
73 grpc_connectivity_state *optional_new_state;
74 grpc_completion_queue *cq;
75 grpc_cq_completion completion_storage;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070076 grpc_channel *channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -070077 void *tag;
78} state_watcher;
79
80static void delete_state_watcher(state_watcher *w) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -070081 grpc_channel_element *client_channel_elem =
82 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(w->channel));
83 grpc_client_channel_del_interested_party(client_channel_elem, grpc_cq_pollset(w->cq));
84 GRPC_CHANNEL_INTERNAL_UNREF(w->channel, "watch_connectivity");
Craig Tiller48cb07c2015-07-15 16:16:15 -070085 gpr_mu_destroy(&w->mu);
86 gpr_free(w);
87}
88
89static void finished_completion(void *pw, grpc_cq_completion *ignored) {
90 int delete = 0;
91 state_watcher *w = pw;
92 gpr_mu_lock(&w->mu);
93 switch (w->phase) {
94 case WAITING:
95 case CALLED_BACK:
96 gpr_log(GPR_ERROR, "should never reach here");
97 abort();
98 break;
99 case CALLING_BACK:
100 w->phase = CALLED_BACK;
101 break;
102 case CALLING_BACK_AND_FINISHED:
103 delete = 1;
104 break;
105 }
106 gpr_mu_unlock(&w->mu);
107
108 if (delete) {
109 delete_state_watcher(w);
110 }
111}
112
113static void partly_done(state_watcher *w, int due_to_completion) {
114 int delete = 0;
115
116 if (due_to_completion) {
117 gpr_mu_lock(&w->mu);
118 w->success = 1;
119 gpr_mu_unlock(&w->mu);
120 grpc_alarm_cancel(&w->alarm);
121 }
122
123 gpr_mu_lock(&w->mu);
124 switch (w->phase) {
125 case WAITING:
126 w->phase = CALLING_BACK;
127 if (w->optional_new_state) {
128 *w->optional_new_state = w->state;
129 }
130 grpc_cq_end_op(w->cq, w->tag, w->success, finished_completion, w,
131 &w->completion_storage);
132 break;
133 case CALLING_BACK:
134 w->phase = CALLING_BACK_AND_FINISHED;
135 break;
136 case CALLING_BACK_AND_FINISHED:
137 gpr_log(GPR_ERROR, "should never reach here");
138 abort();
139 break;
140 case CALLED_BACK:
141 delete = 1;
142 break;
143 }
144 gpr_mu_unlock(&w->mu);
145
146 if (delete) {
147 delete_state_watcher(w);
148 }
149}
150
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700151static void watch_complete(void *pw, int success) { partly_done(pw, 1); }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700152
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700153static void timeout_complete(void *pw, int success) { partly_done(pw, 0); }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700154
155void grpc_channel_watch_connectivity_state(
156 grpc_channel *channel, grpc_connectivity_state last_observed_state,
157 grpc_connectivity_state *optional_new_state, gpr_timespec deadline,
158 grpc_completion_queue *cq, void *tag) {
159 grpc_channel_element *client_channel_elem =
160 grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
161 state_watcher *w = gpr_malloc(sizeof(*w));
162
163 grpc_cq_begin_op(cq);
164
165 gpr_mu_init(&w->mu);
166 grpc_iomgr_closure_init(&w->on_complete, watch_complete, w);
167 w->phase = WAITING;
168 w->state = last_observed_state;
169 w->success = 0;
170 w->optional_new_state = optional_new_state;
171 w->cq = cq;
172 w->tag = tag;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700173 w->channel = channel;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700174
175 grpc_alarm_init(&w->alarm, deadline, timeout_complete, w,
176 gpr_now(GPR_CLOCK_REALTIME));
177
178 if (client_channel_elem->filter != &grpc_client_channel_filter) {
179 gpr_log(GPR_ERROR,
180 "grpc_channel_watch_connectivity_state called on something that is "
181 "not a client channel, but '%s'",
182 client_channel_elem->filter->name);
183 grpc_iomgr_add_delayed_callback(&w->on_complete, 1);
184 } else {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700185 GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
186 grpc_client_channel_add_interested_party(client_channel_elem, grpc_cq_pollset(cq));
Craig Tiller48cb07c2015-07-15 16:16:15 -0700187 grpc_client_channel_watch_connectivity_state(client_channel_elem, &w->state,
188 &w->on_complete);
189 }
190}