Skip to content

Commit

Permalink
was/{Glue,MGlue}: merge common code into class WasStockRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Aug 25, 2024
1 parent f7baf50 commit a92ac7b
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 264 deletions.
135 changes: 3 additions & 132 deletions src/was/Glue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,17 @@

#include "Glue.hxx"
#include "Stock.hxx"
#include "SConnection.hxx"
#include "SLease.hxx"
#include "Client.hxx"
#include "SRequest.hxx"
#include "was/async/Socket.hxx"
#include "http/PendingRequest.hxx"
#include "http/ResponseHandler.hxx"
#include "stock/GetHandler.hxx"
#include "stock/Stock.hxx"
#include "stock/Item.hxx"
#include "pool/pool.hxx"
#include "pool/LeakDetector.hxx"
#include "stopwatch.hxx"
#include "util/Cancellable.hxx"
#include "util/StringCompare.hxx"

#include <assert.h>
#include <string.h>

class WasRequest final : StockGetHandler, Cancellable, HttpResponseHandler, PoolLeakDetector {
struct pool &pool;

StopwatchPtr stopwatch;

const char *const site_name;

const char *const remote_host;

PendingHttpRequest pending_request;
const char *script_name;
const char *path_info;
const char *query_string;

std::span<const char *const> parameters;

WasMetricsHandler *const metrics_handler;
HttpResponseHandler &handler;
CancellablePointer cancel_ptr;

class WasRequest final : WasStockRequest {
public:
WasRequest(struct pool &_pool,
StopwatchPtr &&_stopwatch,
const char *_site_name,
const char *_remote_host,
HttpMethod _method, const char *_uri,
const char *_script_name, const char *_path_info,
const char *_query_string,
StringMap &&_headers,
UnusedIstreamPtr _body,
std::span<const char *const> _parameters,
WasMetricsHandler *_metrics_handler,
HttpResponseHandler &_handler)
:PoolLeakDetector(_pool),
pool(_pool),
stopwatch(std::move(_stopwatch)),
site_name(_site_name),
remote_host(_remote_host),
pending_request(_pool, _method, _uri,
std::move(_headers), std::move(_body)),
script_name(_script_name),
path_info(_path_info), query_string(_query_string),
parameters(_parameters),
metrics_handler(_metrics_handler),
handler(_handler) {}

void Destroy() noexcept {
DeleteFromPool(pool, this);
}
using WasStockRequest::WasStockRequest;

void Start(WasStock &was_stock, const ChildOptions &options,
const char *action, std::span<const char *const> args,
Expand All @@ -84,81 +28,8 @@ class WasRequest final : StockGetHandler, Cancellable, HttpResponseHandler, Pool
parallelism, disposable,
*this, cancel_ptr);
}

private:
/* virtual methods from class StockGetHandler */
void OnStockItemReady(StockItem &item) noexcept override;
void OnStockItemError(std::exception_ptr ep) noexcept override;

/* virtual methods from class HttpResponseHandler */
void OnHttpResponse(HttpStatus status, StringMap &&headers,
UnusedIstreamPtr body) noexcept override;
void OnHttpError(std::exception_ptr error) noexcept override;

/* virtual methods from class Cancellable */
void Cancel() noexcept override {
auto c = std::move(cancel_ptr);
Destroy();
c.Cancel();
}
};

/*
* stock callback
*
*/

void
WasRequest::OnStockItemReady(StockItem &item) noexcept
{
auto &connection = static_cast<WasStockConnection &>(item);
connection.SetSite(site_name);
connection.SetUri(pending_request.uri);

const auto &process = connection.GetSocket();
auto &lease = *NewFromPool<WasStockLease>(pool, connection);

was_client_request(pool, item.GetStock().GetEventLoop(),
std::move(stopwatch),
process.control,
process.input, process.output,
lease,
remote_host,
pending_request.method, pending_request.uri,
script_name, path_info,
query_string,
pending_request.headers,
std::move(pending_request.body),
parameters,
metrics_handler,
*this, cancel_ptr);
}

void
WasRequest::OnStockItemError(std::exception_ptr ep) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeError(ep);
}

void
WasRequest::OnHttpResponse(HttpStatus status, StringMap &&_headers,
UnusedIstreamPtr _body) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeResponse(status, std::move(_headers), std::move(_body));
}

void
WasRequest::OnHttpError(std::exception_ptr error) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeError(std::move(error));
}

