conncache.h 5.0 KB

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