2
0

conncache.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef HEADER_CURL_CONNCACHE_H
  2. #define HEADER_CURL_CONNCACHE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. * Copyright (C) Linus Nielsen Feltzing, <linus@haxx.se>
  12. *
  13. * This software is licensed as described in the file COPYING, which
  14. * you should have received as part of this distribution. The terms
  15. * are also available at https://curl.se/docs/copyright.html.
  16. *
  17. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  18. * copies of the Software, and permit persons to whom the Software is
  19. * furnished to do so, under the terms of the COPYING file.
  20. *
  21. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  22. * KIND, either express or implied.
  23. *
  24. * SPDX-License-Identifier: curl
  25. *
  26. ***************************************************************************/
  27. #include <curl/curl.h>
  28. #include "timeval.h"
  29. struct connectdata;
  30. struct Curl_easy;
  31. struct curl_pollfds;
  32. struct curl_waitfds;
  33. struct Curl_multi;
  34. struct Curl_share;
  35. /**
  36. * Callback invoked when disconnecting connections.
  37. * @param data transfer last handling the connection, not attached
  38. * @param conn the connection to discard
  39. * @param aborted if the connection is being aborted
  40. * @return if the connection is being aborted, e.g. should NOT perform
  41. * a shutdown and just close.
  42. **/
  43. typedef bool Curl_cpool_disconnect_cb(struct Curl_easy *data,
  44. struct connectdata *conn,
  45. bool aborted);
  46. struct cpool {
  47. /* the pooled connections, bundled per destination */
  48. struct Curl_hash dest2bundle;
  49. size_t num_conn;
  50. curl_off_t next_connection_id;
  51. curl_off_t next_easy_id;
  52. struct curltime last_cleanup;
  53. struct Curl_llist shutdowns; /* The connections being shut down */
  54. struct Curl_easy *idata; /* internal handle used for discard */
  55. struct Curl_multi *multi; /* != NULL iff pool belongs to multi */
  56. struct Curl_share *share; /* != NULL iff pool belongs to share */
  57. Curl_cpool_disconnect_cb *disconnect_cb;
  58. BIT(locked);
  59. };
  60. /* Init the pool, pass multi only if pool is owned by it.
  61. * returns 1 on error, 0 is fine.
  62. */
  63. int Curl_cpool_init(struct cpool *cpool,
  64. Curl_cpool_disconnect_cb *disconnect_cb,
  65. struct Curl_multi *multi,
  66. struct Curl_share *share,
  67. size_t size);
  68. /* Destroy all connections and free all members */
  69. void Curl_cpool_destroy(struct cpool *connc);
  70. /* Init the transfer to be used within its connection pool.
  71. * Assigns `data->id`. */
  72. void Curl_cpool_xfer_init(struct Curl_easy *data);
  73. /**
  74. * Get the connection with the given id from the transfer's pool.
  75. */
  76. struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
  77. curl_off_t conn_id);
  78. CURLcode Curl_cpool_add_conn(struct Curl_easy *data,
  79. struct connectdata *conn) WARN_UNUSED_RESULT;
  80. /**
  81. * Return if the pool has reached its configured limits for adding
  82. * the given connection. Will try to discard the oldest, idle
  83. * connections to make space.
  84. */
  85. #define CPOOL_LIMIT_OK 0
  86. #define CPOOL_LIMIT_DEST 1
  87. #define CPOOL_LIMIT_TOTAL 2
  88. int Curl_cpool_check_limits(struct Curl_easy *data,
  89. struct connectdata *conn);
  90. /* Return of conn is suitable. If so, stops iteration. */
  91. typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,
  92. void *userdata);
  93. /* Act on the result of the find, may override it. */
  94. typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);
  95. /**
  96. * Find a connection in the pool matching `destination`.
  97. * All callbacks are invoked while the pool's lock is held.
  98. * @param data current transfer
  99. * @param destination match agaonst `conn->destination` in pool
  100. * @param dest_len destination length, including terminating NUL
  101. * @param conn_cb must be present, called for each connection in the
  102. * bundle until it returns TRUE
  103. * @param result_cb if not NULL, is called at the end with the result
  104. * of the `conn_cb` or FALSE if never called.
  105. * @return combined result of last conn_db and result_cb or FALSE if no
  106. connections were present.
  107. */
  108. bool Curl_cpool_find(struct Curl_easy *data,
  109. const char *destination, size_t dest_len,
  110. Curl_cpool_conn_match_cb *conn_cb,
  111. Curl_cpool_done_match_cb *done_cb,
  112. void *userdata);
  113. /*
  114. * A connection (already in the pool) is now idle. Do any
  115. * cleanups in regard to the pool's limits.
  116. *
  117. * Return TRUE if idle connection kept in pool, FALSE if closed.
  118. */
  119. bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
  120. struct connectdata *conn);
  121. /**
  122. * Remove the connection from the pool and tear it down.
  123. * If `aborted` is FALSE, the connection will be shut down first
  124. * before closing and destroying it.
  125. * If the shutdown is not immediately complete, the connection
  126. * will be placed into the pool's shutdown queue.
  127. */
  128. void Curl_cpool_disconnect(struct Curl_easy *data,
  129. struct connectdata *conn,
  130. bool aborted);
  131. /**
  132. * This function scans the data's connection pool for half-open/dead
  133. * connections, closes and removes them.
  134. * The cleanup is done at most once per second.
  135. *
  136. * When called, this transfer has no connection attached.
  137. */
  138. void Curl_cpool_prune_dead(struct Curl_easy *data);
  139. /**
  140. * Perform upkeep actions on connections in the transfer's pool.
  141. */
  142. CURLcode Curl_cpool_upkeep(void *data);
  143. typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,
  144. struct Curl_easy *data,
  145. void *cbdata);
  146. /**
  147. * Invoke the callback on the pool's connection with the
  148. * given connection id (if it exists).
  149. */
  150. void Curl_cpool_do_by_id(struct Curl_easy *data,
  151. curl_off_t conn_id,
  152. Curl_cpool_conn_do_cb *cb, void *cbdata);
  153. /**
  154. * Invoked the callback for the given data + connection under the
  155. * connection pool's lock.
  156. * The callback is always invoked, even if the transfer has no connection
  157. * pool associated.
  158. */
  159. void Curl_cpool_do_locked(struct Curl_easy *data,
  160. struct connectdata *conn,
  161. Curl_cpool_conn_do_cb *cb, void *cbdata);
  162. /**
  163. * Add sockets and POLLIN/OUT flags for connections handled by the pool.
  164. */
  165. CURLcode Curl_cpool_add_pollfds(struct cpool *connc,
  166. struct curl_pollfds *cpfds);
  167. CURLcode Curl_cpool_add_waitfds(struct cpool *connc,
  168. struct curl_waitfds *cwfds);
  169. /**
  170. * Perform maintenance on connections in the pool. Specifically,
  171. * progress the shutdown of connections in the queue.
  172. */
  173. void Curl_cpool_multi_perform(struct Curl_multi *multi);
  174. void Curl_cpool_multi_socket(struct Curl_multi *multi,
  175. curl_socket_t s, int ev_bitmask);
  176. #endif /* HEADER_CURL_CONNCACHE_H */