免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2143 | 回复: 1
打印 上一主题 下一主题

[FastDFS] 求5.01版本tracker_get_connection_r的正确用法。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-04 18:48 |只看该作者 |倒序浏览
conn = tracker_get_connection_r(&TrackerServer, &result);

这个TrackerServer  和  conn 是啥关系, 关闭的时候要如何才能正确关闭连接。


论坛徽章:
0
2 [报告]
发表于 2014-06-05 11:33 |只看该作者
本帖最后由 toniz 于 2014-06-05 11:35 编辑

这个问题已经决解。

我为了重用,把connect和disconnect部分单独拆成一个方法。
但其实TrackerServer不能在方法里面定义,要在使用的地方定义,作为参数带进去才行。
不然调用close后,这个链接会一直存在(实际上没有close这个socket)。
如果连接数满了之后,服务端会发起fin,这个时候会造成客户端大量close_wait.

正确代码如下:

  1. ConnectionInfo* FdfsClient::connectFastDFS(ConnectionInfo &trackerServer,
  2.                                            ConnectionInfo &storageServer,
  3.                                            int &store_path_index, char group_name[])
  4. {
  5.     int result;
  6.     ConnectionInfo *conn;
  7.     conn = tracker_get_connection_r(&trackerServer, &result);
  8.     if ( result != 0 )
  9.     {   
  10.         MYLOG_WARN(logger, "tracker_get_connection fail, error no: %d, error info: %s\n", errno , STRERROR(errno));
  11.         return NULL;
  12.     }   

  13.     *group_name = '\0';
  14.     store_path_index = 0;
  15.     result = 0;
  16.     if ((result=tracker_query_storage_store(conn, &storageServer, group_name, &store_path_index)) != 0)
  17.     {   
  18.         fdfs_quit(conn);
  19.         tracker_disconnect_server(conn);
  20.         MYLOG_WARN(logger, "tracker_query_storage fail, error no: %d, error info: %s\n", result, STRERROR(result));
  21.         return NULL;
  22.     }   

  23.     return conn;
  24. }
复制代码
错误代码如下:
  1. ConnectionInfo* FdfsClient::connectFastDFS(ConnectionInfo &storageServer, int &store_path_index, char group_name[])
  2. {
  3.     int result;
  4.     ConnectionInfo TrackerServer;
  5.     ConnectionInfo *conn;
  6.     conn = tracker_get_connection_r(&TrackerServer, &result);
  7.     if ( result != 0 )
  8.     {
  9.         MYLOG_WARN(logger, "tracker_get_connection fail, error no: %d, error info: %s\n", errno , STRERROR(errno));
  10.         return NULL;
  11.     }

  12.     *group_name = '\0';
  13.     store_path_index = 0;
  14.     result = 0;
  15.     if ((result=tracker_query_storage_store(conn, &storageServer, group_name, &store_path_index)) != 0)
  16.     {
  17.         fdfs_quit(conn);
  18.         tracker_disconnect_server(conn);
  19.         MYLOG_WARN(logger, "tracker_query_storage fail, error no: %d, error info: %s\n", result, STRERROR(result));
  20.         return NULL;
  21.     }
  22.     return conn;
  23. }
复制代码
调用的代码如下:
  1. int FdfsClient::uploadAppenderByBuff(const char *file_content, int content_len, const char *file_ext_name, char file_id[])               
  2. {                                                                                                                                       
  3.     int store_path_index;                                                                                                               
  4.     ConnectionInfo trackerServer;                                                                                                        
  5.     ConnectionInfo storageServer;                                                                                                        
  6.     char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];                                                                                       
  7.                                                                                                                                          
  8.     int result;                                                                                                                          
  9.     ConnectionInfo *pTrackerServer;                                                                                                      
  10.     pTrackerServer = connectFastDFS(trackerServer, storageServer, store_path_index, group_name);                                         
  11.                                                                                                                                          
  12.     if( pTrackerServer == NULL )                                                                                                         
  13.     {                                                                                                                                    
  14.         return RETURN_GET_CONNECTION_FAIL;                                                                                               
  15.     }                                                                                                                                    
  16.                                                                                                                                          
  17.     result = storage_upload_appender_by_filebuff1(pTrackerServer, &storageServer, store_path_index, \                                    
  18.                             file_content, content_len, file_ext_name, NULL, 0, group_name, file_id);                                    
  19.                                                                                                                                          
  20.     if (result != 0)                                                                                                                     
  21.     {                                                                                                                                    
  22.         MYLOG_WARN(logger, "upload file fail, error no: %d, error info: %s\n", result, STRERROR(result));                                
  23.         disConnectFastDFS(pTrackerServer, storageServer);                                                                                
  24.         return RETURN_UPLOAD_APPENDER_FAIL;                                                                                             
  25.     }                                                                                                                                    
  26.     disConnectFastDFS(pTrackerServer, storageServer);                                                                                    
  27.     return RETURN_SUCCESS;                                                                                                               
  28. }
复制代码
disconnect代码如下:
  1. int FdfsClient::disConnectFastDFS(ConnectionInfo *pTrackerServer, ConnectionInfo &storageServer)
  2. {
  3.     fdfs_quit(&storageServer);
  4.     tracker_disconnect_server(&storageServer);
  5.     fdfs_quit(pTrackerServer);
  6.     tracker_disconnect_server(pTrackerServer);
  7.     return RETURN_SUCCESS;                                                                                                               
  8. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP