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