Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
w-gc committed Jan 14, 2025
1 parent 001bea2 commit 344af1f
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/brpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ ServerOptions::ServerOptions()
, rtmp_service(NULL)
, redis_service(NULL)
, bthread_tag(BTHREAD_TAG_DEFAULT)
, rpc_pb_message_factory(NULL)
, rpc_pb_message_factory(new DefaultRpcPBMessageFactory())
, ignore_eovercrowded(false) {
if (s_ncore > 0) {
num_threads = s_ncore + 1;
Expand Down Expand Up @@ -782,6 +782,47 @@ static bool OptionsAvailableOverRdma(const ServerOptions* opt) {
static AdaptiveMaxConcurrency g_default_max_concurrency_of_method(0);
static bool g_default_ignore_eovercrowded(false);


inline void copy_server_option(ServerOptions& dst, const ServerOptions& src) {
if (&dst == &src) {
return;
}

#define FREE_PTR_IF_NOT_REUSED(ptr) \
if (dst.ptr != src.ptr) { \
delete dst.ptr; \
dst.ptr = NULL; \
}

FREE_PTR_IF_NOT_REUSED(nshead_service);

#ifdef ENABLE_THRIFT_FRAMED_PROTOCOL
FREE_PTR_IF_NOT_REUSED(thrift_service);
#endif

FREE_PTR_IF_NOT_REUSED(baidu_master_service);
FREE_PTR_IF_NOT_REUSED(http_master_service);
FREE_PTR_IF_NOT_REUSED(rpc_pb_message_factory);

if (dst.pid_file != src.pid_file && !dst.pid_file.empty()) {
unlink(dst.pid_file.c_str());
}

if (dst.server_owns_auth) {
FREE_PTR_IF_NOT_REUSED(auth);
}

if (dst.server_owns_interceptor) {
FREE_PTR_IF_NOT_REUSED(interceptor);
}

FREE_PTR_IF_NOT_REUSED(redis_service);

#undef FREE_PTR_IF_NOT_REUSED

dst = src;
}

int Server::StartInternal(const butil::EndPoint& endpoint,
const PortRange& port_range,
const ServerOptions *opt) {
Expand Down Expand Up @@ -814,23 +855,16 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
return -1;
}

if (&_options == opt) {
// do nothing
} else if (opt) {
if (_options.rpc_pb_message_factory != opt->rpc_pb_message_factory) {
delete _options.rpc_pb_message_factory;
_options.rpc_pb_message_factory = NULL;
}

_options = *opt;
if (opt) {
copy_server_option(_options, *opt);
} else {
// Manual free here is because new occured in constructor
delete _options.rpc_pb_message_factory;
_options.rpc_pb_message_factory = NULL;

// Always reset to default options explicitly since `_options'
// may be the options for the last run or even bad options
_options = ServerOptions();
_options.rpc_pb_message_factory = new DefaultRpcPBMessageFactory();
}

if (!_options.h2_settings.IsValid(true/*log_error*/)) {
Expand Down

0 comments on commit 344af1f

Please sign in to comment.