conncache.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /*
  28. * All accesses to struct fields and changing of data in the connection cache
  29. * and connectbundles must be done with the conncache LOCKED. The cache might
  30. * be shared.
  31. */
  32. #include <curl/curl.h>
  33. #include "timeval.h"
  34. struct connectdata;
  35. struct curl_pollfds;
  36. struct curl_waitfds;
  37. struct Curl_multi;
  38. struct connshutdowns {
  39. struct Curl_llist conn_list; /* The connectdata to shut down */
  40. BIT(iter_locked); /* TRUE while iterating the list */
  41. };
  42. struct conncache {
  43. struct Curl_hash hash;
  44. size_t num_conn;
  45. curl_off_t next_connection_id;
  46. curl_off_t next_easy_id;
  47. struct curltime last_cleanup;
  48. struct connshutdowns shutdowns;
  49. /* handle used for closing cached connections */
  50. struct Curl_easy *closure_handle;
  51. struct Curl_multi *multi; /* Optional, set if cache belongs to multi */
  52. };
  53. #define BUNDLE_NO_MULTIUSE -1
  54. #define BUNDLE_UNKNOWN 0 /* initial value */
  55. #define BUNDLE_MULTIPLEX 2
  56. #ifdef DEBUGBUILD
  57. /* the debug versions of these macros make extra certain that the lock is
  58. never doubly locked or unlocked */
  59. #define CONNCACHE_LOCK(x) \
  60. do { \
  61. if((x)->share) { \
  62. Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, \
  63. CURL_LOCK_ACCESS_SINGLE); \
  64. DEBUGASSERT(!(x)->state.conncache_lock); \
  65. (x)->state.conncache_lock = TRUE; \
  66. } \
  67. } while(0)
  68. #define CONNCACHE_UNLOCK(x) \
  69. do { \
  70. if((x)->share) { \
  71. DEBUGASSERT((x)->state.conncache_lock); \
  72. (x)->state.conncache_lock = FALSE; \
  73. Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT); \
  74. } \
  75. } while(0)
  76. #else
  77. #define CONNCACHE_LOCK(x) if((x)->share) \
  78. Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
  79. #define CONNCACHE_UNLOCK(x) if((x)->share) \
  80. Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
  81. #endif
  82. struct connectbundle {
  83. int multiuse; /* supports multi-use */
  84. size_t num_connections; /* Number of connections in the bundle */
  85. struct Curl_llist conn_list; /* The connectdata members of the bundle */
  86. };
  87. /* Init the cache, pass multi only if cache is owned by it.
  88. * returns 1 on error, 0 is fine.
  89. */
  90. int Curl_conncache_init(struct conncache *,
  91. struct Curl_multi *multi,
  92. size_t size);
  93. void Curl_conncache_destroy(struct conncache *connc);
  94. /* return the correct bundle, to a host or a proxy */
  95. struct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
  96. struct connectdata *conn,
  97. struct conncache *connc);
  98. /* returns number of connections currently held in the connection cache */
  99. size_t Curl_conncache_size(struct Curl_easy *data);
  100. bool Curl_conncache_return_conn(struct Curl_easy *data,
  101. struct connectdata *conn);
  102. CURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
  103. void Curl_conncache_remove_conn(struct Curl_easy *data,
  104. struct connectdata *conn,
  105. bool lock);
  106. bool Curl_conncache_foreach(struct Curl_easy *data,
  107. struct conncache *connc,
  108. void *param,
  109. int (*func)(struct Curl_easy *data,
  110. struct connectdata *conn,
  111. void *param));
  112. struct connectdata *
  113. Curl_conncache_find_first_connection(struct conncache *connc);
  114. struct connectdata *
  115. Curl_conncache_extract_bundle(struct Curl_easy *data,
  116. struct connectbundle *bundle);
  117. struct connectdata *
  118. Curl_conncache_extract_oldest(struct Curl_easy *data);
  119. void Curl_conncache_close_all_connections(struct conncache *connc);
  120. void Curl_conncache_print(struct conncache *connc);
  121. /**
  122. * Tear down the connection. If `aborted` is FALSE, the connection
  123. * will be shut down first before discarding. If the shutdown
  124. * is not immediately complete, the connection
  125. * will be placed into the cache is shutdown queue.
  126. */
  127. void Curl_conncache_disconnect(struct Curl_easy *data,
  128. struct connectdata *conn,
  129. bool aborted);
  130. /**
  131. * Add sockets and POLLIN/OUT flags for connections handled by the cache.
  132. */
  133. CURLcode Curl_conncache_add_pollfds(struct conncache *connc,
  134. struct curl_pollfds *cpfds);
  135. CURLcode Curl_conncache_add_waitfds(struct conncache *connc,
  136. struct curl_waitfds *cwfds);
  137. /**
  138. * Perform maintenance on connections in the cache. Specifically,
  139. * progress the shutdown of connections in the queue.
  140. */
  141. void Curl_conncache_multi_perform(struct Curl_multi *multi);
  142. void Curl_conncache_multi_socket(struct Curl_multi *multi,
  143. curl_socket_t s, int ev_bitmask);
  144. void Curl_conncache_multi_close_all(struct Curl_multi *multi);
  145. #endif /* HEADER_CURL_CONNCACHE_H */