mirror of
https://github.com/matrix-construct/construct
synced 2024-12-27 07:54:05 +01:00
modules/client/sync: Simplify stack arguments; addl cleanup.
This commit is contained in:
parent
fce5be0317
commit
96363a527d
2 changed files with 69 additions and 67 deletions
|
@ -80,27 +80,16 @@ ircd::m::sync::method_get
|
||||||
ircd::resource::response
|
ircd::resource::response
|
||||||
ircd::m::sync::handle_get(client &client,
|
ircd::m::sync::handle_get(client &client,
|
||||||
const resource::request &request)
|
const resource::request &request)
|
||||||
|
try
|
||||||
{
|
{
|
||||||
const args args
|
const args args
|
||||||
{
|
{
|
||||||
request
|
request
|
||||||
};
|
};
|
||||||
|
|
||||||
if(!short_poll(client, request, args))
|
|
||||||
longpoll::poll(client, request, args);
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
ircd::m::sync::short_poll(client &client,
|
|
||||||
const resource::request &request,
|
|
||||||
const args &args)
|
|
||||||
try
|
|
||||||
{
|
|
||||||
shortpoll sp
|
shortpoll sp
|
||||||
{
|
{
|
||||||
client, request, args
|
client, args
|
||||||
};
|
};
|
||||||
|
|
||||||
if(sp.since > sp.current)
|
if(sp.since > sp.current)
|
||||||
|
@ -119,12 +108,19 @@ try
|
||||||
384 //TODO: conf
|
384 //TODO: conf
|
||||||
};
|
};
|
||||||
|
|
||||||
return
|
const bool shortpolled
|
||||||
|
{
|
||||||
sp.delta == 0?
|
sp.delta == 0?
|
||||||
false:
|
false:
|
||||||
sp.delta > max_linear_sync?
|
sp.delta > max_linear_sync?
|
||||||
polylog::handle(client, request, sp, top):
|
polylog::handle(client, sp, top):
|
||||||
linear::handle(client, request, sp, top);
|
linear::handle(client, sp, top)
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!shortpolled)
|
||||||
|
longpoll::poll(client, args);
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
catch(const bad_lex_cast &e)
|
catch(const bad_lex_cast &e)
|
||||||
{
|
{
|
||||||
|
@ -136,7 +132,6 @@ catch(const bad_lex_cast &e)
|
||||||
|
|
||||||
void
|
void
|
||||||
ircd::m::sync::longpoll::poll(client &client,
|
ircd::m::sync::longpoll::poll(client &client,
|
||||||
const resource::request &request,
|
|
||||||
const args &args)
|
const args &args)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -156,7 +151,7 @@ try
|
||||||
if(!accepted.opts->notify_clients)
|
if(!accepted.opts->notify_clients)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if(handle(client, request, args, accepted))
|
if(handle(client, args, accepted))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,7 +177,6 @@ catch(const ctx::timeout &e)
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ircd::m::sync::longpoll::handle(client &client,
|
ircd::m::sync::longpoll::handle(client &client,
|
||||||
const resource::request &request,
|
|
||||||
const args &args,
|
const args &args,
|
||||||
const m::event &event)
|
const m::event &event)
|
||||||
{
|
{
|
||||||
|
@ -194,7 +188,7 @@ ircd::m::sync::longpoll::handle(client &client,
|
||||||
if(room_id)
|
if(room_id)
|
||||||
{
|
{
|
||||||
const m::room room{room_id};
|
const m::room room{room_id};
|
||||||
return handle(client, request, args, event, room);
|
return handle(client, args, event, room);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -202,14 +196,13 @@ ircd::m::sync::longpoll::handle(client &client,
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ircd::m::sync::longpoll::handle(client &client,
|
ircd::m::sync::longpoll::handle(client &client,
|
||||||
const resource::request &request,
|
|
||||||
const args &args,
|
const args &args,
|
||||||
const m::event &event,
|
const m::event &event,
|
||||||
const m::room &room)
|
const m::room &room)
|
||||||
{
|
{
|
||||||
const m::user::id &user_id
|
const m::user::id &user_id
|
||||||
{
|
{
|
||||||
request.user_id
|
args.request.user_id
|
||||||
};
|
};
|
||||||
|
|
||||||
if(!room.membership(user_id, "join"))
|
if(!room.membership(user_id, "join"))
|
||||||
|
@ -222,12 +215,15 @@ ircd::m::sync::longpoll::handle(client &client,
|
||||||
|
|
||||||
const auto rooms
|
const auto rooms
|
||||||
{
|
{
|
||||||
sync_rooms(client, request, request.user_id, room, args, event)
|
sync_rooms(client, user_id, room, args, event)
|
||||||
};
|
};
|
||||||
|
|
||||||
const m::user::room ur
|
const m::user::room ur
|
||||||
{
|
{
|
||||||
m::user::id{request.user_id}
|
m::user::id
|
||||||
|
{
|
||||||
|
args.request.user_id
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<json::value> presents;
|
std::vector<json::value> presents;
|
||||||
|
@ -267,7 +263,6 @@ ircd::m::sync::longpoll::handle(client &client,
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
ircd::m::sync::longpoll::sync_rooms(client &client,
|
ircd::m::sync::longpoll::sync_rooms(client &client,
|
||||||
const resource::request &request,
|
|
||||||
const m::user::id &user_id,
|
const m::user::id &user_id,
|
||||||
const m::room &room,
|
const m::room &room,
|
||||||
const args &args,
|
const args &args,
|
||||||
|
@ -377,7 +372,6 @@ ircd::m::sync::longpoll::sync_room(client &client,
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ircd::m::sync::linear::handle(client &client,
|
ircd::m::sync::linear::handle(client &client,
|
||||||
const resource::request &request,
|
|
||||||
shortpoll &sp,
|
shortpoll &sp,
|
||||||
json::stack::object &object)
|
json::stack::object &object)
|
||||||
{
|
{
|
||||||
|
@ -407,7 +401,7 @@ ircd::m::sync::linear::handle(client &client,
|
||||||
json::get<"room_id"_>(event)
|
json::get<"room_id"_>(event)
|
||||||
};
|
};
|
||||||
|
|
||||||
if(!room.membership(request.user_id))
|
if(!room.membership(sp.args.request.user_id))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
auto it
|
auto it
|
||||||
|
@ -503,7 +497,6 @@ ircd::m::sync::linear::handle(client &client,
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ircd::m::sync::polylog::handle(client &client,
|
ircd::m::sync::polylog::handle(client &client,
|
||||||
const resource::request &request,
|
|
||||||
shortpoll &sp,
|
shortpoll &sp,
|
||||||
json::stack::object &object)
|
json::stack::object &object)
|
||||||
try
|
try
|
||||||
|
|
|
@ -14,10 +14,8 @@ namespace ircd::m::sync
|
||||||
struct stats;
|
struct stats;
|
||||||
struct shortpoll;
|
struct shortpoll;
|
||||||
|
|
||||||
static bool short_poll(client &, const resource::request &, const args &);
|
|
||||||
static resource::response handle_get(client &, const resource::request &);
|
static resource::response handle_get(client &, const resource::request &);
|
||||||
extern resource::method method_get;
|
extern resource::method method_get;
|
||||||
|
|
||||||
extern const string_view description;
|
extern const string_view description;
|
||||||
extern resource resource;
|
extern resource resource;
|
||||||
}
|
}
|
||||||
|
@ -25,15 +23,15 @@ namespace ircd::m::sync
|
||||||
namespace ircd::m::sync::longpoll
|
namespace ircd::m::sync::longpoll
|
||||||
{
|
{
|
||||||
static std::string sync_room(client &, const m::room &, const args &, const m::event &);
|
static std::string sync_room(client &, const m::room &, const args &, const m::event &);
|
||||||
static std::string sync_rooms(client &, const resource::request &, const m::user::id &, const m::room &, const args &, const m::event &);
|
static std::string sync_rooms(client &, const m::user::id &, const m::room &, const args &, const m::event &);
|
||||||
static bool handle(client &, const resource::request &, const args &, const m::event &, const m::room &);
|
static bool handle(client &, const args &, const m::event &, const m::room &);
|
||||||
static bool handle(client &, const resource::request &, const args &, const m::event &);
|
static bool handle(client &, const args &, const m::event &);
|
||||||
static void poll(client &, const resource::request &, const args &);
|
static void poll(client &, const args &);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace ircd::m::sync::linear
|
namespace ircd::m::sync::linear
|
||||||
{
|
{
|
||||||
static bool handle(client &, const resource::request &, shortpoll &, json::stack::object &);
|
static bool handle(client &, shortpoll &, json::stack::object &);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace ircd::m::sync::polylog
|
namespace ircd::m::sync::polylog
|
||||||
|
@ -50,7 +48,7 @@ namespace ircd::m::sync::polylog
|
||||||
static void rooms(shortpoll &, json::stack::object &);
|
static void rooms(shortpoll &, json::stack::object &);
|
||||||
static void presence(shortpoll &, json::stack::object &);
|
static void presence(shortpoll &, json::stack::object &);
|
||||||
static void account_data(shortpoll &, json::stack::object &);
|
static void account_data(shortpoll &, json::stack::object &);
|
||||||
static bool handle(client &, const resource::request &, shortpoll &, json::stack::object &);
|
static bool handle(client &, shortpoll &, json::stack::object &);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Argument parser for the client's /sync request
|
/// Argument parser for the client's /sync request
|
||||||
|
@ -60,6 +58,10 @@ struct ircd::m::sync::args
|
||||||
static conf::item<milliseconds> timeout_min;
|
static conf::item<milliseconds> timeout_min;
|
||||||
static conf::item<milliseconds> timeout_default;
|
static conf::item<milliseconds> timeout_default;
|
||||||
|
|
||||||
|
args(const resource::request &request)
|
||||||
|
:request{request}
|
||||||
|
{}
|
||||||
|
|
||||||
const resource::request &request;
|
const resource::request &request;
|
||||||
|
|
||||||
string_view filter_id
|
string_view filter_id
|
||||||
|
@ -106,10 +108,6 @@ struct ircd::m::sync::args
|
||||||
// marked as being online when it uses this API. One of: ["offline"]
|
// marked as being online when it uses this API. One of: ["offline"]
|
||||||
request.query.get("set_presence", true)
|
request.query.get("set_presence", true)
|
||||||
};
|
};
|
||||||
|
|
||||||
args(const resource::request &request)
|
|
||||||
:request{request}
|
|
||||||
{}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ircd::m::sync::stats
|
struct ircd::m::sync::stats
|
||||||
|
@ -123,10 +121,19 @@ struct ircd::m::sync::shortpoll
|
||||||
{
|
{
|
||||||
static conf::item<size_t> flush_hiwat;
|
static conf::item<size_t> flush_hiwat;
|
||||||
|
|
||||||
ircd::client &client;
|
shortpoll(ircd::client &client,
|
||||||
const resource::request &request;
|
const sync::args &args)
|
||||||
const sync::args &args;
|
:client{client}
|
||||||
|
,args{args}
|
||||||
|
{}
|
||||||
|
|
||||||
sync::stats stats;
|
sync::stats stats;
|
||||||
|
ircd::client &client;
|
||||||
|
const sync::args &args;
|
||||||
|
const resource::request &request
|
||||||
|
{
|
||||||
|
args.request
|
||||||
|
};
|
||||||
|
|
||||||
const uint64_t &since
|
const uint64_t &since
|
||||||
{
|
{
|
||||||
|
@ -180,33 +187,35 @@ struct ircd::m::sync::shortpoll
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
unique_buffer<mutable_buffer> buf {96_KiB};
|
unique_buffer<mutable_buffer> buf
|
||||||
|
{
|
||||||
|
96_KiB
|
||||||
|
};
|
||||||
|
|
||||||
std::unique_ptr<resource::response::chunked> response;
|
std::unique_ptr<resource::response::chunked> response;
|
||||||
json::stack out
|
json::stack out
|
||||||
{
|
{
|
||||||
buf, [this](const const_buffer &buf)
|
buf, std::bind(&shortpoll::flush, this, ph::_1), size_t(flush_hiwat)
|
||||||
{
|
|
||||||
if(!committed)
|
|
||||||
return buf;
|
|
||||||
|
|
||||||
if(!response)
|
|
||||||
response = std::make_unique<resource::response::chunked>
|
|
||||||
(
|
|
||||||
client, http::OK, "application/json; charset=utf-8"
|
|
||||||
);
|
|
||||||
|
|
||||||
stats.flush_bytes += response->write(buf);
|
|
||||||
stats.flush_count++;
|
|
||||||
return buf;
|
|
||||||
},
|
|
||||||
size_t(flush_hiwat)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
shortpoll(ircd::client &client,
|
void commit()
|
||||||
const resource::request &request,
|
{
|
||||||
const sync::args &args)
|
response = std::make_unique<resource::response::chunked>
|
||||||
:client{client}
|
(
|
||||||
,request{request}
|
client, http::OK, "application/json; charset=utf-8"
|
||||||
,args{args}
|
);
|
||||||
{}
|
}
|
||||||
|
|
||||||
|
const_buffer flush(const const_buffer &buf)
|
||||||
|
{
|
||||||
|
if(!committed)
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
if(!response)
|
||||||
|
commit();
|
||||||
|
|
||||||
|
stats.flush_bytes += response->write(buf);
|
||||||
|
stats.flush_count++;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue