/** * swap the server connection with a connection from * the connection pool * * we can only switch backends if we have a authed connection in the pool. * * @return NULL if swapping failed * the new backend on success */ network_socket *network_connection_pool_lua_swap(network_mysqld_con *con, int backend_ndx, GHashTable *pwd_table) { network_backend_t *backend = NULL; network_socket *send_sock; network_mysqld_con_lua_t *st = con->plugin_con_state; // GString empty_username = { "", 0, 0 }; /* * we can only change to another backend if the backend is already * in the connection pool and connected */ backend = network_backends_get(con->srv->backends, backend_ndx); if (!backend) return NULL; /** * get a connection from the pool which matches our basic requirements * - username has to match * - default_db should match */ #ifdef DEBUG_CONN_POOL g_debug("%s: (swap) check if we have a connection for this user in the pool '%s'", G_STRLOC, con->client->response ? con->client->response->username->str: "empty_user"); #endif int flag = 0; network_connection_pool* pool = chassis_event_thread_pool(backend); if (NULL == (send_sock = network_connection_pool_get(pool))) { /** * no connections in the pool */ flag = 1; if (NULL == (send_sock = self_connect(con, backend, pwd_table))) { st->backend_ndx = -1; return NULL; } } /* the backend is up and cool, take and move the current backend into the pool */ #ifdef DEBUG_CONN_POOL g_debug("%s: (swap) added the previous connection to the pool", G_STRLOC); #endif // network_connection_pool_lua_add_connection(con); /* connect to the new backend */ st->backend = backend; // st->backend->connected_clients++; st->backend_ndx = backend_ndx; if (flag == 0 && !g_atomic_int_compare_and_exchange(&st->backend->connected_clients, 0, 0)) { g_atomic_int_dec_and_test(&st->backend->connected_clients); //g_critical("pool_lua_swap:%08x's connected_clients is %d\n", backend, backend->connected_clients); } return send_sock; }
/** * swap the server connection with a connection from * the connection pool * * we can only switch backends if we have a authed connection in the pool. * * @return NULL if swapping failed * the new backend on success */ network_socket *network_connection_pool_lua_swap(network_mysqld_con *con, int backend_ndx) { network_backend_t *backend = NULL; network_socket *send_sock; network_mysqld_con_lua_t *st = con->plugin_con_state; chassis_private *g = con->srv->priv; // GString empty_username = { "", 0, 0 }; /* * we can only change to another backend if the backend is already * in the connection pool and connected */ backend = network_backends_get(g->backends, backend_ndx); if (!backend) return NULL; /** * get a connection from the pool which matches our basic requirements * - username has to match * - default_db should match */ #ifdef DEBUG_CONN_POOL g_debug("%s: (swap) check if we have a connection for this user in the pool '%s'", G_STRLOC, con->client->response ? con->client->response->username->str: "empty_user"); #endif network_connection_pool* pool = chassis_event_thread_pool(backend); if (NULL == (send_sock = network_connection_pool_get(pool))) { /** * no connections in the pool */ if (NULL == (send_sock = self_connect(con, backend))) { st->backend_ndx = -1; return NULL; } } /* the backend is up and cool, take and move the current backend into the pool */ #ifdef DEBUG_CONN_POOL g_debug("%s: (swap) added the previous connection to the pool", G_STRLOC); #endif // network_connection_pool_lua_add_connection(con); /* connect to the new backend */ st->backend = backend; // st->backend->connected_clients++; st->backend_ndx = backend_ndx; return send_sock; }
/** * swap the server connection with a connection from * the connection pool * * we can only switch backends if we have a authed connection in the pool. * * @return NULL if swapping failed * the new backend on success */ network_socket* network_connection_pool_lua_swap(network_mysqld_con *con, int backend_ndx, int need_keep_conn, int *err) { network_backend_t *backend = NULL; network_socket *send_sock; network_connection_pool *pool, *second_pool; network_mysqld_con_lua_t *st = con->plugin_con_state; chassis_private *g = con->srv->priv; /* * we can only change to another backend if the backend is already * in the connection pool and connected */ backend = network_backends_get(g->backends, backend_ndx); if (!backend) return NULL; /** * get a connection from the pool which matches our basic requirements * - username has to match * - default_db should match */ #ifdef DEBUG_CONN_POOL g_debug("%s: (swap) check if we have a connection for this user in the pool '%s'", G_STRLOC, con->client->response ? con->client->response->username->str: "empty_user"); #endif if(need_keep_conn == 0) { pool = chassis_event_thread_pool(backend); send_sock = network_connection_pool_get(pool); if(send_sock == NULL) { second_pool = chassis_event_thread_secondpool(backend); send_sock = network_expiretime_connection_pool_get(second_pool, con); } }else { second_pool = chassis_event_thread_secondpool(backend); send_sock = network_connection_secondpool_get(second_pool, con); if(send_sock == NULL) g_message("%s:the connection need keep, but it's not in the pool now.", G_STRLOC); } if (NULL == send_sock ) { /** * no connections in the pool */ if ((con->config->max_connections <= 0) || (backend->connected_clients < con->config->max_connections)) { if (NULL == (send_sock = self_connect(con, backend))) { st->backend_ndx = -1; return NULL; } } else { st->backend_ndx = -1; *err = -1; return NULL; } } /* the backend is up and cool, take and move the current backend into the pool */ #ifdef DEBUG_CONN_POOL g_debug("%s: (swap) added the previous connection to the pool", G_STRLOC); #endif /* connect to the new backend */ st->backend = backend; g_atomic_int_inc(&(st->backend->connected_clients)); st->backend_ndx = backend_ndx; return send_sock; }