- 論壇徽章:
- 0
|
本帖最后由 toniz 于 2014-06-05 11:35 編輯
這個問題已經(jīng)決解。
我為了重用,把connect和disconnect部分單獨拆成一個方法。
但其實TrackerServer不能在方法里面定義,要在使用的地方定義,作為參數(shù)帶進去才行。
不然調(diào)用close后,這個鏈接會一直存在(實際上沒有close這個socket)。
如果連接數(shù)滿了之后,服務(wù)端會發(fā)起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;
- }
復(fù)制代碼 錯誤代碼如下:- 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;
- }
復(fù)制代碼 調(diào)用的代碼如下:- 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;
- }
復(fù)制代碼 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;
- }
復(fù)制代碼 |
|