- 论坛徽章:
- 0
|
本帖最后由 toniz 于 2014-06-05 11:35 编辑
这个问题已经决解。
我为了重用,把connect和disconnect部分单独拆成一个方法。
但其实TrackerServer不能在方法里面定义,要在使用的地方定义,作为参数带进去才行。
不然调用close后,这个链接会一直存在(实际上没有close这个socket)。
如果连接数满了之后,服务端会发起fin,这个时候会造成客户端大量close_wait.
正确代码如下:
- ConnectionInfo* FdfsClient::connectFastDFS(ConnectionInfo &trackerServer,
- ConnectionInfo &storageServer,
- int &store_path_index, char group_name[])
- {
- int result;
- ConnectionInfo *conn;
- conn = tracker_get_connection_r(&trackerServer, &result);
- if ( result != 0 )
- {
- MYLOG_WARN(logger, "tracker_get_connection fail, error no: %d, error info: %s\n", errno , STRERROR(errno));
- return NULL;
- }
- *group_name = '\0';
- store_path_index = 0;
- result = 0;
- if ((result=tracker_query_storage_store(conn, &storageServer, group_name, &store_path_index)) != 0)
- {
- fdfs_quit(conn);
- tracker_disconnect_server(conn);
- MYLOG_WARN(logger, "tracker_query_storage fail, error no: %d, error info: %s\n", result, STRERROR(result));
- return NULL;
- }
- return conn;
- }
复制代码 错误代码如下:- ConnectionInfo* FdfsClient::connectFastDFS(ConnectionInfo &storageServer, int &store_path_index, char group_name[])
- {
- int result;
- ConnectionInfo TrackerServer;
- ConnectionInfo *conn;
- conn = tracker_get_connection_r(&TrackerServer, &result);
- if ( result != 0 )
- {
- MYLOG_WARN(logger, "tracker_get_connection fail, error no: %d, error info: %s\n", errno , STRERROR(errno));
- return NULL;
- }
- *group_name = '\0';
- store_path_index = 0;
- result = 0;
- if ((result=tracker_query_storage_store(conn, &storageServer, group_name, &store_path_index)) != 0)
- {
- fdfs_quit(conn);
- tracker_disconnect_server(conn);
- MYLOG_WARN(logger, "tracker_query_storage fail, error no: %d, error info: %s\n", result, STRERROR(result));
- return NULL;
- }
- return conn;
- }
复制代码 调用的代码如下:- int FdfsClient::uploadAppenderByBuff(const char *file_content, int content_len, const char *file_ext_name, char file_id[])
- {
- int store_path_index;
- ConnectionInfo trackerServer;
- ConnectionInfo storageServer;
- char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
-
- int result;
- ConnectionInfo *pTrackerServer;
- pTrackerServer = connectFastDFS(trackerServer, storageServer, store_path_index, group_name);
-
- if( pTrackerServer == NULL )
- {
- return RETURN_GET_CONNECTION_FAIL;
- }
-
- result = storage_upload_appender_by_filebuff1(pTrackerServer, &storageServer, store_path_index, \
- file_content, content_len, file_ext_name, NULL, 0, group_name, file_id);
-
- if (result != 0)
- {
- MYLOG_WARN(logger, "upload file fail, error no: %d, error info: %s\n", result, STRERROR(result));
- disConnectFastDFS(pTrackerServer, storageServer);
- return RETURN_UPLOAD_APPENDER_FAIL;
- }
- disConnectFastDFS(pTrackerServer, storageServer);
- return RETURN_SUCCESS;
- }
复制代码 disconnect代码如下:- int FdfsClient::disConnectFastDFS(ConnectionInfo *pTrackerServer, ConnectionInfo &storageServer)
- {
- fdfs_quit(&storageServer);
- tracker_disconnect_server(&storageServer);
- fdfs_quit(pTrackerServer);
- tracker_disconnect_server(pTrackerServer);
- return RETURN_SUCCESS;
- }
复制代码 |
|