How It Works
The problem of efficient I/O interactions is solved transparently for the developers
std::size_t Ins(storages::postgres::Transaction& tr,
std::string_view key) {
auto res = tr.Execute("INSERT INTO keys VALUES ($1)", key);
return res.RowsAffected();
}
template <class OnSuccess>
void Ins(storages::postgres::Transaction& tr,
std::string_view key, OnSuccess&& on_success) {
tr.Execute("INSERT INTO keys VALUES ($1)", key,
[on_success = std::forward<OnSuccess>(on_success)]
(const auto& res, const auto& error) {
if (error) {
report_error(error);
return;
}
on_success(res.RowsAffected());
}
);
}
Values of userver
Technologies for debugging and memory profiling a running production service
Write your first toy C++ service, evolve it into a production ready service.
Efficient asynchronous drivers for databases (MongoDB, PostgreSQL, MySQL/MariaDB, Redis, ClickHouse, YDB, ...) and data transfer protocols (HTTP, WEbSockets, gRPC, TCP, AMQP-0.9.1, Apache Kafka, ...), tasks construction and cancellation.
Functionality to change the service configuration on-the-fly. Adjust options of the deadline propagation, timeouts, congestion-control without a restart.
Rich set of high-level components for caches, tasks, distributed locking, logging, tracing, statistics, metrics, JSON/YAML/BSON.
Comprehensive set of asynchronous low-level synchronization primitives and OS abstractions.
Service Example
Simple π userver service that handles HTTP requests to "/kv" URL and responds with a key from database
#include <userver/easy.hpp>
#include "schemas/key_value.hpp"
int main(int argc, char* argv[]) {
using namespace userver;
easy::HttpWith<easy::PgDep>(argc, argv)
// Handles multiple HTTP requests to `/kv` URL concurrently
.Get("/kv", [](formats::json::Value request_json, const easy::PgDep& dep) {
// JSON parser and serializer are generated from JSON schema by userver
auto key = request_json.As<schemas::KeyRequest>().key;
// Asynchronous execution of the SQL query in transaction. Current thread
// handles other requests while the response from the DB is being received:
auto res = dep.pg().Execute(
storages::postgres::ClusterHostType::kSlave,
// Query is converted into a prepared statement. Subsequent requests
// send only parameters in a binary form and meta information is
// discarded on the DB side, significantly saving network bandwith.
"SELECT value FROM key_value_table WHERE key=$1", key
);
schemas::KeyValue response{key, res[0][0].As<std::string>()};
return formats::json::ValueBuilder{response}.ExtractValue();
});
}
Brands and companies using userver
Leave Your Feedback
Your opinion will help to improve our service