blob: f4a1110ee6eeb1ca60aa3673b76a076af7f5ed74 [file] [log] [blame]
Mark D. Roth14c072c2016-08-26 08:31:34 -07001//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002// Copyright 2016 gRPC authors.
Mark D. Roth14c072c2016-08-26 08:31:34 -07003//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004// 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. Roth14c072c2016-08-26 08:31:34 -07007//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008// http://www.apache.org/licenses/LICENSE-2.0
Mark D. Roth14c072c2016-08-26 08:31:34 -07009//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010// 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. Roth14c072c2016-08-26 08:31:34 -070015//
16
Craig Tillere3593d92017-04-03 14:51:02 -070017#ifndef GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H
18#define GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H
Mark D. Roth14c072c2016-08-26 08:31:34 -070019
20#include "src/core/lib/channel/channel_stack.h"
Mark D. Roth72f6da82016-09-02 13:42:38 -070021#include "src/core/lib/iomgr/timer.h"
Mark D. Roth14c072c2016-08-26 08:31:34 -070022
Yash Tibrewala7e6d652017-09-20 18:56:37 -070023#ifdef __cplusplus
24extern "C" {
25#endif
26
Craig Tillerc84886b2017-02-16 13:10:38 -080027typedef 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 Tiller4447c2c2017-02-16 12:35:13 -080032
Mark D. Roth72f6da82016-09-02 13:42:38 -070033// State used for filters that enforce call deadlines.
Mark D. Rothf28763c2016-09-14 15:18:40 -070034// Must be the first field in the filter's call_data.
Mark D. Roth72f6da82016-09-02 13:42:38 -070035typedef 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. Roth764cf042017-09-01 09:00:06 -070038 grpc_call_combiner* call_combiner;
39 grpc_deadline_timer_state timer_state;
Mark D. Roth72f6da82016-09-02 13:42:38 -070040 grpc_timer timer;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080041 grpc_closure timer_callback;
Mark D. Roth72f6da82016-09-02 13:42:38 -070042 // 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. Roth72f6da82016-09-02 13:42:38 -070048} grpc_deadline_state;
49
Mark D. Rothf28763c2016-09-14 15:18:40 -070050//
Mark D. Rothe40dd292016-10-05 14:58:37 -070051// NOTE: All of these functions require that the first field in
52// elem->call_data is a grpc_deadline_state.
Mark D. Rothf28763c2016-09-14 15:18:40 -070053//
Mark D. Rothe40dd292016-10-05 14:58:37 -070054
Craig Tiller6f417882017-02-16 14:09:39 -080055// assumes elem->call_data is zero'd
Mark D. Rothf28763c2016-09-14 15:18:40 -070056void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
Craig Tiller71d6ce62017-04-06 09:10:09 -070057 grpc_call_stack* call_stack,
Mark D. Roth764cf042017-09-01 09:00:06 -070058 grpc_call_combiner* call_combiner,
Craig Tiller71d6ce62017-04-06 09:10:09 -070059 gpr_timespec deadline);
Mark D. Rothf28763c2016-09-14 15:18:40 -070060void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx,
61 grpc_call_element* elem);
Mark D. Rothe40dd292016-10-05 14:58:37 -070062
Mark D. Rothe40dd292016-10-05 14:58:37 -070063// 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. Roth764cf042017-09-01 09:00:06 -070070//
71// Note: Must be called while holding the call combiner.
Mark D. Rothe40dd292016-10-05 14:58:37 -070072void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
73 gpr_timespec new_deadline);
74
Craig Tillera0f3abd2017-03-31 15:42:16 -070075// To be called from the client-side filter's start_transport_stream_op_batch()
Mark D. Rothe40dd292016-10-05 14:58:37 -070076// 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. Roth764cf042017-09-01 09:00:06 -070081//
82// Note: Must be called while holding the call combiner.
Craig Tillera0f3abd2017-03-31 15:42:16 -070083void grpc_deadline_state_client_start_transport_stream_op_batch(
Mark D. Roth72f6da82016-09-02 13:42:38 -070084 grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
Craig Tillera0f3abd2017-03-31 15:42:16 -070085 grpc_transport_stream_op_batch* op);
Mark D. Roth72f6da82016-09-02 13:42:38 -070086
Craig Tiller3be7dd02017-04-03 14:30:03 -070087// Should deadline checking be performed (according to channel args)
88bool grpc_deadline_checking_enabled(const grpc_channel_args* args);
89
Mark D. Roth72f6da82016-09-02 13:42:38 -070090// 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. Roth14c072c2016-08-26 08:31:34 -070093extern const grpc_channel_filter grpc_client_deadline_filter;
94extern const grpc_channel_filter grpc_server_deadline_filter;
95
Yash Tibrewala7e6d652017-09-20 18:56:37 -070096#ifdef __cplusplus
97}
98#endif
99
100#endif /* GRPC_CORE_EXT_FILTERS_DEADLINE_DEADLINE_FILTER_H */