lattimore-liou / fastdfs

Automatically exported from code.google.com/p/fastdfs
GNU General Public License v3.0
0 stars 0 forks source link

连接池的bug,看description 的38行 #47

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
ConnectionInfo *conn_pool_get_connection(ConnectionPool *cp, 
    const ConnectionInfo *conn, int *err_no)
{
............
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(ConnectionInfo) + sizeof(ConnectionNode);
            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 + sizeof(ConnectionInfo));
            node->conn = (ConnectionInfo *)p;
            node->manager = cm;
            node->next = NULL;
            node->atime = 0;
  38                       cm->head = node //是不是应加上这行代码呢?
            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)
            {
                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(ci);
                continue;
            }

..............
}

Original issue reported on code.google.com by lichunfe...@gmail.com on 10 Apr 2013 at 2:34

GoogleCodeExporter commented 9 years ago
这个不是问题啊!
分配出去的连接,用完后才会加入到链表中。

Original comment by happyfis...@gmail.com on 19 Apr 2013 at 9:54

GoogleCodeExporter commented 9 years ago
我也认为这里有问题,,这里应该加上cm->head=node,要不然cm->he
ad永远都是空的。。不停的在分配node,并且       cm->head = 
node->next;
            cm->free_count--;  这里也应该去掉,在else分支。

Original comment by guowl0...@gmail.com on 16 Aug 2013 at 5:43