Skip to content

Commit 05d8424

Browse files
author
yanyuan06
committed
support change ownship for SelectiveChannel
1 parent 39a3436 commit 05d8424

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

src/brpc/selective_channel.cpp

Lines changed: 14 additions & 13 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

@@ -83,7 +86,8 @@ class ChannelBalancer : public SharedLoadBalancer {
8386
ChannelBalancer() {}
8487
~ChannelBalancer();
8588
int Init(const char* lb_name);
86-
int AddChannel(ChannelBase* sub_channel, const std::string& tag,
89+
int AddChannel(ChannelBase* sub_channel,
90+
const SelectiveChannel::SubChannelOptions& subopt,
8791
SelectiveChannel::ChannelHandle* handle);
8892
void RemoveAndDestroyChannel(const SelectiveChannel::ChannelHandle& handle);
8993
int SelectChannel(const LoadBalancer::SelectIn& in, SelectOut* out);
@@ -168,7 +172,8 @@ 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 SelectiveChannel::SubChannelOptions& subopt,
172177
SelectiveChannel::ChannelHandle* handle) {
173178
if (NULL == sub_channel) {
174179
LOG(ERROR) << "Parameter[sub_channel] is NULL";
@@ -185,6 +190,7 @@ int ChannelBalancer::AddChannel(ChannelBase* sub_channel, const std::string& tag
185190
return -1;
186191
}
187192
sub_chan->chan = sub_channel;
193+
sub_chan->ownership = subopt.ownership;
188194
SocketId sock_id;
189195
SocketOptions options;
190196
options.user = sub_chan;
@@ -206,7 +212,7 @@ int ChannelBalancer::AddChannel(ChannelBase* sub_channel, const std::string& tag
206212
<< sock_id << " is disabled";
207213
return -1;
208214
}
209-
if (!AddServer(ServerId(sock_id, tag))) {
215+
if (!AddServer(ServerId(sock_id, subopt.tag))) {
210216
LOG(ERROR) << "Duplicated sub_channel=" << sub_channel;
211217
// sub_chan will be deleted when the socket is recycled.
212218
ptr->SetFailed();
@@ -215,10 +221,10 @@ int ChannelBalancer::AddChannel(ChannelBase* sub_channel, const std::string& tag
215221
return -1;
216222
}
217223
// The health-check-related reference has been held on created.
218-
_chan_map[sub_channel]= ptr.get();
224+
_chan_map[sub_channel] = ptr.get();
219225
if (handle) {
220226
handle->id = sock_id;
221-
handle->tag = tag;
227+
handle->tag = subopt.tag;
222228
}
223229
return 0;
224230
}
@@ -532,20 +538,15 @@ bool SelectiveChannel::initialized() const {
532538
}
533539

534540
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,
541+
const SubChannelOptions& option,
541542
ChannelHandle* handle) {
542543
schan::ChannelBalancer* lb =
543544
static_cast<schan::ChannelBalancer*>(_chan._lb.get());
544545
if (lb == NULL) {
545546
LOG(ERROR) << "You must call Init() to initialize a SelectiveChannel";
546547
return -1;
547548
}
548-
return lb->AddChannel(sub_channel, tag, handle);
549+
return lb->AddChannel(sub_channel, option, handle);
549550
}
550551

551552
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)