From 8507b00b34d4857cb702b1be1ea6b199007025d7 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 27 Oct 2003 15:14:03 +0100 Subject: fixes for max_user_connections (connections are now counted even if max_user_connections is not set - that is no limit - so that when max_user_connections is set (with SET) old connections are also taken into account mutexes are added where appropriate --- sql/sql_parse.cc | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'sql/sql_parse.cc') diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index e068c309e21..ebcfc2bdec1 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -156,7 +156,7 @@ static int get_or_create_user_conn(THD *thd, const char *user, uc->user_len= user_len; uc->host=uc->user + uc->user_len + 1; uc->len = temp_len; - uc->connections = 1; + uc->connections = 0; uc->questions=uc->updates=uc->conn_per_hour=0; uc->user_resources=*mqh; if (max_user_connections && mqh->connections > max_user_connections) @@ -171,6 +171,7 @@ static int get_or_create_user_conn(THD *thd, const char *user, } } thd->user_connect=uc; + uc->connections++; end: (void) pthread_mutex_unlock(&LOCK_user_conn); return return_val; @@ -255,8 +256,8 @@ static bool check_user(THD *thd,enum_server_command command, const char *user, if ((ur.questions || ur.updates || ur.connections || max_user_connections) && get_or_create_user_conn(thd,user,thd->host_or_ip,&ur)) return -1; - if (thd->user_connect && ((thd->user_connect->user_resources.connections) || - max_user_connections) && + if (thd->user_connect && (thd->user_connect->user_resources.connections || + max_user_connections) && check_for_max_user_connections(thd->user_connect)) return -1; if (db && db[0]) @@ -303,16 +304,17 @@ static int check_for_max_user_connections(USER_CONN *uc) int error=0; DBUG_ENTER("check_for_max_user_connections"); + (void) pthread_mutex_lock(&LOCK_user_conn); if (max_user_connections && - (max_user_connections < (uint) uc->connections)) + max_user_connections <= uc->connections) { net_printf(&(current_thd->net),ER_TOO_MANY_USER_CONNECTIONS, uc->user); error=1; + uc->connections--; goto end; } - uc->connections++; if (uc->user_resources.connections && - uc->conn_per_hour++ >= uc->user_resources.connections) + uc->user_resources.connections <= uc->conn_per_hour) { net_printf(¤t_thd->net, ER_USER_LIMIT_REACHED, uc->user, "max_connections", @@ -320,7 +322,9 @@ static int check_for_max_user_connections(USER_CONN *uc) error=1; goto end; } + uc->conn_per_hour++; end: + (void) pthread_mutex_unlock(&LOCK_user_conn); DBUG_RETURN(error); } @@ -328,13 +332,14 @@ end: static void decrease_user_connections(USER_CONN *uc) { DBUG_ENTER("decrease_user_connections"); - if ((uc->connections && !--uc->connections) && !mqh_used) + (void) pthread_mutex_lock(&LOCK_user_conn); + DBUG_ASSERT(uc->connections); + if (!--uc->connections && !mqh_used) { /* Last connection for user; Delete it */ - (void) pthread_mutex_lock(&LOCK_user_conn); (void) hash_delete(&hash_user_connections,(byte*) uc); - (void) pthread_mutex_unlock(&LOCK_user_conn); } + (void) pthread_mutex_unlock(&LOCK_user_conn); DBUG_VOID_RETURN; } @@ -1026,7 +1031,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, thd->priv_user=save_priv_user; break; } - if (max_connections && save_uc) + if (save_uc) decrease_user_connections(save_uc); x_free((gptr) save_db); x_free((gptr) save_user); -- cgit v1.2.1