/*
* constructor
*
Expand Down
135 changes: 3 additions & 132 deletions src/was/MGlue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,21 @@
#include "MGlue.hxx"
#include "MStock.hxx"
#include "RStock.hxx"
#include "SConnection.hxx"
#include "SLease.hxx"
#include "Client.hxx"
#include "SRequest.hxx"
#include "was/async/Socket.hxx"
#include "http/PendingRequest.hxx"
#include "http/ResponseHandler.hxx"
#include "stock/GetHandler.hxx"
#include "stock/Stock.hxx"
#include "stock/Item.hxx"
#include "pool/pool.hxx"
#include "pool/LeakDetector.hxx"
#include "stopwatch.hxx"
#include "net/SocketAddress.hxx"
#include "net/FormatAddress.hxx"
#include "util/Cancellable.hxx"
#include "util/StringCompare.hxx"
#include "AllocatorPtr.hxx"

#include <cassert>

class MultiWasRequest final
: StockGetHandler, Cancellable, HttpResponseHandler, PoolLeakDetector
class MultiWasRequest final : WasStockRequest
{
struct pool &pool;

StopwatchPtr stopwatch;

const char *const site_name;

const char *const remote_host;

PendingHttpRequest pending_request;
const char *script_name;
const char *path_info;
const char *query_string;

std::span<const char *const> parameters;

WasMetricsHandler *const metrics_handler;
HttpResponseHandler &handler;
CancellablePointer cancel_ptr;

public:
MultiWasRequest(struct pool &_pool,
StopwatchPtr &&_stopwatch,
const char *_site_name,
const char *_remote_host,
HttpMethod _method, const char *_uri,
const char *_script_name, const char *_path_info,
const char *_query_string,
StringMap &&_headers,
UnusedIstreamPtr _body,
std::span<const char *const> _parameters,
WasMetricsHandler *_metrics_handler,
HttpResponseHandler &_handler) noexcept
:PoolLeakDetector(_pool),
pool(_pool),
stopwatch(std::move(_stopwatch)),
site_name(_site_name),
remote_host(_remote_host),
pending_request(_pool, _method, _uri,
std::move(_headers), std::move(_body)),
script_name(_script_name),
path_info(_path_info), query_string(_query_string),
parameters(_parameters),
metrics_handler(_metrics_handler),
handler(_handler) {}

void Destroy() noexcept {
this->~MultiWasRequest();
}
using WasStockRequest::WasStockRequest;

void Start(MultiWasStock &stock, const ChildOptions &options,
const char *action, std::span<const char *const> args,
Expand All @@ -96,81 +40,8 @@ class MultiWasRequest final
stock.Get(pool, address, parallelism, concurrency,
*this, cancel_ptr);
}

private:
/* virtual methods from class StockGetHandler */
void OnStockItemReady(StockItem &item) noexcept override;
void OnStockItemError(std::exception_ptr ep) noexcept override;

/* virtual methods from class HttpResponseHandler */
void OnHttpResponse(HttpStatus status, StringMap &&headers,
UnusedIstreamPtr body) noexcept override;
void OnHttpError(std::exception_ptr error) noexcept override;

/* virtual methods from class Cancellable */
void Cancel() noexcept override {
auto c = std::move(cancel_ptr);
Destroy();
c.Cancel();
}
};

/*
* stock callback
*
*/

void
MultiWasRequest::OnStockItemReady(StockItem &item) noexcept
{
auto &connection = static_cast<WasStockConnection &>(item);
connection.SetSite(site_name);
connection.SetUri(pending_request.uri);

const auto &socket = connection.GetSocket();
auto &lease = *NewFromPool<WasStockLease>(pool, connection);

was_client_request(pool, item.GetStock().GetEventLoop(),
std::move(stopwatch),
socket.control,
socket.input, socket.output,
lease,
remote_host,
pending_request.method, pending_request.uri,
script_name, path_info,
query_string,
pending_request.headers,
std::move(pending_request.body),
parameters,
metrics_handler,
*this, cancel_ptr);
}

void
MultiWasRequest::OnStockItemError(std::exception_ptr ep) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeError(ep);
}

void
MultiWasRequest::OnHttpResponse(HttpStatus status, StringMap &&_headers,
UnusedIstreamPtr _body) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeResponse(status, std::move(_headers), std::move(_body));
}

void
MultiWasRequest::OnHttpError(std::exception_ptr error) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeError(std::move(error));
}

/*
* constructor
*
Expand Down
76 changes: 76 additions & 0 deletions src/was/SRequest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: BSD-2-Clause
// Copyright CM4all GmbH
// author: Max Kellermann <[email protected]>

#include "SRequest.hxx"
#include "SConnection.hxx"
#include "SLease.hxx"
#include "Client.hxx"
#include "pool/pool.hxx"
#include "stock/Item.hxx"
#include "stock/Stock.hxx"

inline void
WasStockRequest::Destroy() noexcept
{
DeleteFromPool(pool, this);
}

void
WasStockRequest::OnStockItemReady(StockItem &item) noexcept
{
auto &connection = static_cast<WasStockConnection &>(item);
connection.SetSite(site_name);
connection.SetUri(pending_request.uri);

const auto &process = connection.GetSocket();
auto &lease = *NewFromPool<WasStockLease>(pool, connection);

was_client_request(pool, item.GetStock().GetEventLoop(),
std::move(stopwatch),
process.control,
process.input, process.output,
lease,
remote_host,
pending_request.method, pending_request.uri,
script_name, path_info,
query_string,
pending_request.headers,
std::move(pending_request.body),
parameters,
metrics_handler,
*this, cancel_ptr);
}

void
WasStockRequest::OnStockItemError(std::exception_ptr ep) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeError(ep);
}

void
WasStockRequest::OnHttpResponse(HttpStatus status, StringMap &&_headers,
UnusedIstreamPtr _body) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeResponse(status, std::move(_headers), std::move(_body));
}

void
WasStockRequest::OnHttpError(std::exception_ptr error) noexcept
{
auto &_handler = handler;
Destroy();
_handler.InvokeError(std::move(error));
}

void
WasStockRequest::Cancel() noexcept
{
auto c = std::move(cancel_ptr);
Destroy();
c.Cancel();
}
Loading

0 comments on commit a92ac7b

Please sign in to comment.