blob: d8db9a9f975c01b80d136637be6623a43fea341d [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;
Mark D. Roth72f6da82016-09-02 13:42:38 -070050 grpc_timer timer;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080051 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
Craig Tiller6f417882017-02-16 14:09:39 -080065// assumes elem->call_data is zero'd
Mark D. Rothf28763c2016-09-14 15:18:40 -070066void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
Mark D. Rothe40dd292016-10-05 14:58:37 -070067 grpc_call_stack* call_stack);
Mark D. Rothf28763c2016-09-14 15:18:40 -070068void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx,
69 grpc_call_element* elem);
Mark D. Rothe40dd292016-10-05 14:58:37 -070070
71// Starts the timer with the specified deadline.
72// Should be called from the filter's init_call_elem() method.
73void 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.
83void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
84 gpr_timespec new_deadline);
85
Craig Tillera0f3abd2017-03-31 15:42:16 -070086// To be called from the client-side filter's start_transport_stream_op_batch()
Mark D. Rothe40dd292016-10-05 14:58:37 -070087// 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 Tillera0f3abd2017-03-31 15:42:16 -070092void grpc_deadline_state_client_start_transport_stream_op_batch(
Mark D. Roth72f6da82016-09-02 13:42:38 -070093 grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
Craig Tillera0f3abd2017-03-31 15:42:16 -070094 grpc_transport_stream_op_batch* op);
Mark D. Roth72f6da82016-09-02 13:42:38 -070095
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. Roth14c072c2016-08-26 08:31:34 -070099extern const grpc_channel_filter grpc_client_deadline_filter;
100extern const grpc_channel_filter grpc_server_deadline_filter;
101
Mark D. Roth932b10c2016-09-09 08:44:30 -0700102#endif /* GRPC_CORE_LIB_CHANNEL_DEADLINE_FILTER_H */