mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-24 10:30:38 +00:00
The json parser runs in a static thread which accepts and parses documents. Documents smaller than a parsing threshold (currently: 16KiB) will be parsed in place without yielding. The assumption is that most alternator requests are small and there's no need to parse them in a yieldable way, which also induces overhead. For reference, parsing a 128KiB document made of many small objects with rapidjson takes around 0.5 millisecond, and a 16KiB document is parsed in around 0.06ms - a value small enough not to disturb Seastar's current value of 0.5ms task quota too much.
80 lines
2.7 KiB
C++
80 lines
2.7 KiB
C++
/*
|
|
* Copyright 2019 ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* This file is part of Scylla.
|
|
*
|
|
* Scylla is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Scylla is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "alternator/executor.hh"
|
|
#include <seastar/core/future.hh>
|
|
#include <seastar/http/httpd.hh>
|
|
#include <seastar/net/tls.hh>
|
|
#include <optional>
|
|
#include <alternator/auth.hh>
|
|
#include <utils/small_vector.hh>
|
|
#include <seastar/core/units.hh>
|
|
|
|
namespace alternator {
|
|
|
|
class server {
|
|
static constexpr size_t content_length_limit = 16*MB;
|
|
using alternator_callback = std::function<future<executor::request_return_type>(executor&, executor::client_state&, tracing::trace_state_ptr, rjson::value, std::unique_ptr<request>)>;
|
|
using alternator_callbacks_map = std::unordered_map<std::string_view, alternator_callback>;
|
|
|
|
http_server _http_server;
|
|
http_server _https_server;
|
|
executor& _executor;
|
|
|
|
key_cache _key_cache;
|
|
bool _enforce_authorization;
|
|
utils::small_vector<std::reference_wrapper<seastar::httpd::http_server>, 2> _enabled_servers;
|
|
gate _pending_requests;
|
|
alternator_callbacks_map _callbacks;
|
|
|
|
class json_parser {
|
|
static constexpr size_t yieldable_parsing_threshold = 16*KB;
|
|
std::string_view _raw_document;
|
|
rjson::value _parsed_document;
|
|
std::exception_ptr _current_exception;
|
|
semaphore _parsing_sem{1};
|
|
condition_variable _document_waiting;
|
|
condition_variable _document_parsed;
|
|
abort_source _as;
|
|
future<> _run_parse_json_thread;
|
|
public:
|
|
json_parser();
|
|
future<rjson::value> parse(std::string_view content);
|
|
future<> stop();
|
|
};
|
|
json_parser _json_parser;
|
|
|
|
public:
|
|
server(executor& executor);
|
|
|
|
future<> init(net::inet_address addr, std::optional<uint16_t> port, std::optional<uint16_t> https_port, std::optional<tls::credentials_builder> creds, bool enforce_authorization);
|
|
future<> stop();
|
|
private:
|
|
void set_routes(seastar::httpd::routes& r);
|
|
future<> verify_signature(const seastar::httpd::request& r);
|
|
future<executor::request_return_type> handle_api_request(std::unique_ptr<request>&& req);
|
|
};
|
|
|
|
}
|
|
|