How to close a client connection using libevent library (on c ++) after receiving a response from the server so that the file descriptor used by the connection is released?

The scheme by which the request is sent:

struct evhttp_connection *conn; struct evhttp_request *req; conn = evhttp_connection_new(addr, port); evhttp_connection_set_timeout(conn, timeout); evhttp_connection_set_retries(conn, count_of_retry_sending); evhttp_connection_free_on_completion(conn); evhttp_connection_set_closecb(conn, callback_close_connection, nullptr); // привязка обработчика ответа к соединению req = evhttp_request_new(response_handler, static_cast<void*>(context)); // заполнение выходного буфера данными // ...... // отправка запроса evhttp_make_request(conn, req, EVHTTP_REQ_POST, resource); 

When viewing a call in the callback_close_connection under the debugger, I found that the connection was closed 5 minutes after the response from the server. It is not clear why the connection does not close immediately after receiving the answer.

  • one
    Because you can reuse it for acceleration. - Pavel Mayorov

1 answer 1

Found a solution. Removed the lines:

 evhttp_connection_free_on_completion(conn); evhttp_connection_set_retries(conn, count_of_retry_sending); 

A pointer to the connection passed to the function a response handler from the server, where it called upon completion of processing evhttp_connection_free(conn);