conncache.h 4.5 KB

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