Skip to content

Commit

Permalink
优化curl句柄复用 (#57)
Browse files Browse the repository at this point in the history
Do not reuse the curl handle when the return value is not CURLE_OK.
  • Loading branch information
Huberyxiao authored Jun 20, 2024
1 parent a6fdaa6 commit 50eaf40
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
21 changes: 21 additions & 0 deletions cos_c_sdk/cos_http_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ void request_release(CURL *request)
}
}

void request_release2(cos_curl_http_transport_t* t)
{
CURL* request = t->curl;
CURLcode code = t->curl_code;
apr_thread_mutex_lock(requestStackMutexG);

// If the request stack is full, destroy this one
// else put this one at the front of the request stack; we do this because
// we want the most-recently-used curl handle to be re-used on the next
// request, to maximize our chances of re-using a TCP connection before it
// times out
if (requestStackCountG == COS_REQUEST_STACK_SIZE || code != CURLE_OK) {
apr_thread_mutex_unlock(requestStackMutexG);
curl_easy_cleanup(request);
}
else {
requestStackG[requestStackCountG++] = request;
apr_thread_mutex_unlock(requestStackMutexG);
}
}

void cos_set_default_request_options(cos_http_request_options_t *op)
{
cos_default_http_request_options = op;
Expand Down
1 change: 1 addition & 0 deletions cos_c_sdk/cos_http_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static APR_INLINE const char *cos_http_controller_get_reason(cos_http_controller

CURL *cos_request_get();
void request_release(CURL *request);
void request_release2(cos_curl_http_transport_t* t);

int cos_http_io_initialize(const char *user_agent_info, int flag);
void cos_http_io_deinitialize();
Expand Down
2 changes: 1 addition & 1 deletion cos_c_sdk/cos_sys_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ typedef apr_array_header_t cos_array_header_t;
#define CR (char) 13
#define CRLF "\x0d\x0a"

#define COS_VERSION "5.0.17"
#define COS_VERSION "5.0.18"
#define COS_VER "cos-sdk-c/" COS_VERSION

#define COS_HTTP_PREFIX "http://"
Expand Down
7 changes: 4 additions & 3 deletions cos_c_sdk/cos_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ cos_http_transport_t *cos_curl_http_transport_create(cos_pool_t *p)
cos_fstack_push(t->cleanup, t, func, 1);

t->curl = cos_request_get();
func.func1 = (cos_func1_pt)request_release;
cos_fstack_push(t->cleanup, t->curl, func, 1);
func.func1 = (cos_func1_pt)request_release2;
cos_fstack_push(t->cleanup, t, func, 1);

t->header_callback = cos_curl_default_header_callback;
t->read_callback = cos_curl_default_read_callback;
Expand Down Expand Up @@ -487,7 +487,8 @@ int cos_curl_http_transport_perform(cos_http_transport_t *t_)
code = curl_easy_perform(t->curl);
t->controller->finish_time = apr_time_now();
cos_move_transport_state(t, TRANS_STATE_DONE);


t->curl_code = code;
if ((code != CURLE_OK) && (t->controller->error_code == COSE_OK)) {
ecode = cos_curl_code_to_status(code);
if (ecode != COSE_OK) {
Expand Down
1 change: 1 addition & 0 deletions cos_c_sdk/cos_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ struct cos_http_transport_s {
struct cos_curl_http_transport_s {
COS_HTTP_BASE_TRANSPORT_DEFINE
CURL *curl;
CURLcode curl_code;
char *url;
struct curl_slist *headers;
curl_read_callback header_callback;
Expand Down

0 comments on commit 50eaf40

Please sign in to comment.