0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-03-14 05:20:17 +01:00

ircd::ctx: Add soft and hard limit logic for submitting to pool.

This commit is contained in:
Jason Volk 2018-12-27 15:45:02 -08:00
parent 73459e75ff
commit 364302c212
2 changed files with 51 additions and 2 deletions

View file

@ -24,8 +24,11 @@ class ircd::ctx::pool
string_view name;
size_t stack_size;
size_t q_max_soft;
size_t q_max_hard;
size_t running;
size_t working;
dock q_max;
queue<closure> q;
std::vector<context> ctxs;
@ -61,7 +64,9 @@ class ircd::ctx::pool
pool(const string_view &name = "<unnamed pool>"_sv,
const size_t &stack_size = DEFAULT_STACK_SIZE,
const size_t &initial_ctxs = 0);
const size_t &initial_ctxs = 0,
const size_t &q_max_soft = -1,
const size_t &q_max_hard = -1);
pool(pool &&) = delete;
pool(const pool &) = delete;

View file

@ -1232,9 +1232,13 @@ ircd::ctx::name(const pool &pool)
ircd::ctx::pool::pool(const string_view &name,
const size_t &stack_size,
const size_t &size)
const size_t &size,
const size_t &q_max_soft,
const size_t &q_max_hard)
:name{name}
,stack_size{stack_size}
,q_max_soft{q_max_soft}
,q_max_hard{q_max_hard}
,running{0}
,working{0}
{
@ -1314,6 +1318,44 @@ ircd::ctx::pool::add(const size_t &num)
void
ircd::ctx::pool::operator()(closure closure)
{
if(!avail() && q.size() > q_max_soft)
log::dwarning
{
"pool(%p '%s') ctx(%p): size:%zu active:%zu queue:%zu exceeded soft max:%zu",
this,
name,
current,
size(),
active(),
q.size(),
q_max_soft
};
if(current)
q_max.wait([this]
{
if(q.size() < q_max_soft)
return true;
if(!q_max_soft && q.size() < avail())
return true;
return false;
});
if(unlikely(q.size() >= q_max_hard))
throw error
{
"pool(%p '%s') ctx(%p): size:%zu avail:%zu queue:%zu exceeded hard max:%zu",
this,
name,
current,
size(),
avail(),
q.size(),
q_max_hard
};
q.push(std::move(closure));
}
@ -1323,6 +1365,7 @@ ircd::ctx::pool::main()
noexcept try
{
++running;
q_max.notify();
const unwind avail([this]
{
--running;
@ -1359,6 +1402,7 @@ try
const unwind avail([this]
{
--working;
q_max.notify();
});
// Execute the user's function