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)); |
| 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 | |
| 59 | typedef enum { |
| 60 | WAITING, |
| 61 | CALLING_BACK, |
| 62 | CALLING_BACK_AND_FINISHED, |
| 63 | CALLED_BACK |
| 64 | } callback_phase; |
| 65 | |
| 66 | typedef 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; |
| 76 | void *tag; |
| 77 | } state_watcher; |
| 78 | |
| 79 | static void delete_state_watcher(state_watcher *w) { |
| 80 | gpr_mu_destroy(&w->mu); |
| 81 | gpr_free(w); |
| 82 | } |
| 83 | |
| 84 | static void finished_completion(void *pw, grpc_cq_completion *ignored) { |
| 85 | int delete = 0; |
| 86 | state_watcher *w = pw; |
| 87 | gpr_mu_lock(&w->mu); |
| 88 | switch (w->phase) { |
| 89 | case WAITING: |
| 90 | case CALLED_BACK: |
| 91 | gpr_log(GPR_ERROR, "should never reach here"); |
| 92 | abort(); |
| 93 | break; |
| 94 | case CALLING_BACK: |
| 95 | w->phase = CALLED_BACK; |
| 96 | break; |
| 97 | case CALLING_BACK_AND_FINISHED: |
| 98 | delete = 1; |
| 99 | break; |
| 100 | } |
| 101 | gpr_mu_unlock(&w->mu); |
| 102 | |
| 103 | if (delete) { |
| 104 | delete_state_watcher(w); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void partly_done(state_watcher *w, int due_to_completion) { |
| 109 | int delete = 0; |
| 110 | |
| 111 | if (due_to_completion) { |
| 112 | gpr_mu_lock(&w->mu); |
| 113 | w->success = 1; |
| 114 | gpr_mu_unlock(&w->mu); |
| 115 | grpc_alarm_cancel(&w->alarm); |
| 116 | } |
| 117 | |
| 118 | gpr_mu_lock(&w->mu); |
| 119 | switch (w->phase) { |
| 120 | case WAITING: |
| 121 | w->phase = CALLING_BACK; |
| 122 | if (w->optional_new_state) { |
| 123 | *w->optional_new_state = w->state; |
| 124 | } |
| 125 | grpc_cq_end_op(w->cq, w->tag, w->success, finished_completion, w, |
| 126 | &w->completion_storage); |
| 127 | break; |
| 128 | case CALLING_BACK: |
| 129 | w->phase = CALLING_BACK_AND_FINISHED; |
| 130 | break; |
| 131 | case CALLING_BACK_AND_FINISHED: |
| 132 | gpr_log(GPR_ERROR, "should never reach here"); |
| 133 | abort(); |
| 134 | break; |
| 135 | case CALLED_BACK: |
| 136 | delete = 1; |
| 137 | break; |
| 138 | } |
| 139 | gpr_mu_unlock(&w->mu); |
| 140 | |
| 141 | if (delete) { |
| 142 | delete_state_watcher(w); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void watch_complete(void *pw, int success) { partly_done(pw, 0); } |
| 147 | |
| 148 | static void timeout_complete(void *pw, int success) { partly_done(pw, 1); } |
| 149 | |
| 150 | void grpc_channel_watch_connectivity_state( |
| 151 | grpc_channel *channel, grpc_connectivity_state last_observed_state, |
| 152 | grpc_connectivity_state *optional_new_state, gpr_timespec deadline, |
| 153 | grpc_completion_queue *cq, void *tag) { |
| 154 | grpc_channel_element *client_channel_elem = |
| 155 | grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel)); |
| 156 | state_watcher *w = gpr_malloc(sizeof(*w)); |
| 157 | |
| 158 | grpc_cq_begin_op(cq); |
| 159 | |
| 160 | gpr_mu_init(&w->mu); |
| 161 | grpc_iomgr_closure_init(&w->on_complete, watch_complete, w); |
| 162 | w->phase = WAITING; |
| 163 | w->state = last_observed_state; |
| 164 | w->success = 0; |
| 165 | w->optional_new_state = optional_new_state; |
| 166 | w->cq = cq; |
| 167 | w->tag = tag; |
| 168 | |
| 169 | grpc_alarm_init(&w->alarm, deadline, timeout_complete, w, |
| 170 | gpr_now(GPR_CLOCK_REALTIME)); |
| 171 | |
| 172 | if (client_channel_elem->filter != &grpc_client_channel_filter) { |
| 173 | gpr_log(GPR_ERROR, |
| 174 | "grpc_channel_watch_connectivity_state called on something that is " |
| 175 | "not a client channel, but '%s'", |
| 176 | client_channel_elem->filter->name); |
| 177 | grpc_iomgr_add_delayed_callback(&w->on_complete, 1); |
| 178 | } else { |
| 179 | grpc_client_channel_watch_connectivity_state(client_channel_elem, &w->state, |
| 180 | &w->on_complete); |
| 181 | } |
| 182 | } |