nghttpx: Share mruby context if it is compiled from same file

This commit is contained in:
Tatsuhiro Tsujikawa 2018-08-24 23:11:21 +09:00
parent fb97f596e1
commit 6195d747ce
2 changed files with 13 additions and 3 deletions

View File

@ -181,6 +181,10 @@ void Worker::replace_downstream_config(
std::vector<std::shared_ptr<DownstreamAddrGroup>>(groups.size()); std::vector<std::shared_ptr<DownstreamAddrGroup>>(groups.size());
std::map<DownstreamKey, size_t> addr_groups_indexer; std::map<DownstreamKey, size_t> addr_groups_indexer;
// TODO It is a bit less efficient because
// mruby::create_mruby_context returns std::unique_ptr and we cannot
// use std::make_shared.
std::map<StringRef, std::shared_ptr<mruby::MRubyContext>> shared_mruby_ctxs;
for (size_t i = 0; i < groups.size(); ++i) { for (size_t i = 0; i < groups.size(); ++i) {
auto &src = groups[i]; auto &src = groups[i];
@ -190,8 +194,14 @@ void Worker::replace_downstream_config(
dst->pattern = dst->pattern =
ImmutableString{std::begin(src.pattern), std::end(src.pattern)}; ImmutableString{std::begin(src.pattern), std::end(src.pattern)};
#ifdef HAVE_MRUBY #ifdef HAVE_MRUBY
dst->mruby_ctx = mruby::create_mruby_context(src.mruby_file); auto mruby_ctx_it = shared_mruby_ctxs.find(src.mruby_file);
assert(dst->mruby_ctx); if (mruby_ctx_it == std::end(shared_mruby_ctxs)) {
dst->mruby_ctx = mruby::create_mruby_context(src.mruby_file);
assert(dst->mruby_ctx);
shared_mruby_ctxs.emplace(src.mruby_file, dst->mruby_ctx);
} else {
dst->mruby_ctx = (*mruby_ctx_it).second;
}
#endif // HAVE_MRUBY #endif // HAVE_MRUBY
auto shared_addr = std::make_shared<SharedDownstreamAddr>(); auto shared_addr = std::make_shared<SharedDownstreamAddr>();

View File

@ -194,7 +194,7 @@ struct DownstreamAddrGroup {
ImmutableString pattern; ImmutableString pattern;
std::shared_ptr<SharedDownstreamAddr> shared_addr; std::shared_ptr<SharedDownstreamAddr> shared_addr;
#ifdef HAVE_MRUBY #ifdef HAVE_MRUBY
std::unique_ptr<mruby::MRubyContext> mruby_ctx; std::shared_ptr<mruby::MRubyContext> mruby_ctx;
#endif // HAVE_MRUBY #endif // HAVE_MRUBY
// true if this group is no longer used for new request. If this is // true if this group is no longer used for new request. If this is
// true, the connection made using one of address in shared_addr // true, the connection made using one of address in shared_addr