pub(crate) const FILE_CHUNK_SIZE: usize = 256 << 10;
pub(crate) const MIN_MESSAGE_SIZE: usize = FILE_CHUNK_SIZE + (4 << 10);
#[cfg(feature = "protocol-grpc")]
const _: () = assert!(FILE_CHUNK_SIZE == surrealdb_protocol::DEFAULT_FILE_CHUNK_SIZE);
pub(crate) const MAX_MESSAGE_SIZE: usize = u32::MAX as usize;
#[derive(Debug, Clone, Default)]
pub struct GrpcConfig {
pub(crate) max_message_size: Option<usize>,
}
impl GrpcConfig {
pub fn new() -> Self {
Default::default()
}
pub fn max_message_size(mut self, max_message_size: usize) -> Self {
self.max_message_size = Some(max_message_size);
self
}
}
#[cfg(test)]
mod tests {
use super::{FILE_CHUNK_SIZE, GrpcConfig, MAX_MESSAGE_SIZE, MIN_MESSAGE_SIZE};
use crate::opt::Config;
#[test]
fn a_message_size_too_small_to_transfer_a_file_is_refused() {
for size in [1, 64 << 10, FILE_CHUNK_SIZE, MIN_MESSAGE_SIZE - 1] {
let refused = Config::new()
.grpc(GrpcConfig::new().max_message_size(size))
.expect_err("below the floor");
assert!(
refused.is_configuration(),
"expected a configuration error for {size}, got {refused:?}"
);
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn a_message_size_past_the_frame_width_is_refused() {
for size in [MAX_MESSAGE_SIZE + 1, 8 << 30] {
let refused = Config::new()
.grpc(GrpcConfig::new().max_message_size(size))
.expect_err("above the frame width");
assert!(
refused.is_configuration(),
"expected a configuration error for {size}, got {refused:?}"
);
}
}
#[test]
fn a_workable_message_size_is_accepted() {
for size in [MIN_MESSAGE_SIZE, 128 << 20, MAX_MESSAGE_SIZE] {
Config::new()
.grpc(GrpcConfig::new().max_message_size(size))
.unwrap_or_else(|error| panic!("{size} should be accepted: {error:?}"));
}
Config::new().grpc(GrpcConfig::new()).expect("an unset size is the default");
}
}