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
//! Shared client send pipeline helpers.
//!
//! This module centralizes the common serialize, request-hook, framed-send,
//! timing, and error-hook flow used by the client's send APIs.
use std::time::Instant;
use futures::SinkExt;
use tracing::{Instrument, Span};
use super::{ClientError, WireframeClient, runtime::ClientStream, tracing_config::TracingConfig};
use crate::{message::EncodeWith, serializer::Serializer};
impl<S, T, C> WireframeClient<S, T, C>
where
S: Serializer + Send + Sync,
T: ClientStream,
{
/// Serialize a value, run outbound hooks, and send the resulting frame.
pub(crate) async fn serialize_and_send<M, F>(
&mut self,
value: &M,
timing_start: Option<Instant>,
span_for_frame: F,
) -> Result<(), ClientError>
where
M: EncodeWith<S>,
F: FnOnce(&TracingConfig, usize) -> Span,
{
let mut bytes = match self.serializer.serialize(value) {
Ok(bytes) => bytes,
Err(e) => {
let err = ClientError::Serialize(e);
super::tracing_helpers::emit_timing_event(timing_start);
self.invoke_error_hook(&err).await;
return Err(err);
}
};
// Count the materialization at the client boundary as well, because a
// client send does not pass through `encode_message_frame`. Hooks run
// afterwards, so a mutation enlarges the buffer without being counted
// as a serializer copy.
crate::benchmark_counters::record_serialized(bytes.len());
self.invoke_before_send_hooks(&mut bytes);
let span = span_for_frame(&self.tracing_config, bytes.len());
let send_result = async {
// Go through the counter's hand-off rather than `Bytes::from`, so
// the client path reports a relocation the same way the server's
// `encode_message_frame` does instead of bypassing the guard.
let result = self
.framed
.send(crate::benchmark_counters::bytes_from_vec(bytes))
.await;
super::tracing_helpers::emit_timing_event(timing_start);
result
}
.instrument(span)
.await;
if let Err(e) = send_result {
let err = ClientError::from(e);
self.invoke_error_hook(&err).await;
return Err(err);
}
Ok(())
}
}