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