Skip to content

Commit 029d73c

Browse files
authored
Merge pull request #180 from zd-double/opensource
Add create_with_init option in RpcChannelOptions
2 parents 636920b + 3462acc commit 029d73c

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/sofa/pbrpc/rpc_channel.cc

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ RpcChannel::RpcChannel(RpcClient* rpc_client,
1616
const RpcChannelOptions& options)
1717
: _impl(new SimpleRpcChannelImpl(rpc_client->impl(), server_address, options))
1818
{
19-
_impl->Init();
19+
if (options.create_with_init)
20+
{
21+
_impl->Init();
22+
}
2023
}
2124

2225
RpcChannel::RpcChannel(RpcClient* rpc_client,
@@ -27,23 +30,37 @@ RpcChannel::RpcChannel(RpcClient* rpc_client,
2730
std::ostringstream os;
2831
os << server_ip << ":" << server_port;
2932
_impl.reset(new SimpleRpcChannelImpl(rpc_client->impl(), os.str(), options));
30-
_impl->Init();
33+
if (options.create_with_init)
34+
{
35+
_impl->Init();
36+
}
3137
}
3238

3339
RpcChannel::RpcChannel(RpcClient* rpc_client,
3440
const std::vector<std::string>& address_list,
3541
const RpcChannelOptions& options)
3642
: _impl(new DynamicRpcChannelImpl(rpc_client->impl(), address_list, options))
3743
{
38-
_impl->Init();
44+
if (options.create_with_init)
45+
{
46+
_impl->Init();
47+
}
3948
}
4049

4150
RpcChannel::RpcChannel(RpcClient* rpc_client,
4251
AddressProvider* address_provider,
4352
const RpcChannelOptions& options)
4453
: _impl(new DynamicRpcChannelImpl(rpc_client->impl(), address_provider, options))
4554
{
46-
_impl->Init();
55+
if (options.create_with_init)
56+
{
57+
_impl->Init();
58+
}
59+
}
60+
61+
bool RpcChannel::Init()
62+
{
63+
return _impl->Init();
4764
}
4865

4966
RpcChannel::~RpcChannel()

src/sofa/pbrpc/rpc_channel.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ struct RpcChannelOptions {
3232
// Value 0 means no limit, default value is 0.
3333
uint32 server_load_capacity;
3434

35+
// If initialize the RpcChannel in construct function, default is true.
36+
// If create_with_init is false, RpcChannel should be initialized by calling Init().
37+
bool create_with_init;
38+
3539
RpcChannelOptions()
3640
: connect_timeout(10)
3741
, server_load_capacity(0)
42+
, create_with_init(true)
3843
{}
3944
};
4045

@@ -93,6 +98,14 @@ class RpcChannel : public google::protobuf::RpcChannel
9398
AddressProvider* address_provider,
9499
const RpcChannelOptions& options = RpcChannelOptions());
95100

101+
// Initialize RpcChannel.
102+
// For single server point, it will resolve server address in this function,
103+
// and if resolve server address succeed return true, otherwise return false.
104+
// For multiple server points, it will update internal server list and
105+
// register detect task. After all of these are completed return true, and
106+
// never return false.
107+
bool Init();
108+
96109
// Destructor.
97110
virtual ~RpcChannel();
98111

0 commit comments

Comments
 (0)