conncache.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef HEADER_CURL_CONNCACHE_H
  2. #define HEADER_CURL_CONNCACHE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 2015 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  11. * Copyright (C) 2012 - 2014, 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 conncache {
  36. struct Curl_hash hash;
  37. size_t num_conn;
  38. long next_connection_id;
  39. struct curltime last_cleanup;
  40. /* handle used for closing cached connections */
  41. struct Curl_easy *closure_handle;
  42. };
  43. #define BUNDLE_NO_MULTIUSE -1
  44. #define BUNDLE_UNKNOWN 0 /* initial value */
  45. #define BUNDLE_MULTIPLEX 2
  46. #ifdef CURLDEBUG
  47. /* the debug versions of these macros make extra certain that the lock is
  48. never doubly locked or unlocked */
  49. #define CONNCACHE_LOCK(x) \
  50. do { \
  51. if((x)->share) { \
  52. Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, \
  53. CURL_LOCK_ACCESS_SINGLE); \
  54. DEBUGASSERT(!(x)->state.conncache_lock); \
  55. (x)->state.conncache_lock = TRUE; \
  56. } \
  57. } while(0)
  58. #define CONNCACHE_UNLOCK(x) \
  59. do { \
  60. if((x)->share) { \
  61. DEBUGASSERT((x)->state.conncache_lock); \
  62. (x)->state.conncache_lock = FALSE; \
  63. Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT); \
  64. } \
  65. } while(0)
  66. #else
  67. #define CONNCACHE_LOCK(x) if((x)->share) \
  68. Curl_share_lock((x), CURL_LOCK_DATA_CONNECT, CURL_LOCK_ACCESS_SINGLE)
  69. #define CONNCACHE_UNLOCK(x) if((x)->share) \
  70. Curl_share_unlock((x), CURL_LOCK_DATA_CONNECT)
  71. #endif
  72. struct connectbundle {
  73. int multiuse; /* supports multi-use */
  74. size_t num_connections; /* Number of connections in the bundle */
  75. struct Curl_llist conn_list; /* The connectdata members of the bundle */
  76. };
  77. /* returns 1 on error, 0 is fine */
  78. int Curl_conncache_init(struct conncache *, int size);
  79. void Curl_conncache_destroy(struct conncache *connc);
  80. /* return the correct bundle, to a host or a proxy */
  81. struct connectbundle *Curl_conncache_find_bundle(struct Curl_easy *data,
  82. struct connectdata *conn,
  83. struct conncache *connc);
  84. /* returns number of connections currently held in the connection cache */
  85. size_t Curl_conncache_size(struct Curl_easy *data);
  86. bool Curl_conncache_return_conn(struct Curl_easy *data,
  87. struct connectdata *conn);
  88. CURLcode Curl_conncache_add_conn(struct Curl_easy *data) WARN_UNUSED_RESULT;
  89. void Curl_conncache_remove_conn(struct Curl_easy *data,
  90. struct connectdata *conn,
  91. bool lock);
  92. bool Curl_conncache_foreach(struct Curl_easy *data,
  93. struct conncache *connc,
  94. void *param,
  95. int (*func)(struct Curl_easy *data,
  96. struct connectdata *conn,
  97. void *param));
  98. struct connectdata *
  99. Curl_conncache_find_first_connection(struct conncache *connc);
  100. struct connectdata *
  101. Curl_conncache_extract_bundle(struct Curl_easy *data,
  102. struct connectbundle *bundle);
  103. struct connectdata *
  104. Curl_conncache_extract_oldest(struct Curl_easy *data);
  105. void Curl_conncache_close_all_connections(struct conncache *connc);
  106. void Curl_conncache_print(struct conncache *connc);
  107. #endif /* HEADER_CURL_CONNCACHE_H */