blob: 94717f6bc70a33c03d1f7cec327c105f54e2a774 [file] [log] [blame]
Mark D. Roth14c072c2016-08-26 08:31:34 -07001//
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. Roth72f6da82016-09-02 13:42:38 -070036#include "src/core/lib/iomgr/timer.h"
Mark D. Roth14c072c2016-08-26 08:31:34 -070037
Craig Tillerc84886b2017-02-16 13:10:38 -080038typedef 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 Tiller4447c2c2017-02-16 12:35:13 -080043
Mark D. Roth72f6da82016-09-02 13:42:38 -070044// State used for filters that enforce call deadlines.
Mark D. Rothf28763c2016-09-14 15:18:40 -070045// Must be the first field in the filter's call_data.
Mark D. Roth72f6da82016-09-02 13:42:38 -070046typedef struct grpc_deadline_state {
47 // We take a reference to the call stack for the timer callback.
48 grpc_call_stack* call_stack;
Craig Tillerc84886b2017-02-16 13:10:38 -080049 gpr_atm timer_state;
50 grpc_timer timer;
51 grpc_closure timer_callback;
Mark D. Roth72f6da82016-09-02 13:42:38 -070052 // 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. Roth72f6da82016-09-02 13:42:38 -070058} grpc_deadline_state;
59
Mark D. Rothf28763c2016-09-14 15:18:40 -070060//
Mark D. Rothe40dd292016-10-05 14:58:37 -070061// NOTE: All of these functions require that the first field in
62// elem->call_data is a grpc_deadline_state.
Mark D. Rothf28763c2016-09-14 15:18:40 -070063//
Mark D. Rothe40dd292016-10-05 14:58:37 -070064
Mark D. Rothf28763c2016-09-14 15:18:40 -070065void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
Mark D. Rothe40dd292016-10-05 14:58:37 -070066 grpc_call_stack* call_stack);
Mark D. Rothf28763c2016-09-14 15:18:40 -070067void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx,
68 grpc_call_element* elem);
Mark D. Rothe40dd292016-10-05 14:58:37 -070069
70// Starts the timer with the specified deadline.
71// Should be called from the filter's init_call_elem() method.
72void 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.
82void 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. Roth72f6da82016-09-02 13:42:38 -070091void 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. Roth14c072c2016-08-26 08:31:34 -070098extern const grpc_channel_filter grpc_client_deadline_filter;
99extern const grpc_channel_filter grpc_server_deadline_filter;
100
Mark D. Roth932b10c2016-09-09 08:44:30 -0700101#endif /* GRPC_CORE_LIB_CHANNEL_DEADLINE_FILTER_H */