@@ -41,10 +41,13 @@ typedef std::map<ChannelBase*, Socket*> ChannelToIdMap;
4141class SubChannel : public SocketUser {
4242public:
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
534540int 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
551552void SelectiveChannel::RemoveAndDestroyChannel (const ChannelHandle& handle) {
0 commit comments