Configurable read buffer size

This commit is contained in:
2025-08-19 12:20:56 -04:00
parent 2b458747f8
commit 91dcf04635
4 changed files with 86 additions and 10 deletions

View File

@@ -161,13 +161,12 @@ struct Connection {
// response
bool closeConnection{false};
bool readBytes(size_t max_request_size) {
// Use smaller buffer size but respect max request size
// TODO revisit
size_t buf_size = std::min(size_t(4096), max_request_size);
std::vector<char> buf(buf_size);
bool readBytes(size_t max_request_size, size_t buffer_size) {
// Use Variable Length Array for optimal stack allocation
char buf[buffer_size];
for (;;) {
int r = read(fd, buf.data(), buf.size());
int r = read(fd, buf, buffer_size);
if (r == -1) {
if (errno == EINTR) {
continue;
@@ -183,7 +182,7 @@ struct Connection {
}
// "pump parser"
// TODO revisit
appendMessage({buf.data(), size_t(r)});
appendMessage({buf, size_t(r)});
}
}
@@ -261,11 +260,53 @@ struct Connection {
#endif
};
void print_help(const char *program_name) {
std::cout << "WeaselDB - High-performance write-side database component\n\n";
std::cout << "Usage: " << program_name << " [OPTIONS]\n\n";
std::cout << "Options:\n";
std::cout << " --config <file> Path to TOML configuration file (default: "
"config.toml)\n";
std::cout << " --help Show this help message and exit\n";
std::cout << " -h Show this help message and exit\n\n";
std::cout << "Examples:\n";
std::cout << " " << program_name
<< " # Use default config.toml\n";
std::cout << " " << program_name
<< " --config my.toml # Use custom config file\n";
std::cout << " " << program_name
<< " --help # Show this help\n\n";
std::cout << "For configuration documentation, see config.md\n";
}
int main(int argc, char *argv[]) {
std::string config_file = "config.toml";
if (argc > 1) {
config_file = argv[1];
// Parse command line arguments
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
print_help(argv[0]);
return 0;
} else if (arg == "--config") {
if (i + 1 >= argc) {
std::cerr << "Error: --config requires a filename argument\n";
print_help(argv[0]);
return 1;
}
config_file = argv[++i];
} else if (arg.starts_with("--config=")) {
config_file = arg.substr(9);
} else {
// For backward compatibility, treat lone argument as config file
if (argc == 2) {
config_file = arg;
} else {
std::cerr << "Error: Unknown argument '" << arg << "'\n";
print_help(argv[0]);
return 1;
}
}
}
auto config = weaseldb::ConfigParser::load_from_file(config_file);
@@ -289,6 +330,8 @@ int main(int argc, char *argv[]) {
<< std::endl;
std::cout << "Max connections: " << config->server.max_connections
<< std::endl;
std::cout << "Read buffer size: " << config->server.read_buffer_size
<< " bytes" << std::endl;
std::cout << "Min request ID length: " << config->commit.min_request_id_length
<< std::endl;
std::cout << "Request ID retention: "
@@ -345,6 +388,7 @@ int main(int argc, char *argv[]) {
threads.emplace_back(
[network_epollfd, networkThreadId,
max_request_size = config->server.max_request_size_bytes,
read_buffer_size = config->server.read_buffer_size,
event_batch_size = config->server.event_batch_size]() {
pthread_setname_np(
pthread_self(),
@@ -388,7 +432,7 @@ int main(int argc, char *argv[]) {
(events[i].events & EPOLLOUT)));
if (events[i].events & EPOLLIN) {
bool done = conn->readBytes(max_request_size);
bool done = conn->readBytes(max_request_size, read_buffer_size);
if (done) {
continue;
}