Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
50 lines
1.4 KiB
C++
50 lines
1.4 KiB
C++
// Partitioned bulkhead: at most 32 partitions, each with 64 concurrent
|
|
// slots and a 128 deep queue. Excess work is rejected immediately so the
|
|
// caller can shed with 503 plus Retry-After instead of queueing forever.
|
|
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
#include <string>
|
|
|
|
namespace molo::common {
|
|
class Observer;
|
|
} // namespace molo::common
|
|
|
|
namespace molo::resilience {
|
|
|
|
class Bulkhead {
|
|
public:
|
|
enum class Admission : int { kAdmitted = 0, kQueued = 1, kRejected = 2 };
|
|
|
|
Bulkhead(::molo::common::Observer& observer, std::size_t partitions = 8,
|
|
std::size_t max_concurrent = 64, std::size_t max_queue = 128);
|
|
|
|
Bulkhead(const Bulkhead&) = delete;
|
|
Bulkhead& operator=(const Bulkhead&) = delete;
|
|
|
|
Admission try_acquire(const std::string& partition);
|
|
void release(const std::string& partition);
|
|
std::size_t inflight(const std::string& partition) const;
|
|
std::size_t queued(const std::string& partition) const;
|
|
std::size_t partition_count() const;
|
|
|
|
private:
|
|
struct Slot {
|
|
std::size_t inflight = 0;
|
|
std::size_t queued = 0;
|
|
};
|
|
std::size_t slot_of(const std::string& partition) const;
|
|
|
|
::molo::common::Observer& observer_;
|
|
std::size_t partitions_;
|
|
std::size_t max_concurrent_;
|
|
std::size_t max_queue_;
|
|
mutable std::mutex mutex_;
|
|
std::array<Slot, 32> slots_;
|
|
};
|
|
|
|
} // namespace molo::resilience
|