client.conf add config item: connect_first_by

pull/687/head
YuQing 2023-12-11 19:20:32 +08:00
parent fd77ababf5
commit ba41d958b2
5 changed files with 91 additions and 3 deletions

View File

@ -376,6 +376,7 @@ static int fdfs_client_do_init_ex(TrackerServerGroup *pTrackerGroup, \
{
FDFSStorageIdInfo *idInfo;
FDFSStorageIdInfo *end;
char *connect_first_by;
end = g_storage_ids_by_id.ids + g_storage_ids_by_id.count;
for (idInfo=g_storage_ids_by_id.ids; idInfo<end; idInfo++)
@ -386,6 +387,17 @@ static int fdfs_client_do_init_ex(TrackerServerGroup *pTrackerGroup, \
break;
}
}
if (g_multi_storage_ips)
{
connect_first_by = iniGetStrValue(NULL,
"connect_first_by", iniContext);
if (connect_first_by != NULL && strncasecmp(connect_first_by,
"last", 4) == 0)
{
g_connect_first_by = fdfs_connect_first_by_last_connected;
}
}
}
#ifdef DEBUG_FLAG
@ -397,13 +409,16 @@ static int fdfs_client_do_init_ex(TrackerServerGroup *pTrackerGroup, \
"anti_steal_secret_key length=%d, "
"use_connection_pool=%d, "
"g_connection_pool_max_idle_time=%ds, "
"use_storage_id=%d, storage server id count: %d, "
"use_storage_id=%d, connect_first_by=%s, "
"storage server id count: %d, "
"multi storage ips: %d\n",
SF_G_BASE_PATH_STR, SF_G_CONNECT_TIMEOUT,
SF_G_NETWORK_TIMEOUT, pTrackerGroup->server_count,
g_anti_steal_token, g_anti_steal_secret_key.length,
g_use_connection_pool, g_connection_pool_max_idle_time,
use_storage_id, g_storage_ids_by_id.count, g_multi_storage_ips);
use_storage_id, g_connect_first_by == fdfs_connect_first_by_tracker ?
"tracker" : "last-connected", g_storage_ids_by_id.count,
g_multi_storage_ips);
#endif
return 0;

View File

@ -14,5 +14,6 @@ int g_tracker_server_http_port = 80;
TrackerServerGroup g_tracker_group = {0, 0, -1, NULL};
bool g_multi_storage_ips = false;
FDFSConnectFirstBy g_connect_first_by = fdfs_connect_first_by_tracker;
bool g_anti_steal_token = false;
BufferInfo g_anti_steal_secret_key = {0};

View File

@ -15,6 +15,11 @@
#include "tracker_types.h"
#include "fdfs_shared_func.h"
typedef enum {
fdfs_connect_first_by_tracker,
fdfs_connect_first_by_last_connected
} FDFSConnectFirstBy;
#ifdef __cplusplus
extern "C" {
#endif
@ -23,6 +28,7 @@ extern int g_tracker_server_http_port;
extern TrackerServerGroup g_tracker_group;
extern bool g_multi_storage_ips;
extern FDFSConnectFirstBy g_connect_first_by;
extern bool g_anti_steal_token;
extern BufferInfo g_anti_steal_secret_key;

View File

@ -64,7 +64,7 @@ static int g_base64_context_inited = 0;
ppStorageServer, TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE, \
group_name, filename, pNewStorage, new_connection)
static ConnectionInfo *storage_make_connection(
static ConnectionInfo *make_connection_by_tracker(
ConnectionInfo *pStorageServer, int *err_no)
{
ConnectionInfo *conn;
@ -102,6 +102,65 @@ static ConnectionInfo *storage_make_connection(
return tracker_make_connection(pStorageServer, err_no);
}
static ConnectionInfo *make_connection_by_last_connected(
ConnectionInfo *pStorageServer, int *err_no)
{
ConnectionInfo *conn;
FDFSStorageIdInfo *idInfo;
int index;
if (!g_multi_storage_ips)
{
return tracker_make_connection(pStorageServer, err_no);
}
if ((idInfo=fdfs_get_storage_id_by_ip_port(pStorageServer->ip_addr,
pStorageServer->port)) == NULL)
{
return tracker_make_connection(pStorageServer, err_no);
}
if (idInfo->ip_addrs.count < 2)
{
return tracker_make_connection(pStorageServer, err_no);
}
index = idInfo->ip_addrs.index;
if (strcmp(pStorageServer->ip_addr, idInfo->ip_addrs.
ips[index].address) != 0)
{
strcpy(pStorageServer->ip_addr, idInfo->ip_addrs.
ips[index].address);
}
if ((conn=tracker_make_connection(pStorageServer, err_no)) != NULL)
{
return conn;
}
if (++index == idInfo->ip_addrs.count)
{
index = 0;
}
strcpy(pStorageServer->ip_addr, idInfo->ip_addrs.ips[index].address);
if ((conn=tracker_make_connection(pStorageServer, err_no)) != NULL)
{
idInfo->ip_addrs.index = index;
}
return conn;
}
static inline ConnectionInfo *storage_make_connection(
ConnectionInfo *pStorageServer, int *err_no)
{
if (g_connect_first_by == fdfs_connect_first_by_tracker)
{
return make_connection_by_tracker(pStorageServer, err_no);
}
else
{
return make_connection_by_last_connected(pStorageServer, err_no);
}
}
static int storage_get_connection(ConnectionInfo *pTrackerServer, \
ConnectionInfo **ppStorageServer, const byte cmd, \
const char *group_name, const char *filename, \

View File

@ -37,6 +37,13 @@ tracker_server = 192.168.0.197:22122
### debug
log_level = info
# connect which ip address first for multi IPs of a storage server, value list:
## tracker: connect to the ip address return by tracker server first
## last-connected: connect to the ip address last connected first
# default value is tracker
# since V6.11
connect_first_by = tracker
# if use connection pool
# default value is false
# since V4.05