conncache.h 5.1 KB

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