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