Skip to content

Commit f98c94a

Browse files
TELECOM-11880: Updating connect_only mechanism
1 parent b6ad1ea commit f98c94a

File tree

3 files changed

+115
-42
lines changed

3 files changed

+115
-42
lines changed

modules/rest_client/rest_methods.c

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,12 +1614,13 @@ int connect_only(preconnect_urls *precon_urls, int total_cons) {
16141614
CURLMcode mrc;
16151615
CURL *handle;
16161616
CURLM *multi_handle;
1617-
OSS_CURLM *multi_list = NULL;
1618-
struct CURLMsg *m;
1617+
curl_socket_t sockfd;
1618+
OSS_CURLM *multi_list;
1619+
CURLEasyHandles easy_handles = { 0, 0 };
16191620
preconnect_urls *start, *next;
16201621
char *url;
1621-
long busy_wait, timer;
1622-
int msgq, num_of_connections, exit_code = 0;
1622+
long busy_wait, timer, local_timer;
1623+
int num_of_connections = 0, exit_code = 0, open_sockets = 0;
16231624

16241625
if (!share_connections) {
16251626
exit_code = -1;
@@ -1662,10 +1663,21 @@ int connect_only(preconnect_urls *precon_urls, int total_cons) {
16621663
start = start->next;
16631664
}
16641665

1666+
if (num_of_connections == 0) {
1667+
goto error;
1668+
}
1669+
1670+
easy_handles.size = 0;
1671+
easy_handles.handles = pkg_malloc(sizeof(CURL*) * num_of_connections);
1672+
1673+
if (easy_handles.handles == NULL) {
1674+
goto error;
1675+
}
1676+
16651677
busy_wait = 100;
16661678

1667-
if (setsocket_callback(multi_handle) != 0) {
1668-
goto cleanup;
1679+
if (setsocket_callback_connect(multi_handle, &easy_handles) != 0) {
1680+
goto done;
16691681
}
16701682

16711683
w_curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, (long) num_of_connections);
@@ -1679,31 +1691,45 @@ int connect_only(preconnect_urls *precon_urls, int total_cons) {
16791691
goto cleanup;
16801692
}
16811693

1682-
LM_DBG("Creating warm pool connection, running handles %d\n", running_handles);
1694+
LM_INFO("Creating warm pool connection, running handles %d\n", running_handles);
1695+
1696+
local_timer = 0;
16831697

16841698
do {
16851699
running_handles = run_multi_socket(multi_handle);
16861700

1687-
if (timer < 0) {
1701+
if (timer < 0 || local_timer >= curl_timeout) {
16881702
break;
16891703
}
16901704

1705+
local_timer += busy_wait;
16911706
usleep(1000UL * busy_wait);
1692-
LM_DBG("Creating warm pool connection, running handles %d\n", running_handles);
16931707
} while (running_sockets());
1694-
16951708
cleanup:
1696-
do {
1697-
m = curl_multi_info_read(multi_handle, &msgq);
1698-
if (m && m->msg == CURLMSG_DONE) {
1699-
mrc = curl_multi_remove_handle(multi_handle, m->easy_handle);
1700-
if (mrc != CURLM_OK) {
1701-
LM_ERR("curl_multi_remove_handle: %s\n", curl_multi_strerror(mrc));
1702-
}
1703-
curl_easy_cleanup(m->easy_handle);
1709+
LM_INFO("Finishing warm pool connection, open sockets %d\n", easy_handles.size);
1710+
1711+
if (running_sockets()) {
1712+
running_handles = end_multi_socket(multi_handle);
1713+
}
1714+
1715+
for (int i = 0; i < easy_handles.size; i++) {
1716+
curl_easy_getinfo(easy_handles.handles[i], CURLINFO_ACTIVESOCKET, &sockfd);
1717+
1718+
if (sockfd != CURL_SOCKET_BAD) {
1719+
open_sockets += 1;
17041720
}
1705-
} while (m);
17061721

1722+
mrc = curl_multi_remove_handle(multi_handle, easy_handles.handles[i]);
1723+
1724+
if (mrc != CURLM_OK) {
1725+
LM_ERR("curl_multi_remove_handle: %s\n", curl_multi_strerror(mrc));
1726+
}
1727+
1728+
curl_easy_cleanup(easy_handles.handles[i]);
1729+
}
1730+
1731+
LM_INFO("Finishing warm pool connection, open sockets %d\n", open_sockets);
1732+
error:
17071733
start = precon_urls;
17081734

17091735
while (start != NULL) {
@@ -1715,6 +1741,10 @@ int connect_only(preconnect_urls *precon_urls, int total_cons) {
17151741
start = next;
17161742
}
17171743

1744+
if (easy_handles.handles != NULL) {
1745+
pkg_free(easy_handles.handles);
1746+
}
1747+
17181748
put_multi(multi_list);
17191749

17201750
done:
@@ -1796,7 +1826,7 @@ int start_async_http_req_v2(struct sip_msg *msg, enum rest_client_method method,
17961826
multi_handle = multi_list->multi_handle;
17971827
curl_multi_add_handle(multi_handle, handle);
17981828

1799-
if (setsocket_callback(multi_handle) != 0) {
1829+
if (setsocket_callback_request(multi_handle) != 0) {
18001830
goto cleanup;
18011831
}
18021832

modules/rest_client/rest_sockets.c

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,66 @@ static void remove_sock(int s) {
120120
}
121121
}
122122

123-
static int socket_action_cb(CURL *e, curl_socket_t s, int event, void *cbp, void *sockp)
124-
{
123+
static int socket_action_connect(CURL *e, curl_socket_t s, int event, void *cbp, void *sockp) {
125124
LM_DBG("called for socket %d status %d\n", s, event);
126125

126+
CURLEasyHandles *easy_handles = (CURLEasyHandles*) cbp;
127127
if (event != CURL_POLL_REMOVE) {
128128
add_sock(s);
129129
} else if (event == CURL_POLL_REMOVE) {
130130
remove_sock(s);
131+
132+
LM_DBG("Adding handle for socket %d current size %d\n", s, easy_handles->size);
133+
easy_handles->handles[easy_handles->size] = e;
134+
easy_handles->size += 1;
131135
}
132136

133137
return 0;
134138
}
135139

136-
int start_multi_socket(CURLM *multi_handle) {
140+
static int socket_action_http(CURL *e, curl_socket_t s, int event, void *cbp, void *sockp) {
141+
LM_DBG("called for socket %d status %d\n", s, event);
142+
143+
if (event != CURL_POLL_REMOVE) {
144+
add_sock(s);
145+
} else if (event == CURL_POLL_REMOVE) {
146+
remove_sock(s);
147+
}
148+
149+
return 0;
150+
}
151+
152+
int setsocket_callback_connect(CURLM *multi_handle, CURLEasyHandles *easy_handles) {
153+
CURLMcode mrc;
154+
155+
mrc = curl_multi_setopt(multi_handle, CURLMOPT_SOCKETFUNCTION, socket_action_connect);
156+
if (mrc != CURLM_OK) {
157+
LM_ERR("curl_multi_setopt(%d): (%s)\n", CURLMOPT_SOCKETFUNCTION, curl_multi_strerror(mrc));
158+
return -1;
159+
}
160+
161+
mrc = curl_multi_setopt(multi_handle, CURLMOPT_SOCKETDATA, easy_handles);
162+
if (mrc != CURLM_OK) {
163+
LM_ERR("curl_multi_setopt(%d): (%s)\n", CURLMOPT_SOCKETFUNCTION, curl_multi_strerror(mrc));
164+
return -1;
165+
}
166+
167+
return 0;
168+
}
169+
170+
int setsocket_callback_request(CURLM *multi_handle) {
171+
CURLMcode mrc;
172+
173+
mrc = curl_multi_setopt(multi_handle, CURLMOPT_SOCKETFUNCTION, socket_action_http);
174+
if (mrc != CURLM_OK) {
175+
LM_ERR("curl_multi_setopt(%d): (%s)\n", CURLMOPT_SOCKETFUNCTION, curl_multi_strerror(mrc));
176+
return -1;
177+
}
178+
179+
return 0;
180+
}
181+
182+
static int run_all_multi_socket(CURLM *multi_handle, int ev_bitmask) {
137183
CURLMcode mrc;
138184
int running;
139185

@@ -149,6 +195,14 @@ int start_multi_socket(CURLM *multi_handle) {
149195
return running;
150196
}
151197

198+
int start_multi_socket(CURLM *multi_handle) {
199+
return run_all_multi_socket(multi_handle, CURL_SOCKET_TIMEOUT);
200+
}
201+
202+
int end_multi_socket(CURLM *multi_handle) {
203+
return run_all_multi_socket(multi_handle, CURL_POLL_REMOVE);
204+
}
205+
152206
int run_multi_socket(CURLM *multi_handle) {
153207
CURLMcode mrc;
154208
cpuword_t sockets;
@@ -173,21 +227,3 @@ int run_multi_socket(CURLM *multi_handle) {
173227

174228
return running;
175229
}
176-
177-
int setsocket_callback(CURLM *multi_handle) {
178-
CURLMcode mrc;
179-
180-
mrc = curl_multi_setopt(multi_handle, CURLMOPT_SOCKETFUNCTION, socket_action_cb);
181-
if (mrc != CURLM_OK) {
182-
LM_ERR("curl_multi_setopt(%d): (%s)\n", CURLMOPT_SOCKETFUNCTION, curl_multi_strerror(mrc));
183-
return -1;
184-
}
185-
186-
mrc = curl_multi_setopt(multi_handle, CURLMOPT_SOCKETDATA, &fds);
187-
if (mrc != CURLM_OK) {
188-
LM_ERR("curl_multi_setopt(%d): (%s)\n", CURLMOPT_SOCKETFUNCTION, curl_multi_strerror(mrc));
189-
return -1;
190-
}
191-
192-
return 0;
193-
}

modules/rest_client/rest_sockets.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@
2626

2727
#include <sys/resource.h>
2828

29+
typedef struct _curl_easy_handles {
30+
int size;
31+
CURL **handles;
32+
} CURLEasyHandles;
33+
2934
int init_process_limits(rlim_t rlim_cur);
3035
int get_max_fd(int no_max_default);
3136
int running_sockets(void);
3237
int start_multi_socket(CURLM *multi_handle);
38+
int end_multi_socket(CURLM *multi_handle);
3339
int run_multi_socket(CURLM *multi_handle);
34-
int setsocket_callback(CURLM *multi_handle);
40+
int setsocket_callback_request(CURLM *multi_handle);
41+
int setsocket_callback_connect(CURLM *multi_handle, CURLEasyHandles *easy_handles);
3542

3643
#endif /* _REST_SOCKET_H_ */

0 commit comments

Comments
 (0)