SessionProxy: rewrite session_open conditions

GitOrigin-RevId: 3a5757fdec567451671143937b5b942610f5ecdc
This commit is contained in:
Arseny Smirnov 2018-11-03 09:33:22 +03:00
parent 08bde763db
commit 130456a3bb

View File

@ -154,16 +154,30 @@ void SessionProxy::open_session(bool force) {
if (!session_.empty()) { if (!session_.empty()) {
return; return;
} }
if (!force) { // There are several assumption that make this code OK
if (auth_state_ == AuthState::Empty && need_destroy_) { // 1. All unauthorized query will be sent into the same SessionProxy
return; // 2. All authorized query are delayed before we have authorization
// So only one SessionProxy will be active before we have authorization key
auto should_open = [&]() {
if (force) {
return true;
} }
if (auth_state_ != AuthState::OK && need_wait_for_key_) { if (auth_state_ != AuthState::Empty && need_destroy_) {
return; return true;
} }
if (!is_main_ && pending_queries_.empty() && !need_destroy_) { if (need_destroy_) {
return; return false;
} }
if (is_main_ && !need_destroy_) { // alays open main
return true;
}
if (!pending_queries_.empty() && auth_state_ == AuthState::OK) {
return true;
}
return false;
}();
if (!should_open) {
return;
} }
CHECK(session_.empty()); CHECK(session_.empty());
@ -187,6 +201,9 @@ void SessionProxy::open_session(bool force) {
void SessionProxy::update_auth_state() { void SessionProxy::update_auth_state() {
auth_state_ = auth_data_->get_auth_state().first; auth_state_ = auth_data_->get_auth_state().first;
open_session(); open_session();
if (session_.empty() || auth_state_ != AuthState::OK) {
return;
}
for (auto &query : pending_queries_) { for (auto &query : pending_queries_) {
query->debug(PSTRING() << get_name() << ": sent to session"); query->debug(PSTRING() << get_name() << ": sent to session");
send_closure(session_, &Session::send, std::move(query)); send_closure(session_, &Session::send, std::move(query));