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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use crate::nats_client::{NatsClient, NatsClientOptions, NatsSid, ClosableMessage};
use crate::nuid::NUID;
use std::{
collections::HashMap,
sync::Arc,
};
use tokio::sync::RwLock;
use tokio::sync::mpsc::UnboundedSender;
use std::fmt::{Debug, Formatter, Error};
pub mod client;
const DEFAULT_DISCOVER_PREFIX: &str = "_STAN.discover";
const DEFAULT_ACK_PREFIX: &str = "_STAN.acks";
const DEFAULT_MAX_PUB_ACKS_INFLIGHT: u32 = 16384;
const DEFAULT_PING_INTERVAL: u32 = 5;
const DEFAULT_PING_MAX_OUT: u32 = 3;
const DEFAULT_ACK_WAIT: i32 = 30 * 60000;
const DEFAULT_MAX_INFLIGHT: i32 = 1024;
#[derive(Debug, Clone, PartialEq, Builder)]
#[builder(setter(into), default)]
pub struct StanOptions {
pub nats_options: NatsClientOptions,
pub cluster_id: String,
pub client_id: String,
pub ping_interval: u32,
pub ping_max_out: u32,
pub max_pub_acks_inflight: u32,
pub discover_prefix: String,
pub ack_prefix: String,
}
#[derive(Debug, Clone)]
pub struct StanSid(pub(crate) NatsSid);
impl StanOptions {
pub fn new<S>(cluster_id: S, client_id: S) -> StanOptions where S: ToString{
StanOptions{
client_id: client_id.to_string(),
cluster_id: cluster_id.to_string(),
..Default::default()
}
}
pub fn with_options<T, S>(nats_options: T, cluster_id: S, client_id: S) -> StanOptions
where T: Into<NatsClientOptions> ,
S: ToString{
StanOptions{
client_id: client_id.to_string(),
cluster_id: cluster_id.to_string(),
nats_options: nats_options.into(),
..Default::default()
}
}
pub fn builder() -> StanOptionsBuilder {
StanOptionsBuilder::default()
}
}
impl Default for StanOptions {
fn default() -> Self {
StanOptions {
nats_options: NatsClientOptions::default(),
cluster_id: String::from(""),
client_id: String::from(""),
ping_interval: DEFAULT_PING_INTERVAL,
ping_max_out: DEFAULT_PING_MAX_OUT,
max_pub_acks_inflight: DEFAULT_MAX_PUB_ACKS_INFLIGHT,
discover_prefix: DEFAULT_DISCOVER_PREFIX.into(),
ack_prefix: DEFAULT_ACK_PREFIX.into(),
}
}
}
pub(crate) struct AckHandler(Box<dyn Fn() -> () + Send + Sync>);
impl Drop for AckHandler {
fn drop(&mut self) {
self.0()
}
}
impl Debug for AckHandler {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.write_str("<ack-handler>")
}
}
#[derive(Debug, Builder)]
#[builder(default)]
pub struct StanMessage {
pub subject: String,
pub reply_to: Option<String>,
pub payload: Vec<u8>,
pub timestamp: i64,
pub sequence: u64,
pub redelivered: bool,
pub ack_inbox: Option<String>,
#[builder(setter(skip))]
ack_handler: Option<AckHandler>,
}
impl StanMessage {
pub fn new(subject: String, payload: Vec<u8>) -> Self {
StanMessage {
subject,
payload,
reply_to: None,
timestamp: 0,
sequence: 0,
redelivered: false,
ack_inbox: None,
ack_handler: None,
}
}
pub fn with_reply(subject: String, payload: Vec<u8>, reply_to: Option<String>) -> Self {
StanMessage {
subject,
payload,
reply_to,
timestamp: 0,
sequence: 0,
redelivered: false,
ack_inbox: None,
ack_handler: None,
}
}
pub fn builder() -> StanMessageBuilder {
StanMessageBuilder::default()
}
}
impl Default for StanMessage {
fn default() -> Self {
use std::time::{SystemTime, UNIX_EPOCH};
let now = SystemTime::now();
let tstamp = now.duration_since(UNIX_EPOCH).unwrap();
let tstamp_ms = tstamp.as_millis() as i64;
StanMessage {
subject: String::new(),
reply_to: None,
payload: Vec::new(),
timestamp: tstamp_ms,
sequence: 0,
redelivered: false,
ack_inbox: None,
ack_handler: None,
}
}
}
impl Clone for StanMessage {
fn clone(&self) -> Self {
StanMessage {
subject: self.subject.clone(),
reply_to: self.reply_to.clone(),
payload: self.payload.clone(),
timestamp: self.timestamp,
sequence: self.sequence,
redelivered: self.redelivered,
ack_inbox: self.ack_inbox.clone(),
ack_handler: None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum StartPosition {
NewOnly = 0,
LastReceived = 1,
TimeDeltaStart = 2,
SequenceStart = 3,
First = 4,
}
#[derive(Clone, Debug, PartialEq, Builder)]
#[builder(default)]
pub struct StanSubscribe {
pub subject: String,
pub queue_group: Option<String>,
pub durable_name: Option<String>,
pub max_in_flight: i32,
pub ack_wait_in_secs: i32,
pub start_position: StartPosition,
pub start_sequence: u64,
pub start_time_delta: Option<i32>,
pub manual_acks: bool,
}
impl StanSubscribe {
pub fn builder() -> StanSubscribeBuilder {
StanSubscribeBuilder::default()
}
}
impl Default for StanSubscribe {
fn default() -> Self {
StanSubscribe {
subject: String::from(""),
queue_group: None,
durable_name: None,
max_in_flight: DEFAULT_MAX_INFLIGHT,
ack_wait_in_secs: DEFAULT_ACK_WAIT,
start_position: StartPosition::LastReceived,
start_sequence: 0,
start_time_delta: None,
manual_acks: false,
}
}
}
#[derive(Clone, Debug)]
struct Subscription {
client_id: String,
subject: String,
queue_group: Option<String>,
durable_name: Option<String>,
max_in_flight: i32,
ack_wait_in_secs: i32,
inbox: String,
ack_inbox: String,
unsub_requests: String,
close_requests: String,
sender: UnboundedSender<ClosableMessage>,
}
pub struct StanClient {
pub options: StanOptions,
pub nats_client: Arc<NatsClient>,
pub client_id: String,
client_info: Arc<RwLock<ClientInfo>>,
id_generator: Arc<RwLock<NUID>>,
conn_id: RwLock<Vec<u8>>,
subscriptions: RwLock<HashMap<String, Subscription>>,
self_reference: RwLock<Option<Arc<StanClient>>>,
}
#[derive(Clone, Debug, Default)]
pub struct ClientInfo {
pub_prefix: String,
sub_requests: String,
unsub_requests: String,
sub_close_requests: String,
close_requests: String,
ping_requests: String,
public_key: String,
}