Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | // |
| 31 | |
| 32 | #include "src/core/lib/channel/deadline_filter.h" |
| 33 | |
| 34 | #include <stdbool.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include <grpc/support/log.h> |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 38 | #include <grpc/support/sync.h> |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 39 | #include <grpc/support/time.h> |
| 40 | |
| 41 | #include "src/core/lib/iomgr/timer.h" |
| 42 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 43 | // |
| 44 | // grpc_deadline_state |
| 45 | // |
| 46 | |
| 47 | // Timer callback. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 48 | static void timer_callback(grpc_exec_ctx* exec_ctx, void* arg, |
| 49 | grpc_error* error) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 50 | grpc_call_element* elem = arg; |
| 51 | grpc_deadline_state* deadline_state = elem->call_data; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 52 | gpr_mu_lock(&deadline_state->timer_mu); |
| 53 | deadline_state->timer_pending = false; |
| 54 | gpr_mu_unlock(&deadline_state->timer_mu); |
| 55 | if (error != GRPC_ERROR_CANCELLED) { |
Mark D. Roth | 75d7478 | 2016-09-09 07:46:01 -0700 | [diff] [blame] | 56 | gpr_slice msg = gpr_slice_from_static_string("Deadline Exceeded"); |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 57 | grpc_call_element_send_cancel_with_message( |
| 58 | exec_ctx, elem, GRPC_STATUS_DEADLINE_EXCEEDED, &msg); |
Mark D. Roth | 75d7478 | 2016-09-09 07:46:01 -0700 | [diff] [blame] | 59 | gpr_slice_unref(msg); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 60 | } |
| 61 | GRPC_CALL_STACK_UNREF(exec_ctx, deadline_state->call_stack, "deadline_timer"); |
| 62 | } |
| 63 | |
| 64 | // Starts the deadline timer. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 65 | static void start_timer_if_needed(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 66 | grpc_call_element* elem, |
| 67 | gpr_timespec deadline) { |
| 68 | grpc_deadline_state* deadline_state = elem->call_data; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 69 | deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC); |
| 70 | if (gpr_time_cmp(deadline, gpr_inf_future(GPR_CLOCK_MONOTONIC)) != 0) { |
| 71 | // Take a reference to the call stack, to be owned by the timer. |
| 72 | GRPC_CALL_STACK_REF(deadline_state->call_stack, "deadline_timer"); |
| 73 | gpr_mu_lock(&deadline_state->timer_mu); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 74 | deadline_state->timer_pending = true; |
| 75 | grpc_timer_init(exec_ctx, &deadline_state->timer, deadline, timer_callback, |
| 76 | elem, gpr_now(GPR_CLOCK_MONOTONIC)); |
| 77 | gpr_mu_unlock(&deadline_state->timer_mu); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Cancels the deadline timer. |
| 82 | static void cancel_timer_if_needed(grpc_exec_ctx* exec_ctx, |
| 83 | grpc_deadline_state* deadline_state) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 84 | gpr_mu_lock(&deadline_state->timer_mu); |
| 85 | if (deadline_state->timer_pending) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 86 | grpc_timer_cancel(exec_ctx, &deadline_state->timer); |
| 87 | deadline_state->timer_pending = false; |
| 88 | } |
| 89 | gpr_mu_unlock(&deadline_state->timer_mu); |
| 90 | } |
| 91 | |
| 92 | // Callback run when the call is complete. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 93 | static void on_complete(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 94 | grpc_deadline_state* deadline_state = arg; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 95 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 96 | // Invoke the next callback. |
| 97 | deadline_state->next_on_complete->cb( |
| 98 | exec_ctx, deadline_state->next_on_complete->cb_arg, error); |
| 99 | } |
| 100 | |
| 101 | // Inject our own on_complete callback into op. |
| 102 | static void inject_on_complete_cb(grpc_deadline_state* deadline_state, |
| 103 | grpc_transport_stream_op* op) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 104 | deadline_state->next_on_complete = op->on_complete; |
| 105 | grpc_closure_init(&deadline_state->on_complete, on_complete, deadline_state); |
| 106 | op->on_complete = &deadline_state->on_complete; |
| 107 | } |
| 108 | |
| 109 | void grpc_deadline_state_init(grpc_deadline_state* deadline_state, |
| 110 | grpc_call_stack* call_stack) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 111 | memset(deadline_state, 0, sizeof(*deadline_state)); |
| 112 | deadline_state->call_stack = call_stack; |
| 113 | gpr_mu_init(&deadline_state->timer_mu); |
| 114 | } |
| 115 | |
| 116 | void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx, |
| 117 | grpc_deadline_state* deadline_state) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 118 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 119 | gpr_mu_destroy(&deadline_state->timer_mu); |
| 120 | } |
| 121 | |
| 122 | void grpc_deadline_state_client_start_transport_stream_op( |
| 123 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 124 | grpc_transport_stream_op* op) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 125 | grpc_deadline_state* deadline_state = elem->call_data; |
| 126 | if (op->cancel_error != GRPC_ERROR_NONE || |
| 127 | op->close_error != GRPC_ERROR_NONE) { |
| 128 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 129 | } else { |
| 130 | // If we're sending initial metadata, get the deadline from the metadata |
| 131 | // and start the timer if needed. |
| 132 | if (op->send_initial_metadata != NULL) { |
| 133 | start_timer_if_needed(exec_ctx, elem, |
| 134 | op->send_initial_metadata->deadline); |
| 135 | } |
| 136 | // Make sure we know when the call is complete, so that we can cancel |
| 137 | // the timer. |
| 138 | if (op->recv_trailing_metadata != NULL) { |
| 139 | inject_on_complete_cb(deadline_state, op); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // |
| 145 | // filter code |
| 146 | // |
| 147 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 148 | // Constructor for channel_data. Used for both client and server filters. |
| 149 | static void init_channel_elem(grpc_exec_ctx* exec_ctx, |
| 150 | grpc_channel_element* elem, |
| 151 | grpc_channel_element_args* args) { |
| 152 | GPR_ASSERT(!args->is_last); |
| 153 | } |
| 154 | |
| 155 | // Destructor for channel_data. Used for both client and server filters. |
| 156 | static void destroy_channel_elem(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 157 | grpc_channel_element* elem) {} |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 158 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 159 | // Call data used for both client and server filter. |
| 160 | typedef struct base_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 161 | grpc_deadline_state deadline_state; |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 162 | } base_call_data; |
| 163 | |
| 164 | // Additional call data used only for the server filter. |
| 165 | typedef struct server_call_data { |
| 166 | base_call_data base; // Must be first. |
| 167 | // The closure for receiving initial metadata. |
| 168 | grpc_closure recv_initial_metadata_ready; |
| 169 | // Received initial metadata batch. |
| 170 | grpc_metadata_batch* recv_initial_metadata; |
| 171 | // The original recv_initial_metadata_ready closure, which we chain to |
| 172 | // after our own closure is invoked. |
| 173 | grpc_closure* next_recv_initial_metadata_ready; |
| 174 | } server_call_data; |
| 175 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 176 | // Constructor for call_data. Used for both client and server filters. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 177 | static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 178 | grpc_call_element* elem, |
| 179 | grpc_call_element_args* args) { |
| 180 | base_call_data* calld = elem->call_data; |
| 181 | // Note: size of call data is different between client and server. |
| 182 | memset(calld, 0, elem->filter->sizeof_call_data); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 183 | grpc_deadline_state_init(&calld->deadline_state, args->call_stack); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 184 | return GRPC_ERROR_NONE; |
| 185 | } |
| 186 | |
| 187 | // Destructor for call_data. Used for both client and server filters. |
| 188 | static void destroy_call_elem(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 189 | const grpc_call_final_info* final_info, |
| 190 | void* and_free_memory) { |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 191 | base_call_data* calld = elem->call_data; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 192 | grpc_deadline_state_destroy(exec_ctx, &calld->deadline_state); |
Mark D. Roth | d2b4533 | 2016-08-26 11:18:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 195 | // Method for starting a call op for client filter. |
| 196 | static void client_start_transport_stream_op(grpc_exec_ctx* exec_ctx, |
| 197 | grpc_call_element* elem, |
| 198 | grpc_transport_stream_op* op) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 199 | grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 200 | // Chain to next filter. |
| 201 | grpc_call_next_op(exec_ctx, elem, op); |
| 202 | } |
| 203 | |
| 204 | // Callback for receiving initial metadata on the server. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 205 | static void recv_initial_metadata_ready(grpc_exec_ctx* exec_ctx, void* arg, |
| 206 | grpc_error* error) { |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 207 | grpc_call_element* elem = arg; |
| 208 | server_call_data* calld = elem->call_data; |
| 209 | // Get deadline from metadata and start the timer if needed. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 210 | start_timer_if_needed(exec_ctx, elem, calld->recv_initial_metadata->deadline); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 211 | // Invoke the next callback. |
| 212 | calld->next_recv_initial_metadata_ready->cb( |
| 213 | exec_ctx, calld->next_recv_initial_metadata_ready->cb_arg, error); |
| 214 | } |
| 215 | |
| 216 | // Method for starting a call op for server filter. |
| 217 | static void server_start_transport_stream_op(grpc_exec_ctx* exec_ctx, |
| 218 | grpc_call_element* elem, |
| 219 | grpc_transport_stream_op* op) { |
| 220 | server_call_data* calld = elem->call_data; |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 221 | if (op->cancel_error != GRPC_ERROR_NONE || |
| 222 | op->close_error != GRPC_ERROR_NONE) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 223 | cancel_timer_if_needed(exec_ctx, &calld->base.deadline_state); |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 224 | } else { |
| 225 | // If we're receiving initial metadata, we need to get the deadline |
| 226 | // from the recv_initial_metadata_ready callback. So we inject our |
| 227 | // own callback into that hook. |
| 228 | if (op->recv_initial_metadata_ready != NULL) { |
| 229 | calld->next_recv_initial_metadata_ready = op->recv_initial_metadata_ready; |
| 230 | calld->recv_initial_metadata = op->recv_initial_metadata; |
| 231 | grpc_closure_init(&calld->recv_initial_metadata_ready, |
| 232 | recv_initial_metadata_ready, elem); |
| 233 | op->recv_initial_metadata_ready = &calld->recv_initial_metadata_ready; |
| 234 | } |
| 235 | // Make sure we know when the call is complete, so that we can cancel |
| 236 | // the timer. |
| 237 | // Note that we trigger this on recv_trailing_metadata, even though |
| 238 | // the client never sends trailing metadata, because this is the |
| 239 | // hook that tells us when the call is complete on the server side. |
| 240 | if (op->recv_trailing_metadata != NULL) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 241 | inject_on_complete_cb(&calld->base.deadline_state, op); |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 242 | } |
Mark D. Roth | d2b4533 | 2016-08-26 11:18:00 -0700 | [diff] [blame] | 243 | } |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 244 | // Chain to next filter. |
| 245 | grpc_call_next_op(exec_ctx, elem, op); |
| 246 | } |
| 247 | |
| 248 | const grpc_channel_filter grpc_client_deadline_filter = { |
| 249 | client_start_transport_stream_op, |
| 250 | grpc_channel_next_op, |
| 251 | sizeof(base_call_data), |
| 252 | init_call_elem, |
| 253 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 254 | destroy_call_elem, |
Mark D. Roth | b3405f0a | 2016-09-09 08:46:28 -0700 | [diff] [blame^] | 255 | 0, // sizeof(channel_data) |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 256 | init_channel_elem, |
| 257 | destroy_channel_elem, |
| 258 | grpc_call_next_get_peer, |
| 259 | "deadline", |
| 260 | }; |
| 261 | |
| 262 | const grpc_channel_filter grpc_server_deadline_filter = { |
| 263 | server_start_transport_stream_op, |
| 264 | grpc_channel_next_op, |
| 265 | sizeof(server_call_data), |
| 266 | init_call_elem, |
| 267 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 268 | destroy_call_elem, |
Mark D. Roth | b3405f0a | 2016-09-09 08:46:28 -0700 | [diff] [blame^] | 269 | 0, // sizeof(channel_data) |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 270 | init_channel_elem, |
| 271 | destroy_channel_elem, |
| 272 | grpc_call_next_get_peer, |
| 273 | "deadline", |
| 274 | }; |