mongo php sdk 阅读笔记(连接)

来源:互联网 发布:ubuntu配置tftp服务器 编辑:程序博客网 时间:2024/06/14 19:30

1 将servers的地址,选项解析出来后,具体的连接函数为mongo_get_connection_single(mcon/manager.c)

2 如果最近连接失败过的机器会加入黑名单,在短时间内不会去重试这台机器(默认是5秒)

3 每次取到连接后会ping一次,如果失败将连接置空,也不会重连(逻辑不太好,应该重连)

static mongo_connection *mongo_get_connection_single(mongo_con_manager *manager, mongo_server_def *server, mongo_server_options *options, int connection_flags, char **error_message){    char *hash;    mongo_connection *con = NULL;    mongo_connection_blacklist *blacklist = NULL;    hash = mongo_server_create_hash(server);    /* See if a connection is in our blacklist to short-circut trying to     * connect to a node that is known to be down. This is done so we don't     * waste precious time in connecting to unreachable nodes */    blacklist = mongo_manager_blacklist_find_by_hash(manager, hash);    if (blacklist) {        struct timeval start;        /* It is blacklisted, but it may have been a long time again and         * chances are we should give it another try */        if (mongo_connection_ping_check(manager, blacklist->last_ping, &start)) {            /* The connection is blacklisted, but we've reached our ping             * interval so lets remove the blacklisting and pretend we didn't             * know about it */            mongo_manager_blacklist_deregister(manager, blacklist, hash);            con = NULL;        } else {            /* Otherwise short-circut the connection attempt, and say we failed             * right away */            free(hash);            *error_message = strdup("Previous connection attempts failed, server blacklisted");            return NULL;        }    }    con = mongo_manager_connection_find_by_hash(manager, hash);    /* If we aren't about to (re-)connect then all we care about if it was a     * known connection or not */    if (connection_flags & MONGO_CON_FLAG_DONT_CONNECT) {        free(hash);        return con;    }    /* If we found a valid connection check if we need to ping it */    if (con) {        /* Do the ping, if needed */        if (!mongo_connection_ping(manager,  con, options, error_message)) {            /* If the ping failed, deregister the connection */            mongo_manager_connection_deregister(manager, con);            /* Set the return value to NULL, as the connection is broken and             * has been removed */            con = NULL;        }
        //free(hash);        //return con;
        //此处官方代码为注释部分,但有个问题就是:当mongos重启后,每条长连接在第一次查询时都会出现异常,因为ping会失败,但在此处没有进行重连,应该加上重连机制
if (con != NULL) { free(hash); return con; } } /* Since we didn't find an existing connection, lets make one! */ con = mongo_connection_create(manager, hash, server, options, error_message); if (con) { /* Do authentication if requested */ if (server->db && server->username && server->password) { mongo_manager_log(manager, MLOG_CON, MLOG_INFO, "get_connection_single: authenticating %s", hash); if (!authenticate_connection(manager, con, options, server->authdb ? server->authdb : server->db, server->username, server->password, error_message)) { mongo_connection_destroy(manager, con, MONGO_CLOSE_BROKEN); free(hash); return NULL; } } /* Do the first-time ping to record the latency of the connection */ if (mongo_connection_ping(manager, con, options, error_message)) { /* Register the connection on successful pinging */ mongo_manager_connection_register(manager, con); } else { /* Or kill it and reset the return value if the ping somehow failed */ mongo_connection_destroy(manager, con, MONGO_CLOSE_BROKEN); con = NULL; } } free(hash); return con;}


原创粉丝点击