1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crateOptionalSend;
use crateRaftTypeConfig;
use crateOneshotSender;
use crateResponder;
use crateTypeConfigExt;
use crateOneshotReceiverOf;
use crateOneshotSenderOf;
/// A [`Responder`] that sends the result of a write through a oneshot channel of the configured
/// [`AsyncRuntime`].
///
/// It sends only the final result and ignores the commit notification. Use
/// [`ProgressResponder`] to also be notified when the write is committed.
///
/// # Usage
///
/// Select this type as `Responder<T>` in the type config, as the example below does: the write
/// APIs that take a responder accept only the configured type, and [`declare_raft_types!`]
/// selects [`ProgressResponder`] by default. Then create a responder with
/// [`new_pair()`](Self::new_pair), keep the receiver, and attach the responder to a write with
/// [`WriteRequest::responder()`], [`Raft::write_many()`] or [`Raft::client_write_ff()`].
///
/// APIs that return the result of a write themselves, such as [`Raft::client_write()`],
/// [`Raft::client_write_many()`] and [`Raft::change_membership()`], wait on a responder that
/// they create internally. They do not use the configured responder type, and selecting
/// `OneshotResponder` does not change them.
///
/// # Example
///
/// ```no_run
/// # use openraft::Raft;
/// # use openraft::storage::RaftStateMachine;
/// use openraft::impls::OneshotResponder;
///
/// openraft::declare_raft_types!(
/// TypeConfig:
/// Responder<T> = OneshotResponder<Self, T>,
/// );
///
/// # async fn example<SM>(
/// # raft: &Raft<TypeConfig, SM>,
/// # request: String,
/// # ) -> Result<(), Box<dyn std::error::Error>>
/// # where SM: RaftStateMachine<TypeConfig>
/// # {
/// let (responder, rx) = OneshotResponder::new_pair();
///
/// // `Err` means that RaftCore has stopped and did not receive the write. `Ok` means only that
/// // the write was submitted: RaftCore may still reject it.
/// raft.write(request).responder(responder).await?;
///
/// let result = match rx.await {
/// Ok(result) => result,
/// // The responder was dropped without a result, such as when RaftCore stopped.
/// // The write may still be committed and applied.
/// Err(_) => return Err("no result arrived for the write".into()),
/// };
///
/// // The result arrived: the applied write's response, or the write's error, such as
/// // `ForwardToLeader` from a follower.
/// let response = result?;
/// println!("applied at {}: {}", response.log_id, response.data);
/// # Ok(())
/// # }
/// ```
///
/// [`AsyncRuntime`]: crate::AsyncRuntime
/// [`ProgressResponder`]: crate::impls::ProgressResponder
/// [`declare_raft_types!`]: crate::declare_raft_types
/// [`WriteRequest::responder()`]: crate::raft::WriteRequest::responder
/// [`Raft::write_many()`]: crate::Raft::write_many
/// [`Raft::client_write_ff()`]: crate::Raft::client_write_ff
/// [`Raft::client_write()`]: crate::Raft::client_write
/// [`Raft::client_write_many()`]: crate::Raft::client_write_many
/// [`Raft::change_membership()`]: crate::Raft::change_membership