Skip to content

Commit

Permalink
cgi/Address: add method GetChildId(), move from CgiChildParams::GetSt…
Browse files Browse the repository at this point in the history
…ockKey()

Prepare for caching the stock keys.
  • Loading branch information
MaxKellermann committed Jan 23, 2025
1 parent e091a9e commit eab94e4
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 63 deletions.
32 changes: 32 additions & 0 deletions src/cgi/Address.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@ CgiAddress::GetURI(AllocatorPtr alloc) const noexcept
return alloc.Concat(sn, pi, qm, qs);
}

StringWithHash
CgiAddress::GetChildId(AllocatorPtr alloc) const noexcept
{
std::size_t hash = options.GetHash();

PoolStringBuilder<256> b;

{
const std::string_view value{action != nullptr ? action : path};
b.push_back(value);
hash = djb_hash(AsBytes(value), hash);
}

for (std::string_view i : args) {
b.push_back("!");
b.push_back(i);
hash = djb_hash(AsBytes(i), hash);
}

for (std::string_view i : options.env) {
b.push_back("$");
b.push_back(i);
hash = djb_hash(AsBytes(i), hash);
}

char options_buffer[16384];
b.emplace_back(options_buffer,
options.MakeId(options_buffer));

return StringWithHash{b.MakeView(alloc), hash};
}

StringWithHash
CgiAddress::GetId(AllocatorPtr alloc) const noexcept
{
Expand Down
8 changes: 8 additions & 0 deletions src/cgi/Address.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ struct CgiAddress {
return path_info;
}

/**
* Generates a string identifying the child process. This can
* be used as a key in a hash table. The string will be
* allocated by the specified pool.
*/
[[gnu::pure]]
StringWithHash GetChildId(AllocatorPtr alloc) const noexcept;

/**
* Generates a string identifying the address. This can be used as a
* key in a hash table. The string will be allocated by the specified
Expand Down
35 changes: 0 additions & 35 deletions src/cgi/ChildParams.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,3 @@ CgiChildParams::CgiChildParams(AllocatorPtr alloc, const CgiChildParams &src) no
disposable(src.disposable)
{
}

inline std::size_t
CgiChildParams::GetStockHash() const noexcept
{
std::size_t hash = options.GetHash();
hash = djb_hash(AsBytes(executable_path), hash);

for (const char *i : args)
hash = djb_hash_string(i, hash);

return hash;
}

StockKey
CgiChildParams::GetStockKey(AllocatorPtr alloc) const noexcept
{
PoolStringBuilder<256> b;
b.push_back(executable_path);

for (auto i : args) {
b.push_back(" ");
b.push_back(i);
}

for (auto i : options.env) {
b.push_back("$");
b.push_back(i);
}

char options_buffer[16384];
b.emplace_back(options_buffer,
options.MakeId(options_buffer));

return StockKey{b.MakeView(alloc), GetStockHash()};
}
12 changes: 0 additions & 12 deletions src/cgi/ChildParams.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,4 @@ struct CgiChildParams {
disposable(_disposable) {}

CgiChildParams(AllocatorPtr alloc, const CgiChildParams &src) noexcept;

/**
* Generates a string identifying the process. This can be
* used as a key in a hash table. The string will be
* allocated by the specified pool.
*/
[[gnu::pure]]
StringWithHash GetStockKey(AllocatorPtr alloc) const noexcept;

private:
[[gnu::pure]]
std::size_t GetStockHash() const noexcept;
};
6 changes: 5 additions & 1 deletion src/fcgi/Request.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "stock/Item.hxx"
#include "istream/UnusedPtr.hxx"
#include "pool/pool.hxx"
#include "pool/tpool.hxx"
#include "pool/LeakDetector.hxx"
#include "event/FineTimerEvent.hxx"
#include "net/SocketDescriptor.hxx"
Expand Down Expand Up @@ -91,7 +92,10 @@ class FcgiRequest final
}

