README.multi_socket 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Implementation of the curl_multi_socket API
  2. The main ideas of the new API are simply:
  3. 1 - The application can use whatever event system it likes as it gets info
  4. from libcurl about what file descriptors libcurl waits for what action
  5. on. (The previous API returns fd_sets which is very select()-centric).
  6. 2 - When the application discovers action on a single socket, it calls
  7. libcurl and informs that there was action on this particular socket and
  8. libcurl can then act on that socket/transfer only and not care about
  9. any other transfers. (The previous API always had to scan through all
  10. the existing transfers.)
  11. The idea is that curl_multi_socket() calls a given callback with information
  12. about what socket to wait for what action on, and the callback only gets
  13. called if the status of that socket has changed.
  14. In the API draft from before, we have a timeout argument on a per socket
  15. basis and we also allowed curl_multi_socket() to pass in an 'easy handle'
  16. instead of socket to allow libcurl to shortcut a lookup and work on the
  17. affected easy handle right away. Both these turned out to be bad ideas.
  18. The timeout argument was removed from the socket callback since after much
  19. thinking I came to the conclusion that we really don't want to handle
  20. timeouts on a per socket basis. We need it on a per transfer (easy handle)
  21. basis and thus we can't provide it in the callbacks in a nice way. Instead,
  22. we have to offer a curl_multi_timeout() that returns the largest amount of
  23. time we should wait before we call the "timeout action" of libcurl, to
  24. trigger the proper internal timeout action on the affected transfer. To get
  25. this to work, I added a struct to each easy handle in which we store an
  26. "expire time" (if any). The structs are then "splay sorted" so that we can
  27. add and remove times from the linked list and yet somewhat swiftly figure
  28. out 1 - how long time there is until the next timer expires and 2 - which
  29. timer (handle) should we take care of now. Of course, the upside of all this
  30. is that we get a curl_multi_timeout() that should also work with old-style
  31. applications that use curl_multi_perform().
  32. We also added a timer callback that makes libcurl call the application when
  33. the timeout value changes, and you set that with curl_multi_setopt().
  34. We created an internal "socket to easy handles" hash table that given
  35. a socket (file descriptor) return the easy handle that waits for action on
  36. that socket. This hash is made using the already existing hash code
  37. (previously only used for the DNS cache).
  38. To make libcurl able to report plain sockets in the socket callback, we had
  39. to re-organize the internals of the curl_multi_fdset() etc so that the
  40. conversion from sockets to fd_sets for that function is only done in the
  41. last step before the data is returned. I also had to extend c-ares to get a
  42. function that can return plain sockets, as that library too returned only
  43. fd_sets and that is no longer good enough. The changes done to c-ares have
  44. been committed and are available in the c-ares CVS repository destined to be
  45. included in the c-ares 1.3.1 release.
  46. We have done a test runs with up to 9000 connections (with a single active
  47. one). The curl_multi_socket() invoke then takes less than 10 microseconds in
  48. average (using the read-only-1-byte-at-a-time hack). We are now below the
  49. 60 microseconds "per socket action" goal (the extra 50 is the time libevent
  50. needs).
  51. Status Right Now
  52. The curl_multi_socket() API is implemented according to how it is
  53. documented. We deem it ready to use.
  54. http://curl.haxx.se/libcurl/c/curl_multi_socket.html
  55. http://curl.haxx.se/libcurl/c/curl_multi_timeout.html
  56. http://curl.haxx.se/libcurl/c/curl_multi_setopt.html
  57. What is Left for the curl_multi_socket API
  58. Real world usage!