static int relationship_ping_leader() { int result; int leader_index; ConnectionInfo *pTrackerServer; if (g_if_leader_self) { return 0; //do not need ping myself } leader_index = g_tracker_servers.leader_index; if (leader_index < 0) { return EINVAL; } pTrackerServer = g_tracker_servers.servers + leader_index; if (pTrackerServer->sock < 0) { if ((result=conn_pool_connect_server(pTrackerServer, \ g_fdfs_connect_timeout)) != 0) { return result; } } if ((result=fdfs_ping_leader(pTrackerServer)) != 0) { close(pTrackerServer->sock); pTrackerServer->sock = -1; } return result; }
static void sigAlarmHandler(int sig) { ConnectionInfo server; if (bAcceptEndFlag) { return; } logDebug("file: "__FILE__", line: %d, " \ "signal server to quit...", __LINE__); if (*g_bind_addr != '\0') { strcpy(server.ip_addr, g_bind_addr); } else { strcpy(server.ip_addr, "127.0.0.1"); } server.port = g_server_port; server.sock = -1; if (conn_pool_connect_server(&server, g_fdfs_connect_timeout) != 0) { return; } fdfs_quit(&server); conn_pool_disconnect_server(&server); logDebug("file: "__FILE__", line: %d, " \ "signal server to quit done", __LINE__); }
int tracker_connect_server_no_pool(ConnectionInfo *pTrackerServer) { if (pTrackerServer->sock >= 0) { return 0; } return conn_pool_connect_server(pTrackerServer, \ g_fdfs_connect_timeout); }
ConnectionInfo *tracker_connect_server_ex(ConnectionInfo *pTrackerServer, \ const int connect_timeout, int *err_no) { if (g_use_connection_pool) { return conn_pool_get_connection(&g_connection_pool, pTrackerServer, err_no); } else { *err_no = conn_pool_connect_server(pTrackerServer, \ connect_timeout); if (*err_no != 0) { return NULL; } else { return pTrackerServer; } } }
int tracker_get_all_connections_ex(TrackerServerGroup *pTrackerGroup) { ConnectionInfo *pServer; ConnectionInfo *pEnd; int success_count; success_count = 0; pEnd = pTrackerGroup->servers + pTrackerGroup->server_count; for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++) { if (pServer->sock >= 0) { success_count++; } else if (conn_pool_connect_server(pServer, \ g_fdfs_connect_timeout) == 0) { fdfs_active_test(pServer); success_count++; } } return success_count > 0 ? 0 : ENOTCONN; }
ConnectionInfo *conn_pool_get_connection(ConnectionPool *cp, const ConnectionInfo *conn, int *err_no) { char key[32]; int key_len; int bytes; char *p; ConnectionManager *cm; ConnectionNode *node; ConnectionInfo *ci; time_t current_time; *err_no = conn_pool_get_key(conn, key, &key_len); if (*err_no != 0) { return NULL; } pthread_mutex_lock(&cp->lock); cm = (ConnectionManager *)hash_find(&cp->hash_array, key, key_len); if (cm == NULL) { cm = (ConnectionManager *)malloc(sizeof(ConnectionManager)); if (cm == NULL) { *err_no = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, errno: %d, " \ "error info: %s", __LINE__, \ (int)sizeof(ConnectionManager), \ *err_no, STRERROR(*err_no)); pthread_mutex_unlock(&cp->lock); return NULL; } cm->head = NULL; cm->total_count = 0; cm->free_count = 0; if ((*err_no=init_pthread_lock(&cm->lock)) != 0) { pthread_mutex_unlock(&cp->lock); return NULL; } hash_insert(&cp->hash_array, key, key_len, cm); } pthread_mutex_unlock(&cp->lock); current_time = get_current_time(); pthread_mutex_lock(&cm->lock); while (1) { if (cm->head == NULL) { if ((cp->max_count_per_entry > 0) && (cm->total_count >= cp->max_count_per_entry)) { *err_no = ENOSPC; logError("file: "__FILE__", line: %d, " \ "connections: %d of server %s:%d " \ "exceed limit: %d", __LINE__, \ cm->total_count, conn->ip_addr, \ conn->port, cp->max_count_per_entry); pthread_mutex_unlock(&cm->lock); return NULL; } bytes = sizeof(ConnectionNode) + sizeof(ConnectionInfo); p = (char *)malloc(bytes); if (p == NULL) { *err_no = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, errno: %d, " \ "error info: %s", __LINE__, \ bytes, *err_no, STRERROR(*err_no)); pthread_mutex_unlock(&cm->lock); return NULL; } node = (ConnectionNode *)p; node->conn = (ConnectionInfo *)(p + sizeof(ConnectionNode)); node->manager = cm; node->next = NULL; node->atime = 0; cm->total_count++; pthread_mutex_unlock(&cm->lock); memcpy(node->conn, conn, sizeof(ConnectionInfo)); node->conn->sock = -1; *err_no = conn_pool_connect_server(node->conn, \ cp->connect_timeout); if (*err_no != 0) { pthread_mutex_lock(&cm->lock); cm->total_count--; //rollback pthread_mutex_unlock(&cm->lock); free(p); return NULL; } logDebug("file: "__FILE__", line: %d, " \ "server %s:%d, new connection: %d, " \ "total_count: %d, free_count: %d", \ __LINE__, conn->ip_addr, conn->port, \ node->conn->sock, cm->total_count, \ cm->free_count); return node->conn; } else { node = cm->head; ci = node->conn; cm->head = node->next; cm->free_count--; if (current_time - node->atime > cp->max_idle_time) { cm->total_count--; logDebug("file: "__FILE__", line: %d, " \ "server %s:%d, connection: %d idle " \ "time: %d exceeds max idle time: %d, "\ "total_count: %d, free_count: %d", \ __LINE__, conn->ip_addr, conn->port, \ ci->sock, \ (int)(current_time - node->atime), \ cp->max_idle_time, cm->total_count, \ cm->free_count); conn_pool_disconnect_server(ci); free(node); continue; } pthread_mutex_unlock(&cm->lock); logDebug("file: "__FILE__", line: %d, " \ "server %s:%d, reuse connection: %d, " \ "total_count: %d, free_count: %d", __LINE__, conn->ip_addr, conn->port, ci->sock, cm->total_count, cm->free_count); return ci; } } }