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 | e3593d9 | 2017-04-03 14:51:02 -0700 | [diff] [blame] | 17 | #ifndef GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H |
| 18 | #define GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 19 | |
| 20 | #include "src/core/lib/channel/channel_stack.h" |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 21 | #include "src/core/lib/iomgr/timer.h" |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 22 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 27 | typedef enum grpc_deadline_timer_state { |
| 28 | GRPC_DEADLINE_STATE_INITIAL, |
| 29 | GRPC_DEADLINE_STATE_PENDING, |
| 30 | GRPC_DEADLINE_STATE_FINISHED |
| 31 | } grpc_deadline_timer_state; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 32 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 33 | // State used for filters that enforce call deadlines. |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 34 | // Must be the first field in the filter's call_data. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 35 | typedef struct grpc_deadline_state { |
| 36 | // We take a reference to the call stack for the timer callback. |
| 37 | grpc_call_stack* call_stack; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 38 | grpc_call_combiner* call_combiner; |
| 39 | grpc_deadline_timer_state timer_state; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 40 | grpc_timer timer; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 41 | grpc_closure timer_callback; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 42 | // Closure to invoke when the call is complete. |
| 43 | // We use this to cancel the timer. |
| 44 | grpc_closure on_complete; |
| 45 | // The original on_complete closure, which we chain to after our own |
| 46 | // closure is invoked. |
| 47 | grpc_closure* next_on_complete; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 48 | } grpc_deadline_state; |
| 49 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 50 | // |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 51 | // NOTE: All of these functions require that the first field in |
| 52 | // elem->call_data is a grpc_deadline_state. |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 53 | // |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 54 | |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 55 | // assumes elem->call_data is zero'd |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 56 | void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
Craig Tiller | 71d6ce6 | 2017-04-06 09:10:09 -0700 | [diff] [blame] | 57 | grpc_call_stack* call_stack, |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 58 | grpc_call_combiner* call_combiner, |
Craig Tiller | 71d6ce6 | 2017-04-06 09:10:09 -0700 | [diff] [blame] | 59 | gpr_timespec deadline); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 60 | void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx, |
| 61 | grpc_call_element* elem); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 62 | |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 63 | // Cancels the existing timer and starts a new one with new_deadline. |
| 64 | // |
| 65 | // Note: It is generally safe to call this with an earlier deadline |
| 66 | // value than the current one, but not the reverse. No checks are done |
| 67 | // to ensure that the timer callback is not invoked while it is in the |
| 68 | // process of being reset, which means that attempting to increase the |
| 69 | // deadline may result in the timer being called twice. |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 70 | // |
| 71 | // Note: Must be called while holding the call combiner. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 72 | void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 73 | gpr_timespec new_deadline); |
| 74 | |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 75 | // To be called from the client-side filter's start_transport_stream_op_batch() |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 76 | // method. Ensures that the deadline timer is cancelled when the call |
| 77 | // is completed. |
| 78 | // |
| 79 | // Note: It is the caller's responsibility to chain to the next filter if |
| 80 | // necessary after this function returns. |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 81 | // |
| 82 | // Note: Must be called while holding the call combiner. |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 83 | void grpc_deadline_state_client_start_transport_stream_op_batch( |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 84 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 85 | grpc_transport_stream_op_batch* op); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 86 | |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 87 | // Should deadline checking be performed (according to channel args) |
| 88 | bool grpc_deadline_checking_enabled(const grpc_channel_args* args); |
| 89 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 90 | // Deadline filters for direct client channels and server channels. |
| 91 | // Note: Deadlines for non-direct client channels are handled by the |
| 92 | // client_channel filter. |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 93 | extern const grpc_channel_filter grpc_client_deadline_filter; |
| 94 | extern const grpc_channel_filter grpc_server_deadline_filter; |
| 95 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 96 | #ifdef __cplusplus |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | #endif /* GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H */ |