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 | |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 37 | // The on_complete callback used when sending a cancel_error batch down the |
| 38 | // filter stack. Yields the call combiner when the batch returns. |
| 39 | static void yield_call_combiner(grpc_exec_ctx* exec_ctx, void* arg, |
| 40 | grpc_error* ignored) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 41 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)arg; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 42 | GRPC_CALL_COMBINER_STOP(exec_ctx, deadline_state->call_combiner, |
| 43 | "got on_complete from cancel_stream batch"); |
| 44 | GRPC_CALL_STACK_UNREF(exec_ctx, deadline_state->call_stack, "deadline_timer"); |
| 45 | } |
| 46 | |
| 47 | // This is called via the call combiner, so access to deadline_state is |
| 48 | // synchronized. |
| 49 | static void send_cancel_op_in_call_combiner(grpc_exec_ctx* exec_ctx, void* arg, |
| 50 | grpc_error* error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 51 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 52 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 53 | grpc_transport_stream_op_batch* batch = grpc_make_transport_stream_op( |
| 54 | GRPC_CLOSURE_INIT(&deadline_state->timer_callback, yield_call_combiner, |
| 55 | deadline_state, grpc_schedule_on_exec_ctx)); |
| 56 | batch->cancel_stream = true; |
| 57 | batch->payload->cancel_stream.cancel_error = GRPC_ERROR_REF(error); |
| 58 | elem->filter->start_transport_stream_op_batch(exec_ctx, elem, batch); |
| 59 | } |
| 60 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 61 | // Timer callback. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 62 | static void timer_callback(grpc_exec_ctx* exec_ctx, void* arg, |
| 63 | grpc_error* error) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 64 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 65 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 66 | if (error != GRPC_ERROR_CANCELLED) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 67 | error = grpc_error_set_int( |
| 68 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Deadline Exceeded"), |
| 69 | GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_DEADLINE_EXCEEDED); |
| 70 | grpc_call_combiner_cancel(exec_ctx, deadline_state->call_combiner, |
| 71 | GRPC_ERROR_REF(error)); |
| 72 | GRPC_CLOSURE_INIT(&deadline_state->timer_callback, |
| 73 | send_cancel_op_in_call_combiner, elem, |
| 74 | grpc_schedule_on_exec_ctx); |
| 75 | GRPC_CALL_COMBINER_START(exec_ctx, deadline_state->call_combiner, |
| 76 | &deadline_state->timer_callback, error, |
| 77 | "deadline exceeded -- sending cancel_stream op"); |
| 78 | } else { |
| 79 | GRPC_CALL_STACK_UNREF(exec_ctx, deadline_state->call_stack, |
| 80 | "deadline_timer"); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 81 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Starts the deadline timer. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 85 | // This is called via the call combiner, so access to deadline_state is |
| 86 | // synchronized. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 87 | static void start_timer_if_needed(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 88 | grpc_call_element* elem, |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 89 | grpc_millis deadline) { |
| 90 | if (deadline == GRPC_MILLIS_INF_FUTURE) { |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 91 | return; |
| 92 | } |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 93 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 94 | grpc_closure* closure = nullptr; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 95 | switch (deadline_state->timer_state) { |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 96 | case GRPC_DEADLINE_STATE_PENDING: |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 97 | // 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] | 98 | return; |
| 99 | case GRPC_DEADLINE_STATE_FINISHED: |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 100 | deadline_state->timer_state = GRPC_DEADLINE_STATE_PENDING; |
| 101 | // If we've already created and destroyed a timer, we always create a |
| 102 | // new closure: we have no other guarantee that the inlined closure is |
| 103 | // not in use (it may hold a pending call to timer_callback) |
| 104 | closure = |
| 105 | GRPC_CLOSURE_CREATE(timer_callback, elem, grpc_schedule_on_exec_ctx); |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 106 | break; |
| 107 | case GRPC_DEADLINE_STATE_INITIAL: |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 108 | deadline_state->timer_state = GRPC_DEADLINE_STATE_PENDING; |
| 109 | closure = |
| 110 | GRPC_CLOSURE_INIT(&deadline_state->timer_callback, timer_callback, |
| 111 | elem, grpc_schedule_on_exec_ctx); |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 112 | break; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 113 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 114 | GPR_ASSERT(closure != nullptr); |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 115 | GRPC_CALL_STACK_REF(deadline_state->call_stack, "deadline_timer"); |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 116 | grpc_timer_init(exec_ctx, &deadline_state->timer, deadline, closure); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Cancels the deadline timer. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 120 | // This is called via the call combiner, so access to deadline_state is |
| 121 | // synchronized. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 122 | static void cancel_timer_if_needed(grpc_exec_ctx* exec_ctx, |
| 123 | grpc_deadline_state* deadline_state) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 124 | if (deadline_state->timer_state == GRPC_DEADLINE_STATE_PENDING) { |
| 125 | deadline_state->timer_state = GRPC_DEADLINE_STATE_FINISHED; |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 126 | grpc_timer_cancel(exec_ctx, &deadline_state->timer); |
Craig Tiller | ac942f4 | 2017-02-22 09:13:14 -0800 | [diff] [blame] | 127 | } else { |
| 128 | // timer was either in STATE_INITAL (nothing to cancel) |
| 129 | // OR in STATE_FINISHED (again nothing to cancel) |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 130 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Callback run when the call is complete. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 134 | 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] | 135 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)arg; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 136 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 137 | // Invoke the next callback. |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 138 | GRPC_CLOSURE_RUN(exec_ctx, deadline_state->next_on_complete, |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 139 | GRPC_ERROR_REF(error)); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Inject our own on_complete callback into op. |
| 143 | static void inject_on_complete_cb(grpc_deadline_state* deadline_state, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 144 | grpc_transport_stream_op_batch* op) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 145 | deadline_state->next_on_complete = op->on_complete; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 146 | GRPC_CLOSURE_INIT(&deadline_state->on_complete, on_complete, deadline_state, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 147 | grpc_schedule_on_exec_ctx); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 148 | op->on_complete = &deadline_state->on_complete; |
| 149 | } |
| 150 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 151 | // Callback and associated state for starting the timer after call stack |
| 152 | // initialization has been completed. |
| 153 | struct start_timer_after_init_state { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 154 | bool in_call_combiner; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 155 | grpc_call_element* elem; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 156 | grpc_millis deadline; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 157 | grpc_closure closure; |
| 158 | }; |
| 159 | static void start_timer_after_init(grpc_exec_ctx* exec_ctx, void* arg, |
| 160 | grpc_error* error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 161 | struct start_timer_after_init_state* state = |
| 162 | (struct start_timer_after_init_state*)arg; |
| 163 | grpc_deadline_state* deadline_state = |
| 164 | (grpc_deadline_state*)state->elem->call_data; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 165 | if (!state->in_call_combiner) { |
| 166 | // We are initially called without holding the call combiner, so we |
| 167 | // need to bounce ourselves into it. |
| 168 | state->in_call_combiner = true; |
| 169 | GRPC_CALL_COMBINER_START(exec_ctx, deadline_state->call_combiner, |
| 170 | &state->closure, GRPC_ERROR_REF(error), |
| 171 | "scheduling deadline timer"); |
| 172 | return; |
| 173 | } |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 174 | start_timer_if_needed(exec_ctx, state->elem, state->deadline); |
| 175 | gpr_free(state); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 176 | GRPC_CALL_COMBINER_STOP(exec_ctx, deadline_state->call_combiner, |
| 177 | "done scheduling deadline timer"); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Craig Tiller | 71d6ce6 | 2017-04-06 09:10:09 -0700 | [diff] [blame] | 180 | void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 181 | grpc_call_stack* call_stack, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 182 | grpc_call_combiner* call_combiner, |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 183 | grpc_millis deadline) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 184 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Craig Tiller | 71d6ce6 | 2017-04-06 09:10:09 -0700 | [diff] [blame] | 185 | deadline_state->call_stack = call_stack; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 186 | deadline_state->call_combiner = call_combiner; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 187 | // Deadline will always be infinite on servers, so the timer will only be |
| 188 | // set on clients with a finite deadline. |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 189 | if (deadline != GRPC_MILLIS_INF_FUTURE) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 190 | // When the deadline passes, we indicate the failure by sending down |
| 191 | // an op with cancel_error set. However, we can't send down any ops |
| 192 | // until after the call stack is fully initialized. If we start the |
| 193 | // timer here, we have no guarantee that the timer won't pop before |
| 194 | // call stack initialization is finished. To avoid that problem, we |
| 195 | // create a closure to start the timer, and we schedule that closure |
| 196 | // to be run after call stack initialization is done. |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 197 | struct start_timer_after_init_state* state = |
| 198 | (struct start_timer_after_init_state*)gpr_zalloc(sizeof(*state)); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 199 | state->elem = elem; |
| 200 | state->deadline = deadline; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 201 | GRPC_CLOSURE_INIT(&state->closure, start_timer_after_init, state, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 202 | grpc_schedule_on_exec_ctx); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 203 | GRPC_CLOSURE_SCHED(exec_ctx, &state->closure, GRPC_ERROR_NONE); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 204 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Craig Tiller | 71d6ce6 | 2017-04-06 09:10:09 -0700 | [diff] [blame] | 207 | void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx, |
| 208 | grpc_call_element* elem) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 209 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Craig Tiller | 71d6ce6 | 2017-04-06 09:10:09 -0700 | [diff] [blame] | 210 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 211 | } |
| 212 | |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 213 | 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] | 214 | grpc_millis new_deadline) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 215 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 216 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 217 | start_timer_if_needed(exec_ctx, elem, new_deadline); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 220 | void grpc_deadline_state_client_start_transport_stream_op_batch( |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 221 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 222 | grpc_transport_stream_op_batch* op) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 223 | grpc_deadline_state* deadline_state = (grpc_deadline_state*)elem->call_data; |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 224 | if (op->cancel_stream) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 225 | cancel_timer_if_needed(exec_ctx, deadline_state); |
| 226 | } else { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 227 | // Make sure we know when the call is complete, so that we can cancel |
| 228 | // the timer. |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 229 | if (op->recv_trailing_metadata) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 230 | inject_on_complete_cb(deadline_state, op); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // |
| 236 | // filter code |
| 237 | // |
| 238 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 239 | // Constructor for channel_data. Used for both client and server filters. |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 240 | static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx, |
| 241 | grpc_channel_element* elem, |
| 242 | grpc_channel_element_args* args) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 243 | GPR_ASSERT(!args->is_last); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 244 | return GRPC_ERROR_NONE; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Destructor for channel_data. Used for both client and server filters. |
| 248 | static void destroy_channel_elem(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 249 | grpc_channel_element* elem) {} |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 250 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 251 | // Call data used for both client and server filter. |
| 252 | typedef struct base_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 253 | grpc_deadline_state deadline_state; |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 254 | } base_call_data; |
| 255 | |
| 256 | // Additional call data used only for the server filter. |
| 257 | typedef struct server_call_data { |
| 258 | base_call_data base; // Must be first. |
| 259 | // The closure for receiving initial metadata. |
| 260 | grpc_closure recv_initial_metadata_ready; |
| 261 | // Received initial metadata batch. |
| 262 | grpc_metadata_batch* recv_initial_metadata; |
| 263 | // The original recv_initial_metadata_ready closure, which we chain to |
| 264 | // after our own closure is invoked. |
| 265 | grpc_closure* next_recv_initial_metadata_ready; |
| 266 | } server_call_data; |
| 267 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 268 | // Constructor for call_data. Used for both client and server filters. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 269 | static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 270 | grpc_call_element* elem, |
Craig Tiller | c52ba3a | 2017-02-15 22:57:43 -0800 | [diff] [blame] | 271 | const grpc_call_element_args* args) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 272 | grpc_deadline_state_init(exec_ctx, elem, args->call_stack, |
| 273 | args->call_combiner, args->deadline); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 274 | return GRPC_ERROR_NONE; |
| 275 | } |
| 276 | |
| 277 | // Destructor for call_data. Used for both client and server filters. |
| 278 | static void destroy_call_elem(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 279 | const grpc_call_final_info* final_info, |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 280 | grpc_closure* ignored) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 281 | grpc_deadline_state_destroy(exec_ctx, elem); |
Mark D. Roth | d2b4533 | 2016-08-26 11:18:00 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 284 | // Method for starting a call op for client filter. |
Craig Tiller | e1b51da | 2017-03-31 15:44:33 -0700 | [diff] [blame] | 285 | static void client_start_transport_stream_op_batch( |
| 286 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 287 | grpc_transport_stream_op_batch* op) { |
| 288 | grpc_deadline_state_client_start_transport_stream_op_batch(exec_ctx, elem, |
| 289 | op); |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 290 | // Chain to next filter. |
| 291 | grpc_call_next_op(exec_ctx, elem, op); |
| 292 | } |
| 293 | |
| 294 | // Callback for receiving initial metadata on the server. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 295 | static void recv_initial_metadata_ready(grpc_exec_ctx* exec_ctx, void* arg, |
| 296 | grpc_error* error) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 297 | grpc_call_element* elem = (grpc_call_element*)arg; |
| 298 | server_call_data* calld = (server_call_data*)elem->call_data; |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 299 | // Get deadline from metadata and start the timer if needed. |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 300 | 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] | 301 | // Invoke the next callback. |
| 302 | calld->next_recv_initial_metadata_ready->cb( |
| 303 | exec_ctx, calld->next_recv_initial_metadata_ready->cb_arg, error); |
| 304 | } |
| 305 | |
| 306 | // Method for starting a call op for server filter. |
Craig Tiller | e1b51da | 2017-03-31 15:44:33 -0700 | [diff] [blame] | 307 | static void server_start_transport_stream_op_batch( |
| 308 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 309 | grpc_transport_stream_op_batch* op) { |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 310 | server_call_data* calld = (server_call_data*)elem->call_data; |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 311 | if (op->cancel_stream) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 312 | cancel_timer_if_needed(exec_ctx, &calld->base.deadline_state); |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 313 | } else { |
| 314 | // If we're receiving initial metadata, we need to get the deadline |
| 315 | // from the recv_initial_metadata_ready callback. So we inject our |
| 316 | // own callback into that hook. |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 317 | if (op->recv_initial_metadata) { |
| 318 | calld->next_recv_initial_metadata_ready = |
| 319 | op->payload->recv_initial_metadata.recv_initial_metadata_ready; |
| 320 | calld->recv_initial_metadata = |
| 321 | op->payload->recv_initial_metadata.recv_initial_metadata; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 322 | GRPC_CLOSURE_INIT(&calld->recv_initial_metadata_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 323 | recv_initial_metadata_ready, elem, |
| 324 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 759965c | 2017-03-02 08:50:18 -0800 | [diff] [blame] | 325 | op->payload->recv_initial_metadata.recv_initial_metadata_ready = |
| 326 | &calld->recv_initial_metadata_ready; |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 327 | } |
| 328 | // Make sure we know when the call is complete, so that we can cancel |
| 329 | // the timer. |
| 330 | // Note that we trigger this on recv_trailing_metadata, even though |
| 331 | // the client never sends trailing metadata, because this is the |
| 332 | // 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] | 333 | if (op->recv_trailing_metadata) { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 334 | inject_on_complete_cb(&calld->base.deadline_state, op); |
Mark D. Roth | 1bbe6cb | 2016-08-31 08:33:37 -0700 | [diff] [blame] | 335 | } |
Mark D. Roth | d2b4533 | 2016-08-26 11:18:00 -0700 | [diff] [blame] | 336 | } |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 337 | // Chain to next filter. |
| 338 | grpc_call_next_op(exec_ctx, elem, op); |
| 339 | } |
| 340 | |
| 341 | const grpc_channel_filter grpc_client_deadline_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 342 | client_start_transport_stream_op_batch, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 343 | grpc_channel_next_op, |
| 344 | sizeof(base_call_data), |
| 345 | init_call_elem, |
| 346 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 347 | destroy_call_elem, |
Mark D. Roth | b3405f0a | 2016-09-09 08:46:28 -0700 | [diff] [blame] | 348 | 0, // sizeof(channel_data) |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 349 | init_channel_elem, |
| 350 | destroy_channel_elem, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 351 | grpc_channel_next_get_info, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 352 | "deadline", |
| 353 | }; |
| 354 | |
| 355 | const grpc_channel_filter grpc_server_deadline_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 356 | server_start_transport_stream_op_batch, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 357 | grpc_channel_next_op, |
| 358 | sizeof(server_call_data), |
| 359 | init_call_elem, |
| 360 | grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 361 | destroy_call_elem, |
Mark D. Roth | b3405f0a | 2016-09-09 08:46:28 -0700 | [diff] [blame] | 362 | 0, // sizeof(channel_data) |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 363 | init_channel_elem, |
| 364 | destroy_channel_elem, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 365 | grpc_channel_next_get_info, |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 366 | "deadline", |
| 367 | }; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 368 | |
| 369 | bool grpc_deadline_checking_enabled(const grpc_channel_args* channel_args) { |
Craig Tiller | 41f2ed6 | 2017-04-06 09:33:48 -0700 | [diff] [blame] | 370 | return grpc_channel_arg_get_bool( |
| 371 | grpc_channel_args_find(channel_args, GRPC_ARG_ENABLE_DEADLINE_CHECKS), |
| 372 | !grpc_channel_args_want_minimal_stack(channel_args)); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static bool maybe_add_deadline_filter(grpc_exec_ctx* exec_ctx, |
| 376 | grpc_channel_stack_builder* builder, |
| 377 | void* arg) { |
| 378 | return grpc_deadline_checking_enabled( |
| 379 | grpc_channel_stack_builder_get_channel_arguments(builder)) |
Craig Tiller | ed38016 | 2017-07-11 08:34:26 -0700 | [diff] [blame] | 380 | ? grpc_channel_stack_builder_prepend_filter( |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame^] | 381 | builder, (const grpc_channel_filter*)arg, nullptr, nullptr) |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 382 | : true; |
| 383 | } |
| 384 | |
Yash Tibrewal | 8306284 | 2017-09-21 18:56:08 -0700 | [diff] [blame] | 385 | extern "C" void grpc_deadline_filter_init(void) { |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 386 | grpc_channel_init_register_stage( |
| 387 | GRPC_CLIENT_DIRECT_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
| 388 | maybe_add_deadline_filter, (void*)&grpc_client_deadline_filter); |
| 389 | grpc_channel_init_register_stage( |
| 390 | GRPC_SERVER_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, |
| 391 | maybe_add_deadline_filter, (void*)&grpc_server_deadline_filter); |
| 392 | } |
| 393 | |
Yash Tibrewal | 8306284 | 2017-09-21 18:56:08 -0700 | [diff] [blame] | 394 | extern "C" void grpc_deadline_filter_shutdown(void) {} |