1 // 2 // Copyright 2016 gRPC authors. 3 // 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 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 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. 15 // 16 17 #ifndef GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H 18 #define GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include "src/core/lib/channel/channel_stack.h" 23 #include "src/core/lib/iomgr/timer.h" 24 25 typedef enum grpc_deadline_timer_state { 26 GRPC_DEADLINE_STATE_INITIAL, 27 GRPC_DEADLINE_STATE_PENDING, 28 GRPC_DEADLINE_STATE_FINISHED 29 } grpc_deadline_timer_state; 30 31 // State used for filters that enforce call deadlines. 32 // Must be the first field in the filter's call_data. 33 typedef struct grpc_deadline_state { 34 // We take a reference to the call stack for the timer callback. 35 grpc_call_stack* call_stack; 36 grpc_call_combiner* call_combiner; 37 grpc_deadline_timer_state timer_state; 38 grpc_timer timer; 39 grpc_closure timer_callback; 40 // Closure to invoke when we receive trailing metadata. 41 // We use this to cancel the timer. 42 grpc_closure recv_trailing_metadata_ready; 43 // The original recv_trailing_metadata_ready closure, which we chain to 44 // after our own closure is invoked. 45 grpc_closure* original_recv_trailing_metadata_ready; 46 } grpc_deadline_state; 47 48 // 49 // NOTE: All of these functions require that the first field in 50 // elem->call_data is a grpc_deadline_state. 51 // 52 53 // assumes elem->call_data is zero'd 54 void grpc_deadline_state_init(grpc_call_element* elem, 55 grpc_call_stack* call_stack, 56 grpc_call_combiner* call_combiner, 57 grpc_millis deadline); 58 59 void grpc_deadline_state_destroy(grpc_call_element* elem); 60 61 // Cancels the existing timer and starts a new one with new_deadline. 62 // 63 // Note: It is generally safe to call this with an earlier deadline 64 // value than the current one, but not the reverse. No checks are done 65 // to ensure that the timer callback is not invoked while it is in the 66 // process of being reset, which means that attempting to increase the 67 // deadline may result in the timer being called twice. 68 // 69 // Note: Must be called while holding the call combiner. 70 void grpc_deadline_state_reset(grpc_call_element* elem, 71 grpc_millis new_deadline); 72 73 // To be called from the client-side filter's start_transport_stream_op_batch() 74 // method. Ensures that the deadline timer is cancelled when the call 75 // is completed. 76 // 77 // Note: It is the caller's responsibility to chain to the next filter if 78 // necessary after this function returns. 79 // 80 // Note: Must be called while holding the call combiner. 81 void grpc_deadline_state_client_start_transport_stream_op_batch( 82 grpc_call_element* elem, grpc_transport_stream_op_batch* op); 83 84 // Should deadline checking be performed (according to channel args) 85 bool grpc_deadline_checking_enabled(const grpc_channel_args* args); 86 87 // Deadline filters for direct client channels and server channels. 88 // Note: Deadlines for non-direct client channels are handled by the 89 // client_channel filter. 90 extern const grpc_channel_filter grpc_client_deadline_filter; 91 extern const grpc_channel_filter grpc_server_deadline_filter; 92 93 #endif /* GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H */ 94