Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
198 lines (172 loc) · 5.5 KB
/
Copy pathmod.rs
File metadata and controls
198 lines (172 loc) · 5.5 KB
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
use super::batch::{
err_event_too_large, Batch, BatchConfig, BatchError, BatchSettings, BatchSize, PushResult,
};
use flate2::write::GzEncoder;
use std::io::Write;
pub mod compression;
pub mod json;
pub mod loki;
pub mod metrics;
pub mod partition;
pub mod vec;
pub use compression::{Compression, GZIP_FAST};
pub use partition::{Partition, PartitionBuffer, PartitionInnerBuffer};
#[derive(Debug)]
pub struct Buffer {
inner: InnerBuffer,
num_items: usize,
num_bytes: usize,
settings: BatchSize<Self>,
compression: Compression,
}
#[derive(Debug)]
pub enum InnerBuffer {
Plain(Vec<u8>),
Gzip(GzEncoder<Vec<u8>>),
}
impl Buffer {
pub fn new(settings: BatchSize<Self>, compression: Compression) -> Self {
let buffer = Vec::with_capacity(settings.bytes);
let inner = match compression {
Compression::None => InnerBuffer::Plain(buffer),
Compression::Gzip(level) => {
let level = level.unwrap_or(GZIP_FAST);
InnerBuffer::Gzip(GzEncoder::new(
buffer,
flate2::Compression::new(level as u32),
))
}
};
Self {
inner,
num_items: 0,
num_bytes: 0,
settings,
compression,
}
}
pub fn push(&mut self, input: &[u8]) {
self.num_items += 1;
match &mut self.inner {
InnerBuffer::Plain(inner) => {
inner.extend_from_slice(input);
}
InnerBuffer::Gzip(inner) => {
inner.write_all(input).unwrap();
}
}
}
pub fn is_empty(&self) -> bool {
match &self.inner {
InnerBuffer::Plain(inner) => inner.is_empty(),
InnerBuffer::Gzip(inner) => inner.get_ref().is_empty(),
}
}
}
impl Batch for Buffer {
type Input = Vec<u8>;
type Output = Vec<u8>;
fn get_settings_defaults(
config: BatchConfig,
defaults: BatchSettings<Self>,
) -> Result<BatchSettings<Self>, BatchError> {
Ok(config
.use_size_as_bytes()?
.get_settings_or_default(defaults))
}
fn push(&mut self, item: Self::Input) -> PushResult<Self::Input> {
// The compressed encoders don't flush bytes immediately, so we
// can't track compressed sizes. Keep a running count of the
// number of bytes written instead.
let new_bytes = self.num_bytes + item.len();
if self.is_empty() && item.len() > self.settings.bytes {
err_event_too_large(item.len())
} else if self.num_items >= self.settings.events || new_bytes > self.settings.bytes {
PushResult::Overflow(item)
} else {
self.push(&item);
self.num_bytes = new_bytes;
PushResult::Ok(
self.num_items >= self.settings.events || new_bytes >= self.settings.bytes,
)
}
}
fn is_empty(&self) -> bool {
self.is_empty()
}
fn fresh(&self) -> Self {
Self::new(self.settings, self.compression)
}
fn finish(self) -> Self::Output {
match self.inner {
InnerBuffer::Plain(inner) => inner,
InnerBuffer::Gzip(inner) => inner
.finish()
.expect("This can't fail because the inner writer is a Vec"),
}
}
fn num_items(&self) -> usize {
self.num_items
}
}
#[cfg(test)]
mod test {
use super::{Buffer, Compression};
use crate::{
buffers::Acker,
sinks::util::{BatchSettings, BatchSink},
};
use futures::{future, stream, SinkExt, StreamExt};
use std::{
io::Read,
sync::{Arc, Mutex},
};
use tokio::time::Duration;
#[tokio::test]
async fn gzip() {
use flate2::read::GzDecoder;
let (acker, _) = Acker::new_for_testing();
let sent_requests = Arc::new(Mutex::new(Vec::new()));
let svc = tower::service_fn(|req| {
let sent_requests = Arc::clone(&sent_requests);
sent_requests.lock().unwrap().push(req);
future::ok::<_, std::io::Error>(())
});
let batch_size = BatchSettings::default().bytes(100_000).events(1_000).size;
let timeout = Duration::from_secs(0);
let buffered = BatchSink::new(
svc,
Buffer::new(batch_size, Compression::gzip_default()),
timeout,
acker,
);
let input = std::iter::repeat(
b"It's going down, I'm yelling timber, You better move, you better dance".to_vec(),
)
.take(100_000);
let _ = buffered
.sink_map_err(drop)
.send_all(&mut stream::iter(input).map(Ok))
.await
.unwrap();
let output = Arc::try_unwrap(sent_requests)
.unwrap()
.into_inner()
.unwrap();
let output = output.into_iter().collect::<Vec<Vec<u8>>>();
assert!(output.len() > 1);
assert!(dbg!(output.iter().map(|o| o.len()).sum::<usize>()) < 80_000);
let decompressed = output.into_iter().flat_map(|batch| {
let mut decompressed = vec![];
GzDecoder::new(batch.as_slice())
.read_to_end(&mut decompressed)
.unwrap();
decompressed
});
assert!(decompressed.eq(std::iter::repeat(
b"It's going down, I'm yelling timber, You better move, you better dance".to_vec()
)
.take(100_000)
.flatten()));
}
}