You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.1 KiB

  1. From 76a06b2804bcdba0fb2c19f834bdb511ce3cf344 Mon Sep 17 00:00:00 2001
  2. From: Willy Tarreau <w@1wt.eu>
  3. Date: Wed, 20 May 2015 10:39:04 +0200
  4. Subject: [PATCH 09/10] BUG/MEDIUM: peers: apply a random reconnection timeout
  5. Commit 9ff95bb ("BUG/MEDIUM: peers: correctly configure the client timeout")
  6. uncovered an old bug in the peers : upon disconnect, we reconnect immediately.
  7. This sometimes results in both ends to do the same thing in parallel causing
  8. a loop of connect/accept/close/close that can last several seconds. The risk
  9. of occurrence of the trouble increases with latency, and is emphasized by the
  10. fact that idle connections are now frequently recycled (after 5s of idle).
  11. In order to avoid this we must apply a random delay before reconnecting.
  12. Fortunately the mechanism already supports a reconnect delay, so here we
  13. compute the random timeout when killing a session. The delay is 50ms plus
  14. a random between 0 and 2 seconds. Ideally an exponential back-off would
  15. be preferred but it's preferable to keep the fix simple.
  16. This bug was reported by Marco Corte.
  17. This fix must be backported to 1.5 since the fix above was backported into
  18. 1.5.12.
  19. (cherry picked from commit b4e34da692d8a7f6837ad16b3389f5830dbc11d2)
  20. ---
  21. src/peers.c | 14 +++++++++++---
  22. 1 file changed, 11 insertions(+), 3 deletions(-)
  23. diff --git a/src/peers.c b/src/peers.c
  24. index b196d88..159f0a4 100644
  25. --- a/src/peers.c
  26. +++ b/src/peers.c
  27. @@ -1063,6 +1063,7 @@ static void peer_session_forceshutdown(struct session * session)
  28. {
  29. struct stream_interface *oldsi = NULL;
  30. struct appctx *appctx = NULL;
  31. + struct peer_session *ps;
  32. int i;
  33. for (i = 0; i <= 1; i++) {
  34. @@ -1079,6 +1080,14 @@ static void peer_session_forceshutdown(struct session * session)
  35. if (!appctx)
  36. return;
  37. + ps = (struct peer_session *)appctx->ctx.peers.ptr;
  38. + /* we're killing a connection, we must apply a random delay before
  39. + * retrying otherwise the other end will do the same and we can loop
  40. + * for a while.
  41. + */
  42. + if (ps)
  43. + ps->reconnect = tick_add(now_ms, MS_TO_TICKS(50 + random() % 2000));
  44. +
  45. /* call release to reinit resync states if needed */
  46. peer_session_release(oldsi);
  47. appctx->st0 = PEER_SESS_ST_END;
  48. @@ -1352,8 +1361,8 @@ static struct task *process_peer_sync(struct task * task)
  49. if (!ps->session) {
  50. /* no active session */
  51. if (ps->statuscode == 0 ||
  52. - ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
  53. ((ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
  54. + ps->statuscode == PEER_SESS_SC_SUCCESSCODE ||
  55. ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) &&
  56. tick_is_expired(ps->reconnect, now_ms))) {
  57. /* connection never tried
  58. @@ -1364,8 +1373,7 @@ static struct task *process_peer_sync(struct task * task)
  59. /* retry a connect */
  60. ps->session = peer_session_create(ps->peer, ps);
  61. }
  62. - else if (ps->statuscode == PEER_SESS_SC_CONNECTCODE ||
  63. - ps->statuscode == PEER_SESS_SC_CONNECTEDCODE) {
  64. + else if (!tick_is_expired(ps->reconnect, now_ms)) {
  65. /* If previous session failed during connection
  66. * but reconnection timer is not expired */
  67. --
  68. 2.0.5