Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | // |
| 31 | |
| 32 | #ifndef GRPC_CORE_LIB_CHANNEL_DEADLINE_FILTER_H |
| 33 | #define GRPC_CORE_LIB_CHANNEL_DEADLINE_FILTER_H |
| 34 | |
| 35 | #include "src/core/lib/channel/channel_stack.h" |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 36 | #include "src/core/lib/iomgr/timer.h" |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 37 | |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 38 | typedef enum grpc_deadline_timer_state { |
| 39 | GRPC_DEADLINE_STATE_INITIAL, |
| 40 | GRPC_DEADLINE_STATE_PENDING, |
| 41 | GRPC_DEADLINE_STATE_FINISHED |
| 42 | } grpc_deadline_timer_state; |
Craig Tiller | 4447c2c | 2017-02-16 12:35:13 -0800 | [diff] [blame] | 43 | |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 44 | // State used for filters that enforce call deadlines. |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 45 | // Must be the first field in the filter's call_data. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 46 | typedef struct grpc_deadline_state { |
| 47 | // We take a reference to the call stack for the timer callback. |
| 48 | grpc_call_stack* call_stack; |
Craig Tiller | c84886b | 2017-02-16 13:10:38 -0800 | [diff] [blame] | 49 | gpr_atm timer_state; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 50 | grpc_timer timer; |
Masood Malekghassemi | b5b4372 | 2017-01-05 15:07:26 -0800 | [diff] [blame] | 51 | grpc_closure timer_callback; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 52 | // Closure to invoke when the call is complete. |
| 53 | // We use this to cancel the timer. |
| 54 | grpc_closure on_complete; |
| 55 | // The original on_complete closure, which we chain to after our own |
| 56 | // closure is invoked. |
| 57 | grpc_closure* next_on_complete; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 58 | } grpc_deadline_state; |
| 59 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 60 | // |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 61 | // NOTE: All of these functions require that the first field in |
| 62 | // elem->call_data is a grpc_deadline_state. |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 63 | // |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 64 | |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 65 | // assumes elem->call_data is zero'd |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 66 | void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 67 | grpc_call_stack* call_stack); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 68 | void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx, |
| 69 | grpc_call_element* elem); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 70 | |
| 71 | // Starts the timer with the specified deadline. |
| 72 | // Should be called from the filter's init_call_elem() method. |
| 73 | void grpc_deadline_state_start(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 74 | gpr_timespec deadline); |
| 75 | |
| 76 | // Cancels the existing timer and starts a new one with new_deadline. |
| 77 | // |
| 78 | // Note: It is generally safe to call this with an earlier deadline |
| 79 | // value than the current one, but not the reverse. No checks are done |
| 80 | // to ensure that the timer callback is not invoked while it is in the |
| 81 | // process of being reset, which means that attempting to increase the |
| 82 | // deadline may result in the timer being called twice. |
| 83 | void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 84 | gpr_timespec new_deadline); |
| 85 | |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 86 | // 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] | 87 | // method. Ensures that the deadline timer is cancelled when the call |
| 88 | // is completed. |
| 89 | // |
| 90 | // Note: It is the caller's responsibility to chain to the next filter if |
| 91 | // necessary after this function returns. |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 92 | void grpc_deadline_state_client_start_transport_stream_op_batch( |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 93 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame^] | 94 | grpc_transport_stream_op_batch* op); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 95 | |
| 96 | // Deadline filters for direct client channels and server channels. |
| 97 | // Note: Deadlines for non-direct client channels are handled by the |
| 98 | // client_channel filter. |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 99 | extern const grpc_channel_filter grpc_client_deadline_filter; |
| 100 | extern const grpc_channel_filter grpc_server_deadline_filter; |
| 101 | |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 102 | #endif /* GRPC_CORE_LIB_CHANNEL_DEADLINE_FILTER_H */ |