Skip to content

Commit 89ea0d5

Browse files
author
yanyuan06
committed
support change ownship for SelectiveChannel
1 parent 39a3436 commit 89ea0d5

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

src/brpc/selective_channel.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ typedef std::map<ChannelBase*, Socket*> ChannelToIdMap;
4141
class SubChannel : public SocketUser {
4242
public:
4343
ChannelBase* chan;
44+
ChannelOwnership ownership;
4445

4546
// internal channel is deleted after the fake Socket is SetFailed
4647
void BeforeRecycle(Socket*) {
47-
delete chan;
48+
if (ownership == OWNS_CHANNEL) {
49+
delete chan;
50+
}
4851
delete this;
4952
}
5053

@@ -84,6 +87,7 @@ class ChannelBalancer : public SharedLoadBalancer {
8487
~ChannelBalancer();
8588
int Init(const char* lb_name);
8689
int AddChannel(ChannelBase* sub_channel, const std::string& tag,
90+
ChannelOwnership ownership,
8791
SelectiveChannel::ChannelHandle* handle);
8892
void RemoveAndDestroyChannel(const SelectiveChannel::ChannelHandle& handle);
8993
int SelectChannel(const LoadBalancer::SelectIn& in, SelectOut* out);
@@ -168,7 +172,9 @@ int ChannelBalancer::Init(const char* lb_name) {
168172
return SharedLoadBalancer::Init(lb_name);
169173
}
170174

171-
int ChannelBalancer::AddChannel(ChannelBase* sub_channel, const std::string& tag,
175+
int ChannelBalancer::AddChannel(ChannelBase* sub_channel,
176+
const std::string& tag,
177+
ChannelOwnership ownership,
172178
SelectiveChannel::ChannelHandle* handle) {
173179
if (NULL == sub_channel) {
174180
LOG(ERROR) << "Parameter[sub_channel] is NULL";
@@ -185,6 +191,7 @@ int ChannelBalancer::AddChannel(ChannelBase* sub_channel, const std::string& tag
185191
return -1;
186192
}
187193
sub_chan->chan = sub_channel;
194+
sub_chan->ownership = ownership;
188195
SocketId sock_id;
189196
SocketOptions options;
190197
options.user = sub_chan;
@@ -215,7 +222,7 @@ int ChannelBalancer::AddChannel(ChannelBase* sub_channel, const std::string& tag
215222
return -1;
216223
}
217224
// The health-check-related reference has been held on created.
218-
_chan_map[sub_channel]= ptr.get();
225+
_chan_map[sub_channel] = ptr.get();
219226
if (handle) {
220227
handle->id = sock_id;
221228
handle->tag = tag;
@@ -532,20 +539,15 @@ bool SelectiveChannel::initialized() const {
532539
}
533540

534541
int SelectiveChannel::AddChannel(ChannelBase* sub_channel,
535-
ChannelHandle* handle) {
536-
return AddChannel(sub_channel, "", handle);
537-
}
538-
539-
int SelectiveChannel::AddChannel(ChannelBase* sub_channel,
540-
const std::string& tag,
542+
const SubChannelOptions& option,
541543
ChannelHandle* handle) {
542544
schan::ChannelBalancer* lb =
543545
static_cast<schan::ChannelBalancer*>(_chan._lb.get());
544546
if (lb == NULL) {
545547
LOG(ERROR) << "You must call Init() to initialize a SelectiveChannel";
546548
return -1;
547549
}
548-
return lb->AddChannel(sub_channel, tag, handle);
550+
return lb->AddChannel(sub_channel, option.tag, option.ownership, handle);
549551
}
550552

551553
void SelectiveChannel::RemoveAndDestroyChannel(const ChannelHandle& handle) {

src/brpc/selective_channel.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class SelectiveChannel : public ChannelBase/*non-copyable*/ {
5656
std::string tag;
5757
};
5858

59+
struct SubChannelOptions {
60+
std::string tag;
61+
ChannelOwnership ownership;
62+
};
63+
5964
SelectiveChannel();
6065
~SelectiveChannel();
6166

@@ -69,8 +74,11 @@ class SelectiveChannel : public ChannelBase/*non-copyable*/ {
6974
// On success, handle is set with the key for removal.
7075
// NOTE: Different from pchan, schan can add channels at any time.
7176
// Returns 0 on success, -1 otherwise.
72-
int AddChannel(ChannelBase* sub_channel, ChannelHandle* handle);
73-
int AddChannel(ChannelBase* sub_channel, const std::string& tag, ChannelHandle* handle);
77+
int AddChannel(ChannelBase* sub_channel, ChannelHandle* handle) {
78+
return AddChannel(sub_channel, SubChannelOptions(), handle);
79+
}
80+
int AddChannel(ChannelBase* sub_channel, const SubChannelOptions& option,
81+
ChannelHandle* handle);
7482

7583
// Remove and destroy the sub_channel associated with `handle'.
7684
void RemoveAndDestroyChannel(const ChannelHandle& handle);

0 commit comments

Comments
 (0)