conncache.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * @return combined result of last conn_db and result_cb or FALSE if no
  104. connections were present.
  105. */
  106. bool Curl_cpool_find(struct Curl_easy *data,
  107. const char *destination, size_t dest_len,
  108. Curl_cpool_conn_match_cb *conn_cb,
  109. Curl_cpool_done_match_cb *done_cb,
  110. void *userdata);
  111. /*
  112. * A connection (already in the pool) is now idle. Do any
  113. * cleanups in regard to the pool's limits.
  114. *
  115. * Return TRUE if idle connection kept in pool, FALSE if closed.
  116. */
  117. bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
  118. struct connectdata *conn);
  119. /**
  120. * Remove the connection from the pool and tear it down.
  121. * If `aborted` is FALSE, the connection will be shut down first
  122. * before closing and destroying it.
  123. * If the shutdown is not immediately complete, the connection
  124. * will be placed into the pool's shutdown queue.
  125. */
  126. void Curl_cpool_disconnect(struct Curl_easy *data,
  127. struct connectdata *conn,
  128. bool aborted);
  129. /**
  130. * This function scans the data's connection pool for half-open/dead
  131. * connections, closes and removes them.
  132. * The cleanup is done at most once per second.
  133. *
  134. * When called, this transfer has no connection attached.
  135. */
  136. void Curl_cpool_prune_dead(struct Curl_easy *data);
  137. /**
  138. * Perform upkeep actions on connections in the transfer's pool.
  139. */
  140. CURLcode Curl_cpool_upkeep(void *data);
  141. typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,
  142. struct Curl_easy *data,
  143. void *cbdata);
  144. /**
  145. * Invoke the callback on the pool's connection with the
  146. * given connection id (if it exists).
  147. */
  148. void Curl_cpool_do_by_id(struct Curl_easy *data,
  149. curl_off_t conn_id,
  150. Curl_cpool_conn_do_cb *cb, void *cbdata);
  151. /**
  152. * Invoked the callback for the given data + connection under the
  153. * connection pool's lock.
  154. * The callback is always invoked, even if the transfer has no connection
  155. * pool associated.
  156. */
  157. void Curl_cpool_do_locked(struct Curl_easy *data,
  158. struct connectdata *conn,
  159. Curl_cpool_conn_do_cb *cb, void *cbdata);
  160. /**
  161. * Add sockets and POLLIN/OUT flags for connections handled by the pool.
  162. */
  163. CURLcode Curl_cpool_add_pollfds(struct cpool *connc,
  164. struct curl_pollfds *cpfds);
  165. CURLcode Curl_cpool_add_waitfds(struct cpool *connc,
  166. struct curl_waitfds *cwfds);
  167. /**
  168. * Perform maintenance on connections in the pool. Specifically,
  169. * progress the shutdown of connections in the queue.
  170. */
  171. void Curl_cpool_multi_perform(struct Curl_multi *multi);
  172. void Curl_cpool_multi_socket(struct Curl_multi *multi,
  173. curl_socket_t s, int ev_bitmask);
  174. #endif /* HEADER_CURL_CONNCACHE_H */