Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 43 | grpc_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)); |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 48 | grpc_call_list call_list = GRPC_CALL_LIST_INIT; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 49 | grpc_connectivity_state state; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 50 | if (client_channel_elem->filter != &grpc_client_channel_filter) { |
| 51 | gpr_log(GPR_ERROR, |
| 52 | "grpc_channel_check_connectivity_state called on something that is " |
| 53 | "not a client channel, but '%s'", |
| 54 | client_channel_elem->filter->name); |
| 55 | return GRPC_CHANNEL_FATAL_FAILURE; |
| 56 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 57 | state = grpc_client_channel_check_connectivity_state( |
| 58 | client_channel_elem, try_to_connect, &call_list); |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 59 | grpc_call_list_run(call_list); |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 60 | return state; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | typedef enum { |
| 64 | WAITING, |
| 65 | CALLING_BACK, |
| 66 | CALLING_BACK_AND_FINISHED, |
| 67 | CALLED_BACK |
| 68 | } callback_phase; |
| 69 | |
| 70 | typedef struct { |
| 71 | gpr_mu mu; |
| 72 | callback_phase phase; |
| 73 | int success; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 74 | grpc_closure on_complete; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 75 | grpc_alarm alarm; |
| 76 | grpc_connectivity_state state; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 77 | grpc_completion_queue *cq; |
| 78 | grpc_cq_completion completion_storage; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 79 | grpc_channel *channel; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 80 | void *tag; |
| 81 | } state_watcher; |
| 82 | |
| 83 | static void delete_state_watcher(state_watcher *w) { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 84 | grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element( |
| 85 | grpc_channel_get_channel_stack(w->channel)); |
| 86 | grpc_client_channel_del_interested_party(client_channel_elem, |
| 87 | grpc_cq_pollset(w->cq)); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 88 | GRPC_CHANNEL_INTERNAL_UNREF(w->channel, "watch_connectivity"); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 89 | gpr_mu_destroy(&w->mu); |
| 90 | gpr_free(w); |
| 91 | } |
| 92 | |
| 93 | static void finished_completion(void *pw, grpc_cq_completion *ignored) { |
| 94 | int delete = 0; |
| 95 | state_watcher *w = pw; |
| 96 | gpr_mu_lock(&w->mu); |
| 97 | switch (w->phase) { |
| 98 | case WAITING: |
| 99 | case CALLED_BACK: |
| 100 | gpr_log(GPR_ERROR, "should never reach here"); |
| 101 | abort(); |
| 102 | break; |
| 103 | case CALLING_BACK: |
| 104 | w->phase = CALLED_BACK; |
| 105 | break; |
| 106 | case CALLING_BACK_AND_FINISHED: |
| 107 | delete = 1; |
| 108 | break; |
| 109 | } |
| 110 | gpr_mu_unlock(&w->mu); |
| 111 | |
| 112 | if (delete) { |
| 113 | delete_state_watcher(w); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void partly_done(state_watcher *w, int due_to_completion) { |
| 118 | int delete = 0; |
| 119 | |
| 120 | if (due_to_completion) { |
| 121 | gpr_mu_lock(&w->mu); |
| 122 | w->success = 1; |
| 123 | gpr_mu_unlock(&w->mu); |
| 124 | grpc_alarm_cancel(&w->alarm); |
| 125 | } |
| 126 | |
| 127 | gpr_mu_lock(&w->mu); |
| 128 | switch (w->phase) { |
| 129 | case WAITING: |
| 130 | w->phase = CALLING_BACK; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 131 | grpc_cq_end_op(w->cq, w->tag, w->success, finished_completion, w, |
| 132 | &w->completion_storage); |
| 133 | break; |
| 134 | case CALLING_BACK: |
| 135 | w->phase = CALLING_BACK_AND_FINISHED; |
| 136 | break; |
| 137 | case CALLING_BACK_AND_FINISHED: |
| 138 | gpr_log(GPR_ERROR, "should never reach here"); |
| 139 | abort(); |
| 140 | break; |
| 141 | case CALLED_BACK: |
| 142 | delete = 1; |
| 143 | break; |
| 144 | } |
| 145 | gpr_mu_unlock(&w->mu); |
| 146 | |
| 147 | if (delete) { |
| 148 | delete_state_watcher(w); |
| 149 | } |
| 150 | } |
| 151 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 152 | static void watch_complete(void *pw, int success) { partly_done(pw, 1); } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 153 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 154 | static void timeout_complete(void *pw, int success) { partly_done(pw, 0); } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 155 | |
| 156 | void grpc_channel_watch_connectivity_state( |
| 157 | grpc_channel *channel, grpc_connectivity_state last_observed_state, |
Craig Tiller | 2cd9dd9 | 2015-07-31 16:34:37 -0700 | [diff] [blame] | 158 | gpr_timespec deadline, grpc_completion_queue *cq, void *tag) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 159 | grpc_channel_element *client_channel_elem = |
| 160 | grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel)); |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 161 | grpc_call_list call_list = GRPC_CALL_LIST_INIT; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 162 | state_watcher *w = gpr_malloc(sizeof(*w)); |
| 163 | |
| 164 | grpc_cq_begin_op(cq); |
| 165 | |
| 166 | gpr_mu_init(&w->mu); |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 167 | grpc_closure_init(&w->on_complete, watch_complete, w); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 168 | w->phase = WAITING; |
| 169 | w->state = last_observed_state; |
| 170 | w->success = 0; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 171 | w->cq = cq; |
| 172 | w->tag = tag; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 173 | w->channel = channel; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 174 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 175 | grpc_alarm_init(&w->alarm, |
| 176 | gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), |
| 177 | timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 178 | |
| 179 | if (client_channel_elem->filter != &grpc_client_channel_filter) { |
| 180 | gpr_log(GPR_ERROR, |
| 181 | "grpc_channel_watch_connectivity_state called on something that is " |
| 182 | "not a client channel, but '%s'", |
| 183 | client_channel_elem->filter->name); |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 184 | grpc_call_list_add(&call_list, &w->on_complete, 1); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 185 | } else { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 186 | GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity"); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 187 | grpc_client_channel_add_interested_party(client_channel_elem, |
| 188 | grpc_cq_pollset(cq)); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 189 | grpc_client_channel_watch_connectivity_state(client_channel_elem, &w->state, |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 190 | &w->on_complete, &call_list); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 191 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 192 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame^] | 193 | grpc_call_list_run(call_list); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 194 | } |