// 进程退出前清理资源 static void end(void) { // 销毁线程池 acl_pthread_pool_destroy(__thr_pool); // 打印所有连接池集群的存活状态 printf("\r\n"); check_all_connections(); printf("\r\n>>> STOPPING check thread now\r\n"); #if 0 int i = 0; while (i++ < 10) { sleep(1); printf("----------- sleep %d seconds -----------\r\n", i); } #endif // 停止后台检测线程 acl::connect_monitor* monitor = __conn_manager->stop_monitor(true); // 删除检测器对象 delete monitor; // 销毁连接池 delete __conn_manager; }
// 子线程处理过程 static void thread_main(void*) { for (int i = 0; i < __loop_count; i++) { connect_pool* pool = (connect_pool*) __conn_manager->peek(); if (pool == NULL) { printf("\r\n>>>%lu(%d): peek pool failed<<<\r\n", (unsigned long) acl_pthread_self(), __LINE__); check_all_connections(); exit (1); } // 设置连接的超时时间及读超时时间 pool->set_timeout(2, 2); // 从连接池中获取一个连接 connect_client* conn = (connect_client*) pool->peek(); if (conn == NULL) { printf("\r\n>>>%lu: peek connect failed from %s<<<\r\n", (unsigned long) acl_pthread_self(), pool->get_addr()); check_all_connections(); exit (1); } // 需要对获得的连接重置状态,以清除上次请求过程的临时数据 else conn->reset(); // 开始新的 HTTP 请求过程 if (get(conn, i) == false) { printf("one request failed, close connection\r\n"); // 错误连接需要关闭 pool->put(conn, false); } else pool->put(conn, true); } printf(">>>>thread: %lu OVER<<<<\r\n", (unsigned long) acl_pthread_self()); }
// 进程退出前清理资源 static void end(void) { // 销毁线程池 acl_pthread_pool_destroy(__thr_pool); // 打印所有连接池集群的存活状态 printf("\r\n"); check_all_connections(); printf("\r\n>>> STOPPING check thread now\r\n"); // 停止后台检测线程 __conn_manager->stop_monitor(true); // 销毁连接池 delete __conn_manager; }