void BeginConnect() noexcept {
fcgi_stock.Get(address.options,
const TempPoolLease tpool;
const auto key = address.GetChildId(*tpool);

fcgi_stock.Get(key, address.options,
action, address.args.ToArray(pool),
address.parallelism, address.concurrency,
*this, cancel_ptr);
Expand Down
6 changes: 1 addition & 5 deletions src/fcgi/Stock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "spawn/ChildOptions.hxx"
#include "pool/DisposablePointer.hxx"
#include "pool/WithPoolDisposablePointer.hxx"
#include "pool/tpool.hxx"
#include "lib/fmt/ToBuffer.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "io/FdHolder.hxx"
Expand Down Expand Up @@ -204,7 +203,7 @@ FcgiStock::FadeTag(std::string_view tag) noexcept
}

void
FcgiStock::Get(const ChildOptions &options,
FcgiStock::Get(StockKey key, const ChildOptions &options,
const char *executable_path,
std::span<const char *const> args,
unsigned parallelism, unsigned concurrency,
Expand All @@ -215,12 +214,9 @@ FcgiStock::Get(const ChildOptions &options,
/* no concurrency by default */
concurrency = 1;

const TempPoolLease tpool;

auto r = ToDeletePointer(new CgiChildParams(executable_path,
args, options,
parallelism, concurrency, false));
const auto key = r->GetStockKey(*tpool);
mchild_stock.Get(key, std::move(r),
concurrency,
handler, cancel_ptr);
Expand Down
2 changes: 1 addition & 1 deletion src/fcgi/Stock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public:
/**
* @param args command-line arguments
*/
void Get(const ChildOptions &options,
void Get(StockKey key, const ChildOptions &options,
const char *executable_path,
std::span<const char *const> args,
unsigned parallelism, unsigned concurrency,
Expand Down
6 changes: 5 additions & 1 deletion src/was/Glue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "SRequest.hxx"
#include "cgi/Address.hxx"
#include "pool/pool.hxx"
#include "pool/tpool.hxx"
#include "util/StringCompare.hxx"
#include "AllocatorPtr.hxx"

Expand Down Expand Up @@ -50,7 +51,10 @@ class WasRequest final : WasStockRequest {

protected:
void GetStockItem() noexcept override {
was_stock.Get(pool,
const TempPoolLease tpool;
const auto key = address.GetChildId(*tpool);

was_stock.Get(pool, key,
address.options,
action, args,
address.parallelism, address.disposable,
Expand Down
1 change: 1 addition & 0 deletions src/was/Glue.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class HttpMethod : uint_least8_t;
struct pool;
class StopwatchPtr;
struct CgiAddress;
struct StringWithHash;
class UnusedIstreamPtr;
class WasStock;
class WasMetricsHandler;
Expand Down
6 changes: 5 additions & 1 deletion src/was/MGlue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "SRequest.hxx"
#include "was/async/Socket.hxx"
#include "pool/pool.hxx"
#include "pool/tpool.hxx"
#include "stopwatch.hxx"
#include "cgi/Address.hxx"
#include "net/FormatAddress.hxx"
Expand Down Expand Up @@ -53,7 +54,10 @@ class MultiWasRequest final : WasStockRequest

protected:
void GetStockItem() noexcept override {
stock.Get(pool,
const TempPoolLease tpool;
const auto key = address.GetChildId(*tpool);

stock.Get(pool, key,
address.options,
action, args,
address.parallelism, address.concurrency,
Expand Down
5 changes: 1 addition & 4 deletions src/was/MStock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "stock/MapStock.hxx"
#include "pool/DisposablePointer.hxx"
#include "pool/WithPoolDisposablePointer.hxx"
#include "pool/tpool.hxx"
#include "AllocatorPtr.hxx"
#include "cgi/ChildParams.hxx"
#include "spawn/ChildStockItem.hxx"
Expand Down Expand Up @@ -211,20 +210,18 @@ MultiWasStock::FadeTag(std::string_view tag) noexcept

void
MultiWasStock::Get(AllocatorPtr alloc,
StockKey key,
const ChildOptions &options,
const char *executable_path,
std::span<const char *const> args,
unsigned parallelism, unsigned concurrency,
StockGetHandler &handler,
CancellablePointer &cancel_ptr) noexcept
{
const TempPoolLease tpool;

auto r = NewDisposablePointer<CgiChildParams>(alloc, executable_path,
args, options,
parallelism, concurrency,
false);
const auto key = r->GetStockKey(*tpool);

mchild_stock.Get(key, std::move(r), concurrency, handler, cancel_ptr);
}
1 change: 1 addition & 0 deletions src/was/MStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public:
* instance.
*/
void Get(AllocatorPtr alloc,
StockKey key,
const ChildOptions &options,
const char *executable_path,
std::span<const char *const> args,
Expand Down
4 changes: 1 addition & 3 deletions src/was/Stock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "spawn/ExitListener.hxx"
#include "spawn/Interface.hxx"
#include "pool/DisposablePointer.hxx"
#include "pool/tpool.hxx"
#include "util/StringList.hxx"

#include <cassert>
Expand Down Expand Up @@ -137,21 +136,20 @@ WasChild::~WasChild() noexcept = default;

void
WasStock::Get(AllocatorPtr alloc,
StockKey key,
const ChildOptions &options,
const char *executable_path,
std::span<const char *const> args,
unsigned parallelism, bool disposable,
StockGetHandler &handler,
CancellablePointer &cancel_ptr) noexcept
{
const TempPoolLease tpool;

auto r = NewDisposablePointer<CgiChildParams>(alloc, executable_path,
args, options,
parallelism,
0,
disposable);
const auto key = r->GetStockKey(*tpool);

stock.Get(key, std::move(r), handler, cancel_ptr);
}
1 change: 1 addition & 0 deletions src/was/Stock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public:
* @param args command-line arguments
*/
void Get(AllocatorPtr alloc,
StockKey key,
const ChildOptions &options,
const char *executable_path,
std::span<const char *const> args,
Expand Down

0 comments on commit eab94e4

Please sign in to comment.