Use hash counts to calculate numbers of clients.

This commit is contained in:
Roger A. Light 2014-09-17 00:03:14 +01:00
parent 3eead8c507
commit 1fb5a3edc6
5 changed files with 12 additions and 22 deletions

View File

@ -199,12 +199,6 @@ void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt)
ctxt->will = NULL; ctxt->will = NULL;
} }
ctxt->disconnect_t = time(NULL); ctxt->disconnect_t = time(NULL);
#ifdef WITH_SYS_TREE
db->connected_count--;
if(!ctxt->clean_session){
db->disconnected_count++;
}
#endif
_mosquitto_socket_close(db, ctxt); _mosquitto_socket_close(db, ctxt);
} }

View File

@ -221,8 +221,6 @@ struct mosquitto_db{
#ifdef WITH_SYS_TREE #ifdef WITH_SYS_TREE
int subscription_count; int subscription_count;
int retained_count; int retained_count;
int connected_count;
int disconnected_count;
#endif #endif
}; };

View File

@ -55,9 +55,6 @@ static struct mosquitto *_db_find_or_add_context(struct mosquitto_db *db, const
return NULL; return NULL;
} }
#ifdef WITH_SYS_TREE
db->disconnected_count++;
#endif
context->clean_session = false; context->clean_session = false;
HASH_ADD_KEYPTR(hh_id, db->contexts_by_id, context->id, strlen(context->id), context); HASH_ADD_KEYPTR(hh_id, db->contexts_by_id, context->id, strlen(context->id), context);

View File

@ -104,7 +104,6 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context)
#ifdef WITH_SYS_TREE #ifdef WITH_SYS_TREE
g_connection_count++; g_connection_count++;
db->connected_count++;
#endif #endif
/* Don't accept multiple CONNECT commands. */ /* Don't accept multiple CONNECT commands. */
@ -407,10 +406,7 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context)
/* Found a matching client */ /* Found a matching client */
if(found_context->sock == INVALID_SOCKET){ if(found_context->sock == INVALID_SOCKET){
/* Client is reconnecting after a disconnect */ /* Client is reconnecting after a disconnect */
/* FIXME - does anything else need to be done here? */ /* FIXME - does anything need to be done here? */
#ifdef WITH_SYS_TREE
db->disconnected_count--;
#endif
}else{ }else{
/* Client is already connected, disconnect old version. This is /* Client is already connected, disconnect old version. This is
* done in mqtt3_context_cleanup() below. */ * done in mqtt3_context_cleanup() below. */

View File

@ -48,8 +48,13 @@ static void _sys_update_clients(struct mosquitto_db *db, char *buf)
static unsigned int disconnected_count = -1; static unsigned int disconnected_count = -1;
static unsigned int connected_count = -1; static unsigned int connected_count = -1;
if(client_count != db->connected_count + db->disconnected_count){ unsigned int count_total, count_by_sock;
client_count = db->connected_count + db->disconnected_count;
count_total = HASH_CNT(hh_id, db->contexts_by_id);
count_by_sock = HASH_CNT(hh_sock, db->contexts_by_sock);
if(client_count != count_total){
client_count = count_total;
snprintf(buf, BUFLEN, "%d", client_count); snprintf(buf, BUFLEN, "%d", client_count);
mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/total", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/total", 2, strlen(buf), buf, 1);
@ -60,14 +65,14 @@ static void _sys_update_clients(struct mosquitto_db *db, char *buf)
} }
} }
if(disconnected_count != db->disconnected_count){ if(disconnected_count != count_total-count_by_sock){
disconnected_count = db->disconnected_count; disconnected_count = count_total-count_by_sock;
snprintf(buf, BUFLEN, "%d", disconnected_count); snprintf(buf, BUFLEN, "%d", disconnected_count);
mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/inactive", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/inactive", 2, strlen(buf), buf, 1);
mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/disconnected", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/disconnected", 2, strlen(buf), buf, 1);
} }
if(connected_count != db->connected_count){ if(connected_count != count_by_sock){
connected_count = db->connected_count; connected_count = count_by_sock;
snprintf(buf, BUFLEN, "%d", connected_count); snprintf(buf, BUFLEN, "%d", connected_count);
mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/active", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/active", 2, strlen(buf), buf, 1);
mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/connected", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/connected", 2, strlen(buf), buf, 1);