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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::cell::UnsafeCell;
use ntex_bytes::{BytePageSize, BytesMut, buf::BufMut};
use ntex_service::cfg::{CfgContext, Configuration};
use ntex_util::{time::Millis, time::Seconds};
const DEFAULT_CACHE_SIZE: usize = 128;
const DEFAULT_HIGH: usize = 16 * 1024 - 24;
const DEFAULT_LOW: usize = 512 + 24;
const DEFAULT_HALF: usize = (16 * 1024 - 24) / 2;
thread_local! {
static CACHE: LocalCache = LocalCache::new();
}
#[derive(Debug)]
/// Base io configuration
pub struct IoConfig {
connect_timeout: Millis,
keepalive_timeout: Seconds,
disconnect_timeout: Seconds,
frame_read_rate: Option<FrameReadRate>,
// io read/write cache and params
read_buf: BufConfig,
write_buf: BufConfig,
write_page_size: BytePageSize,
write_buf_limit: usize,
// shared config
pub(crate) config: CfgContext,
}
impl Default for IoConfig {
fn default() -> Self {
IoConfig::new()
}
}
impl Configuration for IoConfig {
const NAME: &str = "IO Configuration";
fn ctx(&self) -> &CfgContext {
&self.config
}
fn set_ctx(&mut self, ctx: CfgContext) {
self.read_buf.idx = ctx.id();
self.write_buf.idx = ctx.id();
self.config = ctx;
}
}
#[derive(Copy, Clone, Debug)]
pub struct FrameReadRate {
pub timeout: Seconds,
pub max_timeout: Seconds,
pub rate: u32,
}
#[derive(Copy, Clone, Debug)]
pub struct BufConfig {
pub high: usize,
pub low: usize,
pub half: usize,
idx: usize,
first: bool,
cache_size: usize,
}
impl IoConfig {
#[inline]
#[must_use]
/// Create new config object
pub fn new() -> IoConfig {
let config = CfgContext::default();
let idx = config.id();
IoConfig {
config,
connect_timeout: Millis::ZERO,
keepalive_timeout: Seconds(0),
disconnect_timeout: Seconds(1),
frame_read_rate: None,
read_buf: BufConfig {
idx,
high: DEFAULT_HIGH,
low: DEFAULT_LOW,
half: DEFAULT_HALF,
first: true,
cache_size: DEFAULT_CACHE_SIZE,
},
write_buf: BufConfig {
idx,
high: DEFAULT_HIGH,
low: DEFAULT_LOW,
half: DEFAULT_HALF,
first: false,
cache_size: DEFAULT_CACHE_SIZE,
},
write_page_size: BytePageSize::Size16,
write_buf_limit: BytePageSize::Size16.half_capacity(),
}
}
#[inline]
/// Get tag
pub fn tag(&self) -> &str {
self.config.tag()
}
#[inline]
/// Get connect timeout
pub fn connect_timeout(&self) -> Millis {
self.connect_timeout
}
#[inline]
/// Get keep-alive timeout
pub fn keepalive_timeout(&self) -> Seconds {
self.keepalive_timeout
}
#[inline]
/// Get disconnect timeout
pub fn disconnect_timeout(&self) -> Seconds {
self.disconnect_timeout
}
#[inline]
/// Get frame read params
pub fn frame_read_rate(&self) -> Option<&FrameReadRate> {
self.frame_read_rate.as_ref()
}
#[inline]
/// Get read buffer parameters
pub fn read_buf(&self) -> &BufConfig {
&self.read_buf
}
#[inline]
/// Get write buffer parameters
pub fn write_buf(&self) -> &BufConfig {
&self.write_buf
}
#[inline]
/// Get write page size
pub fn write_page_size(&self) -> BytePageSize {
self.write_page_size
}
#[inline]
/// The write buffer limit.
pub fn write_buf_limit(&self) -> usize {
self.write_buf_limit
}
/// Set connect timeout in seconds.
///
/// To disable timeout set value to 0.
///
/// By default connect timeout is disabled.
#[must_use]
pub fn set_connect_timeout<T: Into<Millis>>(mut self, timeout: T) -> Self {
self.connect_timeout = timeout.into();
self
}
/// Set keep-alive timeout in seconds.
///
/// To disable timeout set value to 0.
///
/// By default keep-alive timeout is disabled.
#[must_use]
pub fn set_keepalive_timeout<T: Into<Seconds>>(mut self, timeout: T) -> Self {
self.keepalive_timeout = timeout.into();
self
}
/// Set connection disconnect timeout.
///
/// Defines a timeout for disconnect connection. If a disconnect procedure does not complete
/// within this time, the connection get dropped.
///
/// To disable timeout set value to 0.
///
/// By default disconnect timeout is set to 1 seconds.
#[must_use]
pub fn set_disconnect_timeout<T: Into<Seconds>>(mut self, timeout: T) -> Self {
self.disconnect_timeout = timeout.into();
self
}
/// Set read rate parameters for single frame.
///
/// Set read timeout, max timeout and rate for reading payload. If the client
/// sends `rate` amount of data within `timeout` period of time, extend timeout by `timeout` seconds.
/// But no more than `max_timeout` timeout.
///
/// By default frame read rate is disabled.
#[must_use]
pub fn set_frame_read_rate(
mut self,
timeout: Seconds,
max_timeout: Seconds,
rate: u32,
) -> Self {
self.frame_read_rate = Some(FrameReadRate {
timeout,
max_timeout,
rate,
});
self
}
/// Set read buffer parameters.
///
/// By default high watermark is set to 16Kb, low watermark 1kb.
#[must_use]
pub fn set_read_buf(
mut self,
high_watermark: usize,
low_watermark: usize,
cache_size: usize,
) -> Self {
self.read_buf.cache_size = cache_size;
self.read_buf.high = high_watermark;
self.read_buf.low = low_watermark;
self.read_buf.half = high_watermark >> 1;
self
}
/// Set write buffer page size.
///
/// By default page size is set to 16kb.
#[must_use]
pub fn set_write_page_size(mut self, size: BytePageSize) -> Self {
self.write_page_size = size;
self
}
/// Sets the write buffer limit.
///
/// The app encodes data in response to incoming data,
/// continuing to fill the write buffer until all data
/// has been processed. Only then can the runtime wake
/// the write task to send the buffered data.
///
/// By that time, the buffer may have accumulated a large
/// amount of data, causing it to be sent in large bursts,
/// which introduces latency. To prevent this behavior and
/// flatten data delivery to the peer, ntex's io can initiate
/// out-of-order writes based on a configured threshold.
#[must_use]
pub fn set_write_buf_limit(mut self, size: usize) -> Self {
self.write_buf_limit = size;
self
}
/// Set write buffer parameters.
///
/// By default high watermark is set to 16Kb, low watermark 1kb.
#[must_use]
pub fn set_write_buf(
mut self,
high_watermark: usize,
low_watermark: usize,
cache_size: usize,
) -> Self {
self.write_buf.cache_size = cache_size;
self.write_buf.high = high_watermark;
self.write_buf.low = low_watermark;
self.write_buf.half = high_watermark >> 1;
self
}
}
impl BufConfig {
#[inline]
/// Get buffer
pub fn get(&self) -> BytesMut {
if let Some(mut buf) =
CACHE.with(|c| c.with(self.idx, self.first, |c: &mut Vec<_>| c.pop()))
{
buf.clear();
buf
} else {
BytesMut::with_capacity(self.high)
}
}
/// Get buffer with capacity
pub fn buf_with_capacity(&self, cap: usize) -> BytesMut {
BytesMut::with_capacity(cap)
}
#[inline]
/// Resize buffer
pub fn resize(&self, buf: &mut BytesMut) {
if buf.remaining_mut() < self.low {
self.resize_min(buf, self.high);
}
}
#[inline]
/// Resize buffer
pub fn resize_min(&self, buf: &mut BytesMut, size: usize) {
let mut avail = buf.remaining_mut();
if avail < size {
let mut new_cap = buf.capacity();
while avail < size {
avail += self.high;
new_cap += self.high;
}
buf.reserve_capacity(new_cap);
}
}
#[inline]
/// Release buffer, buf must be allocated from this pool
pub fn release(&self, buf: BytesMut) {
let cap = buf.capacity();
if cap > self.low && cap <= self.high {
CACHE.with(|c| {
c.with(self.idx, self.first, |v: &mut Vec<_>| {
if v.len() < self.cache_size {
v.push(buf);
}
});
});
}
}
}
struct LocalCache {
cache: UnsafeCell<Vec<(Vec<BytesMut>, Vec<BytesMut>)>>,
}
impl LocalCache {
fn new() -> Self {
Self {
cache: UnsafeCell::new(Vec::with_capacity(16)),
}
}
fn with<F, R>(&self, idx: usize, first: bool, f: F) -> R
where
F: FnOnce(&mut Vec<BytesMut>) -> R,
{
let cache = unsafe { &mut *self.cache.get() };
while cache.len() <= idx {
cache.push((Vec::new(), Vec::new()));
}
if first {
f(&mut cache[idx].0)
} else {
f(&mut cache[idx].1)
}
}
}