125 bool is_closed_front =
false;
128 if (package.session().is_closed())
130 is_closed_front =
true;
133 Z_GDU *gdu_req = package.request().get();
136 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
139 if (gdu_req->u.z3950->which == Z_APDU_initRequest)
141 yazpp_1::GDU base_req(gdu_req);
142 Z_APDU *apdu = base_req.get()->u.z3950;
144 Z_InitRequest *org_init = base_req.get()->u.z3950->u.initRequest;
145 mp::odr odr_en(ODR_ENCODE);
147 std::list<std::string> vhosts;
148 mp::util::remove_vhost_otherinfo(&(org_init->otherInfo), vhosts);
153 std::list<std::string>::iterator ivh = vhosts.begin();
154 std::list<std::string>::iterator ivh_pick = vhosts.end();
156 Package init_pkg(package.session(), package.origin());
157 init_pkg.copy_filter(package);
159 unsigned int cost_i = std::numeric_limits<unsigned int>::max();
161 boost::mutex::scoped_lock scoped_lock(
m_mutex);
163 for (; ivh != vhosts.end(); ivh++)
165 if ((*ivh).size() != 0)
170 std::ostringstream os;
174 << "Consider " << *ivh
176 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
185 if (ivh_pick == vhosts.end())
187 std::string target = *ivh_pick;
188 vhosts.erase(ivh_pick);
190 yazpp_1::GDU init_gdu(base_req);
191 Z_InitRequest *init_req = init_gdu.get()->u.z3950->u.initRequest;
193 mp::util::set_vhost_otherinfo(&(init_req->otherInfo),
196 init_pkg.request() = init_gdu;
202 if (!init_pkg.session().is_closed())
205 boost::mutex::scoped_lock scoped_lock(
m_mutex);
209 package.response() = init_pkg.response();
212 std::ostringstream os;
216 << "Failed " << target;
217 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
220 package.response() = odr.create_initResponse(
221 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
222 "load_balance: no available targets");
223 package.session().close();
227 else if (gdu_req->u.z3950->which == Z_APDU_close)
229 is_closed_front =
true;
230 boost::mutex::scoped_lock scoped_lock(
m_mutex);
236 boost::mutex::scoped_lock scoped_lock(
m_mutex);
244 bool is_closed_back =
false;
247 if (package.session().is_closed())
248 is_closed_back =
true;
250 Z_GDU *gdu_res = package.response().get();
253 if (gdu_res && gdu_res->which == Z_GDU_Z3950)
256 if (gdu_res->u.z3950->which == Z_APDU_close)
258 is_closed_back =
true;
259 boost::mutex::scoped_lock scoped_lock(
m_mutex);
265 boost::mutex::scoped_lock scoped_lock(
m_mutex);
271 if (is_closed_back || is_closed_front)
273 boost::mutex::scoped_lock scoped_lock(
m_mutex);
276 if (is_closed_front ==
false)
282 package.session().close();
342 unsigned long session_id = package.session().id();
343 std::map<unsigned long, std::string>::iterator isess;
349 std::ostringstream os;
352 << "0.000000 Add " << target << " size="
353 << m_session_target.size();
354 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
358 std::map<std::string, TargetStat>::iterator itarg;
368 else if (itarg->second.sessions < std::numeric_limits<unsigned int>::max())
370 itarg->second.sessions += 1;
376 unsigned long session_id = package.session().id();
377 std::map<unsigned long, std::string>::iterator isess;
382 std::string target = isess->second;
385 std::ostringstream os;
388 << "0.000000 Remove " << target << " size="
389 << m_session_target.size();
390 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
393 std::map<std::string, TargetStat>::iterator itarg;
398 if (itarg->second.sessions > 0)
399 itarg->second.sessions -= 1;
401 if (itarg->second.sessions == 0 || itarg->second.deads == 0)