conncache.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, <[email protected]>, et al.
  11. * Copyright (C) Linus Nielsen Feltzing, <[email protected]>
  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 "curlx/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. * Terminate the connection, e.g. close and destroy.
  37. * If the connection is in a cpool, remove it.
  38. * If a `cshutdn` is available (e.g. data has a multi handle),
  39. * pass the connection to that for controlled shutdown.
  40. * Otherwise terminate it right away.
  41. * Takes ownership of `conn`.
  42. * `data` should not be attached to a connection.
  43. */
  44. void Curl_conn_terminate(struct Curl_easy *data,
  45. struct connectdata *conn,
  46. bool aborted);
  47. struct cpool {
  48. /* the pooled connections, bundled per destination */
  49. struct Curl_hash dest2bundle;
  50. size_t num_conn;
  51. curl_off_t next_connection_id;
  52. curl_off_t next_easy_id;
  53. struct curltime last_cleanup;
  54. struct Curl_easy *idata; /* internal handle for maintenance */
  55. struct Curl_share *share; /* != NULL if pool belongs to share */
  56. BIT(locked);
  57. BIT(initialised);
  58. };
  59. /* Init the pool, pass multi only if pool is owned by it.
  60. * Cannot fail.
  61. */
  62. void Curl_cpool_init(struct cpool *cpool,
  63. struct Curl_easy *idata,
  64. struct Curl_share *share,
  65. size_t size);
  66. /* Destroy all connections and free all members */
  67. void Curl_cpool_destroy(struct cpool *connc);
  68. /* Init the transfer to be used within its connection pool.
  69. * Assigns `data->id`. */
  70. void Curl_cpool_xfer_init(struct Curl_easy *data);
  71. /* Get the connection with the given id from `data`'s conn pool. */
  72. struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
  73. curl_off_t conn_id);
  74. /* Add the connection to the pool. */
  75. CURLcode Curl_cpool_add(struct Curl_easy *data,
  76. struct connectdata *conn) WARN_UNUSED_RESULT;
  77. /**
  78. * Return if the pool has reached its configured limits for adding
  79. * the given connection. Will try to discard the oldest, idle
  80. * connections to make space.
  81. */
  82. #define CPOOL_LIMIT_OK 0
  83. #define CPOOL_LIMIT_DEST 1
  84. #define CPOOL_LIMIT_TOTAL 2
  85. int Curl_cpool_check_limits(struct Curl_easy *data,
  86. struct connectdata *conn);
  87. /* Return of conn is suitable. If so, stops iteration. */
  88. typedef bool Curl_cpool_conn_match_cb(struct connectdata *conn,
  89. void *userdata);
  90. /* Act on the result of the find, may override it. */
  91. typedef bool Curl_cpool_done_match_cb(bool result, void *userdata);
  92. /**
  93. * Find a connection in the pool matching `destination`.
  94. * All callbacks are invoked while the pool's lock is held.
  95. * @param data current transfer
  96. * @param destination match agaonst `conn->destination` in pool
  97. * @param conn_cb must be present, called for each connection in the
  98. * bundle until it returns TRUE
  99. * @return combined result of last conn_db and result_cb or FALSE if no
  100. connections were present.
  101. */
  102. bool Curl_cpool_find(struct Curl_easy *data,
  103. const char *destination,
  104. Curl_cpool_conn_match_cb *conn_cb,
  105. Curl_cpool_done_match_cb *done_cb,
  106. void *userdata);
  107. /*
  108. * A connection (already in the pool) is now idle. Do any
  109. * cleanups in regard to the pool's limits.
  110. *
  111. * Return TRUE if idle connection kept in pool, FALSE if closed.
  112. */
  113. bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
  114. struct connectdata *conn);
  115. /**
  116. * This function scans the data's connection pool for half-open/dead
  117. * connections, closes and removes them.
  118. * The cleanup is done at most once per second.
  119. *
  120. * When called, this transfer has no connection attached.
  121. */
  122. void Curl_cpool_prune_dead(struct Curl_easy *data);
  123. /**
  124. * Perform upkeep actions on connections in the transfer's pool.
  125. */
  126. CURLcode Curl_cpool_upkeep(void *data);
  127. typedef void Curl_cpool_conn_do_cb(struct connectdata *conn,
  128. struct Curl_easy *data,
  129. void *cbdata);
  130. /**
  131. * Invoke the callback on the pool's connection with the
  132. * given connection id (if it exists).
  133. */
  134. void Curl_cpool_do_by_id(struct Curl_easy *data,
  135. curl_off_t conn_id,
  136. Curl_cpool_conn_do_cb *cb, void *cbdata);
  137. /**
  138. * Invoked the callback for the given data + connection under the
  139. * connection pool's lock.
  140. * The callback is always invoked, even if the transfer has no connection
  141. * pool associated.
  142. */
  143. void Curl_cpool_do_locked(struct Curl_easy *data,
  144. struct connectdata *conn,
  145. Curl_cpool_conn_do_cb *cb, void *cbdata);
  146. #endif /* HEADER_CURL_CONNCACHE_H */