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; |
| 50 | grpc_timer timer; |
| 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 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 65 | 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] | 66 | grpc_call_stack* call_stack); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 67 | void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx, |
| 68 | grpc_call_element* elem); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 69 | |
| 70 | // Starts the timer with the specified deadline. |
| 71 | // Should be called from the filter's init_call_elem() method. |
| 72 | void grpc_deadline_state_start(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 73 | gpr_timespec deadline); |
| 74 | |
| 75 | // Cancels the existing timer and starts a new one with new_deadline. |
| 76 | // |
| 77 | // Note: It is generally safe to call this with an earlier deadline |
| 78 | // value than the current one, but not the reverse. No checks are done |
| 79 | // to ensure that the timer callback is not invoked while it is in the |
| 80 | // process of being reset, which means that attempting to increase the |
| 81 | // deadline may result in the timer being called twice. |
| 82 | void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 83 | gpr_timespec new_deadline); |
| 84 | |
| 85 | // To be called from the client-side filter's start_transport_stream_op() |
| 86 | // method. Ensures that the deadline timer is cancelled when the call |
| 87 | // is completed. |
| 88 | // |
| 89 | // Note: It is the caller's responsibility to chain to the next filter if |
| 90 | // necessary after this function returns. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 91 | void grpc_deadline_state_client_start_transport_stream_op( |
| 92 | grpc_exec_ctx* exec_ctx, grpc_call_element* elem, |
| 93 | grpc_transport_stream_op* op); |
| 94 | |
| 95 | // Deadline filters for direct client channels and server channels. |
| 96 | // Note: Deadlines for non-direct client channels are handled by the |
| 97 | // client_channel filter. |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 98 | extern const grpc_channel_filter grpc_client_deadline_filter; |
| 99 | extern const grpc_channel_filter grpc_server_deadline_filter; |
| 100 | |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 101 | #endif /* GRPC_CORE_LIB_CHANNEL_DEADLINE_FILTER_H */ |