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 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 37 | #include <grpc/support/alloc.h> |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 38 | #include <grpc/support/log.h> |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 39 | #include <grpc/support/sync.h> |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 40 | #include <grpc/support/time.h> |
| 41 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 42 | #include "src/core/lib/iomgr/exec_ctx.h" |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 43 | #include "src/core/lib/iomgr/timer.h" |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 44 | #include "src/core/lib/slice/slice_internal.h" |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 45 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 46 | // |
| 47 | // grpc_deadline_state |
| 48 | // |
| 49 | |
| 50 | // Timer callback. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 51 | static void timer_callback(grpc_exec_ctx* exec_ctx, void* arg, |
| 52 | grpc_error* error) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 53 | grpc_call_element* elem = arg; |
| 54 | grpc_deadline_state* deadline_state = elem->call_data; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 55 | if (error != GRPC_ERROR_CANCELLED) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 56 | grpc_call_element_signal_error( |
| 57 | exec_ctx, elem, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 58 | grpc_error_set_int( |
| 59 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Deadline Exceeded"), |
| 60 | GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_DEADLINE_EXCEEDED)); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 61 | } |
| 62 | GRPC_CALL_STACK_UNREF(exec_ctx, deadline_state->call_stack, "deadline_timer"); |
| 63 | } |
| 64 | |
| 65 | // Starts the deadline timer. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 66 | static void start_timer_if_needed(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 67 | grpc_call_element* elem, |
| 68 | gpr_timespec deadline) { |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 69 | deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC); |
Craig Tiller | 0a77de8 | 2017-02-16 12:39:33 -0800 | [diff] [blame] | 70 | if (gpr_time_cmp(deadline, gpr_inf_future(GPR_CLOCK_MONOTONIC)) == 0) { |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 71 | return; |
| 72 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 73 | grpc_deadline_state* deadline_state = elem->call_data; |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 74 | grpc_deadline_timer_state cur_state; |
| 75 | grpc_closure* closure = NULL; |
| 76 | retry: |
| 77 | cur_state = |
| 78 | (grpc_deadline_timer_state)gpr_atm_acq_load(&deadline_state->timer_state); |
| 79 | switch (cur_state) { |
| 80 | case GRPC_DEADLINE_STATE_PENDING: |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 81 | // Note: We do not start the timer if there is already a timer |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 82 | return; |
| 83 | case GRPC_DEADLINE_STATE_FINISHED: |
| 84 | if (gpr_atm_rel_cas(&deadline_state->timer_state, |
| 85 | GRPC_DEADLINE_STATE_FINISHED, |
| 86 | GRPC_DEADLINE_STATE_PENDING)) { |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 87 | // If we've already created and destroyed a timer, we always create a |
| 88 | // new closure: we have no other guarantee that the inlined closure is |
| 89 | // not in use (it may hold a pending call to timer_callback) |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 90 | closure = grpc_closure_create(timer_callback, elem, |
| 91 | grpc_schedule_on_exec_ctx); |
| 92 | } else { |
| 93 | goto retry; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 94 | } |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 95 | break; |
| 96 | case GRPC_DEADLINE_STATE_INITIAL: |
| 97 | if (gpr_atm_rel_cas(&deadline_state->timer_state, |
| 98 | GRPC_DEADLINE_STATE_INITIAL, |
| 99 | GRPC_DEADLINE_STATE_PENDING)) { |
| 100 | closure = |
| 101 | grpc_closure_init(&deadline_state->timer_callback, timer_callback, |
| 102 | elem, grpc_schedule_on_exec_ctx); |
| 103 | } else { |
| 104 | goto retry; |
| 105 | } |
| 106 | break; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 107 | } |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 108 | GPR_ASSERT(closure); |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 109 | GRPC_CALL_STACK_REF(deadline_state->call_stack, "deadline_timer"); |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 110 | grpc_timer_init(exec_ctx, &deadline_state->timer, deadline, closure, |
| 111 | gpr_now(GPR_CLOCK_MONOTONIC)); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Cancels the deadline timer. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 115 | static void cancel_timer_if_needed(grpc_exec_ctx* exec_ctx, |
| 116 | grpc_deadline_state* deadline_state) { |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 117 | if (gpr_atm_rel_cas(&deadline_state->timer_state, GRPC_DEADLINE_STATE_PENDING, |
| 118 | GRPC_DEADLINE_STATE_FINISHED)) { |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 119 | grpc_timer_cancel(exec_ctx, &deadline_state->timer); |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 120 | } else { |
| 121 | // timer was either in STATE_INITAL (nothing to cancel) |
| 122 | // OR in STATE_FINISHED (again nothing to cancel) |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 123 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Callback run when the call is complete. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 127 | 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] | 128 | grpc_deadline_state* deadline_state = arg; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 129 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 130 | // Invoke the next callback. |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 131 | grpc_closure_run(exec_ctx, deadline_state->next_on_complete, |
| 132 | GRPC_ERROR_REF(error)); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Inject our own on_complete callback into op. |
| 136 | static void inject_on_complete_cb(grpc_deadline_state* deadline_state, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 137 | grpc_transport_stream_op_batch* op) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 138 | deadline_state->next_on_complete = op->on_complete; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 139 | grpc_closure_init(&deadline_state->on_complete, on_complete, deadline_state, |
| 140 | grpc_schedule_on_exec_ctx); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 141 | op->on_complete = &deadline_state->on_complete; |
| 142 | } |
| 143 | |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 144 | void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 145 | grpc_call_stack* call_stack) { |
| 146 | grpc_deadline_state* deadline_state = elem->call_data; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 147 | deadline_state->call_stack = call_stack; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx, |
| 151 | grpc_call_element* elem) { |
| 152 | grpc_deadline_state* deadline_state = elem->call_data; |
| 153 | cancel_timer_if_needed(exec_ctx, deadline_state); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 156 | // Callback and associated state for starting the timer after call stack |
| 157 | // initialization has been completed. |
| 158 | struct start_timer_after_init_state { |
| 159 | grpc_call_element* elem; |
| 160 | gpr_timespec deadline; |
| 161 | grpc_closure closure; |
| 162 | }; |
| 163 | static void start_timer_after_init(grpc_exec_ctx* exec_ctx, void* arg, |
| 164 | grpc_error* error) { |
| 165 | struct start_timer_after_init_state* state = arg; |
| 166 | start_timer_if_needed(exec_ctx, state->elem, state->deadline); |
| 167 | gpr_free(state); |
| 168 | } |
| 169 | |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 170 | void grpc_deadline_state_start(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 171 | gpr_timespec deadline) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 172 | // Deadline will always be infinite on servers, so the timer will only be |
| 173 | // set on clients with a finite deadline. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 174 | deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 175 | if (gpr_time_cmp(deadline, gpr_inf_future(GPR_CLOCK_MONOTONIC)) != 0) { |
| 176 | // When the deadline passes, we indicate the failure by sending down |
| 177 | // an op with cancel_error set. However, we can't send down any ops |
| 178 | // until after the call stack is fully initialized. If we start the |
| 179 | // timer here, we have no guarantee that the timer won't pop before |
| 180 | // call stack initialization is finished. To avoid that problem, we |
| 181 | // create a closure to start the timer, and we schedule that closure |
| 182 | // to be run after call stack initialization is done. |
| 183 | struct start_timer_after_init_state* state = gpr_malloc(sizeof(*state)); |
| 184 | state->elem = elem; |
| 185 | state->deadline = deadline; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 186 | grpc_closure_init(&state->closure, start_timer_after_init, state, |
| 187 | grpc_schedule_on_exec_ctx); |
| 188 | grpc_closure_sched(exec_ctx, &state->closure, GRPC_ERROR_NONE); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 189 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 192 | void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 193 | gpr_timespec new_deadline) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 194 | grpc_deadline_state* deadline_state = elem->call_data; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 195 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 196 | start_timer_if_needed(exec_ctx, elem, new_deadline); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 199 | void grpc_deadline_state_client_start_transport_stream_op_batch( |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 200 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 201 | grpc_transport_stream_op_batch* op) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 202 | grpc_deadline_state* deadline_state = elem->call_data; |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 203 | if (op->cancel_stream) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 204 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 205 | } else { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 206 | // Make sure we know when the call is complete, so that we can cancel |
| 207 | // the timer. |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 208 | if (op->recv_trailing_metadata) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 209 | inject_on_complete_cb(deadline_state, op); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // |
| 215 | // filter code |
| 216 | // |
| 217 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 218 | // Constructor for channel_data. Used for both client and server filters. |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 219 | static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx, |
| 220 | grpc_channel_element* elem, |
| 221 | grpc_channel_element_args* args) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 222 | GPR_ASSERT(!args->is_last); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 223 | return GRPC_ERROR_NONE; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Destructor for channel_data. Used for both client and server filters. |
| 227 | static void destroy_channel_elem(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 228 | grpc_channel_element* elem) {} |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 229 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 230 | // Call data used for both client and server filter. |
| 231 | typedef struct base_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 232 | grpc_deadline_state deadline_state; |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 233 | } base_call_data; |
| 234 | |
| 235 | // Additional call data used only for the server filter. |
| 236 | typedef struct server_call_data { |
| 237 | base_call_data base; // Must be first. |
| 238 | // The closure for receiving initial metadata. |
| 239 | grpc_closure recv_initial_metadata_ready; |
| 240 | // Received initial metadata batch. |
| 241 | grpc_metadata_batch* recv_initial_metadata; |
| 242 | // The original recv_initial_metadata_ready closure, which we chain to |
| 243 | // after our own closure is invoked. |
| 244 | grpc_closure* next_recv_initial_metadata_ready; |
| 245 | } server_call_data; |
| 246 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 247 | // Constructor for call_data. Used for both client and server filters. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 248 | static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 249 | grpc_call_element* elem, |
Craig Tiller | c52ba3a | 2017-02-15 22:57:43 -0800 | [diff] [blame] | 250 | const grpc_call_element_args* args) { |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 251 | grpc_deadline_state_init(exec_ctx, elem, args->call_stack); |
| 252 | grpc_deadline_state_start(exec_ctx, elem, args->deadline); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 253 | return GRPC_ERROR_NONE; |
| 254 | } |
| 255 | |
| 256 | // Destructor for call_data. Used for both client and server filters. |
| 257 | static void destroy_call_elem(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 258 | const grpc_call_final_info* final_info, |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 259 | grpc_closure* ignored) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 260 | grpc_deadline_state_destroy(exec_ctx, elem); |
Mark D. Roth | d2b4533 | 2016-08-26 11:18:00 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 263 | // Method for starting a call op for client filter. |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 264 | static void client_start_transport_stream_op_batch(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 265 | grpc_call_element* elem, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 266 | grpc_transport_stream_op_batch* op) { |
| 267 | grpc_deadline_state_client_start_transport_stream_op_batch(exec_ctx, elem, op); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 268 | // Chain to next filter. |
| 269 | grpc_call_next_op(exec_ctx, elem, op); |
| 270 | } |
| 271 | |
| 272 | // Callback for receiving initial metadata on the server. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 273 | static void recv_initial_metadata_ready(grpc_exec_ctx* exec_ctx, void* arg, |
| 274 | grpc_error* error) { |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 275 | grpc_call_element* elem = arg; |
| 276 | server_call_data* calld = elem->call_data; |
| 277 | // Get deadline from metadata and start the timer if needed. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 278 | 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] | 279 | // Invoke the next callback. |
| 280 | calld->next_recv_initial_metadata_ready->cb( |
| 281 | exec_ctx, calld->next_recv_initial_metadata_ready->cb_arg, error); |
| 282 | } |
| 283 | |
| 284 | // Method for starting a call op for server filter. |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 285 | static void server_start_transport_stream_op_batch(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 286 | grpc_call_element* elem, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 287 | grpc_transport_stream_op_batch* op) { |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 288 | server_call_data* calld = elem->call_data; |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 289 | if (op->cancel_stream) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 290 | cancel_timer_if_needed(exec_ctx, &calld->base.deadline_state); |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 291 | } else { |
| 292 | // If we're receiving initial metadata, we need to get the deadline |
| 293 | // from the recv_initial_metadata_ready callback. So we inject our |
| 294 | // own callback into that hook. |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 295 | if (op->recv_initial_metadata) { |
| 296 | calld->next_recv_initial_metadata_ready = |
| 297 | op->payload->recv_initial_metadata.recv_initial_metadata_ready; |
| 298 | calld->recv_initial_metadata = |
| 299 | op->payload->recv_initial_metadata.recv_initial_metadata; |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 300 | grpc_closure_init(&calld->recv_initial_metadata_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 301 | recv_initial_metadata_ready, elem, |
| 302 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 303 | op->payload->recv_initial_metadata.recv_initial_metadata_ready = |
| 304 | &calld->recv_initial_metadata_ready; |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 305 | } |
| 306 | // Make sure we know when the call is complete, so that we can cancel |
| 307 | // the timer. |
| 308 | // Note that we trigger this on recv_trailing_metadata, even though |
| 309 | // the client never sends trailing metadata, because this is the |
| 310 | // hook that tells us when the call is complete on the server side. |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 311 | if (op->recv_trailing_metadata) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 312 | inject_on_complete_cb(&calld->base.deadline_state, op); |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 313 | } |
Mark D. Roth | d2b4533 | 2016-08-26 11:18:00 -0700 | [diff] [blame] | 314 | } |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 315 | // Chain to next filter. |
| 316 | grpc_call_next_op(exec_ctx, elem, op); |
| 317 | } |
| 318 | |
| 319 | const grpc_channel_filter grpc_client_deadline_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 320 | client_start_transport_stream_op_batch, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 321 | grpc_channel_next_op, |
| 322 | sizeof(base_call_data), |
| 323 | init_call_elem, |
| 324 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 325 | destroy_call_elem, |
Mark D. Roth | b3405f0a | 2016-09-09 08:46:28 -0700 | [diff] [blame] | 326 | 0, // sizeof(channel_data) |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 327 | init_channel_elem, |
| 328 | destroy_channel_elem, |
| 329 | grpc_call_next_get_peer, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 330 | grpc_channel_next_get_info, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 331 | "deadline", |
| 332 | }; |
| 333 | |
| 334 | const grpc_channel_filter grpc_server_deadline_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 335 | server_start_transport_stream_op_batch, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 336 | grpc_channel_next_op, |
| 337 | sizeof(server_call_data), |
| 338 | init_call_elem, |
| 339 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 340 | destroy_call_elem, |
Mark D. Roth | b3405f0a | 2016-09-09 08:46:28 -0700 | [diff] [blame] | 341 | 0, // sizeof(channel_data) |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 342 | init_channel_elem, |
| 343 | destroy_channel_elem, |
| 344 | grpc_call_next_get_peer, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 345 | grpc_channel_next_get_info, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 346 | "deadline", |
| 347 | }; |