src/http/ngx_http_upstream_round_robin.h - nginx

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_HTTP_UPSTREAM_ROUND_ROBIN_H_INCLUDED_
  6. #define _NGX_HTTP_UPSTREAM_ROUND_ROBIN_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_http.h>


  10. #if (NGX_HTTP_UPSTREAM_SID)
  11. #define NGX_HTTP_UPSTREAM_SID_LEN    32  /* md5 in hex */
  12. #endif

  13. #define NGX_HTTP_UPSTREAM_FAILED     1

  14. #if (NGX_HTTP_UPSTREAM_STICKY)
  15. #define NGX_HTTP_UPSTREAM_DRAINING   8
  16. #endif


  17. typedef struct ngx_http_upstream_rr_peers_s  ngx_http_upstream_rr_peers_t;
  18. typedef struct ngx_http_upstream_rr_peer_s   ngx_http_upstream_rr_peer_t;


  19. #if (NGX_HTTP_UPSTREAM_ZONE)

  20. typedef struct {
  21.     ngx_event_t                     event;         /* must be first */
  22.     ngx_uint_t                      worker;
  23.     ngx_str_t                       name;
  24.     ngx_str_t                       service;
  25.     time_t                          valid;
  26.     ngx_http_upstream_rr_peers_t   *peers;
  27.     ngx_http_upstream_rr_peer_t    *peer;
  28. } ngx_http_upstream_host_t;

  29. #endif


  30. struct ngx_http_upstream_rr_peer_s {
  31.     struct sockaddr                *sockaddr;
  32.     socklen_t                       socklen;
  33.     ngx_str_t                       name;
  34.     ngx_str_t                       server;

  35.     ngx_int_t                       current_weight;
  36.     ngx_int_t                       effective_weight;
  37.     ngx_int_t                       weight;

  38.     ngx_uint_t                      conns;
  39.     ngx_uint_t                      max_conns;

  40.     ngx_uint_t                      fails;
  41.     time_t                          accessed;
  42.     time_t                          checked;

  43.     ngx_uint_t                      max_fails;
  44.     time_t                          fail_timeout;
  45.     ngx_msec_t                      slow_start;
  46.     ngx_msec_t                      start_time;

  47.     ngx_uint_t                      down;

  48. #if (NGX_HTTP_SSL || NGX_COMPAT)
  49.     void                           *ssl_session;
  50.     int                             ssl_session_len;
  51. #endif

  52. #if (NGX_HTTP_UPSTREAM_SID || NGX_COMPAT)
  53.     unsigned                        route:1;
  54. #endif

  55. #if (NGX_HTTP_UPSTREAM_ZONE)
  56.     unsigned                        zombie:1;

  57.     ngx_atomic_t                    lock;
  58.     ngx_uint_t                      refs;
  59.     ngx_http_upstream_host_t       *host;
  60. #endif

  61. #if (NGX_HTTP_UPSTREAM_SID || NGX_COMPAT)
  62.     ngx_str_t                       sid;
  63. #endif

  64.     ngx_http_upstream_rr_peer_t    *next;

  65. #if (NGX_HTTP_UPSTREAM_LEAST_TIME || NGX_COMPAT)
  66.     ngx_msec_t                      header_time;
  67.     ngx_msec_t                      response_time;
  68.     ngx_msec_t                      inflight_time;
  69.     ngx_msec_t                      inflight_last;
  70.     ngx_msec_t                      inflight_reqs_changed;
  71.     ngx_uint_t                      inflight_reqs;
  72. #endif

  73.     NGX_COMPAT_BEGIN(7)
  74.     NGX_COMPAT_END
  75. };


  76. struct ngx_http_upstream_rr_peers_s {
  77.     ngx_uint_t                      number;

  78. #if (NGX_HTTP_UPSTREAM_ZONE)
  79.     ngx_slab_pool_t                *shpool;
  80.     ngx_atomic_t                    rwlock;
  81.     ngx_uint_t                     *config;
  82.     ngx_http_upstream_rr_peer_t    *resolve;
  83.     ngx_http_upstream_rr_peers_t   *zone_next;
  84. #endif

  85.     ngx_uint_t                      total_weight;
  86.     ngx_uint_t                      tries;

  87.     unsigned                        single:1;
  88.     unsigned                        weighted:1;

  89.     ngx_str_t                      *name;

  90.     ngx_http_upstream_rr_peers_t   *next;

  91.     ngx_http_upstream_rr_peer_t    *peer;
  92. };


  93. #if (NGX_HTTP_UPSTREAM_ZONE)

  94. #define ngx_http_upstream_rr_peers_rlock(peers)                               \
  95.                                                                               \
  96.     if (peers->shpool) {                                                      \
  97.         ngx_rwlock_rlock(&peers->rwlock);                                     \
  98.     }

  99. #define ngx_http_upstream_rr_peers_wlock(peers)                               \
  100.                                                                               \
  101.     if (peers->shpool) {                                                      \
  102.         ngx_rwlock_wlock(&peers->rwlock);                                     \
  103.     }

  104. #define ngx_http_upstream_rr_peers_unlock(peers)                              \
  105.                                                                               \
  106.     if (peers->shpool) {                                                      \
  107.         ngx_rwlock_unlock(&peers->rwlock);                                    \
  108.     }


  109. #define ngx_http_upstream_rr_peer_lock(peers, peer)                           \
  110.                                                                               \
  111.     if (peers->shpool) {                                                      \
  112.         ngx_rwlock_wlock(&peer->lock);                                        \
  113.     }

  114. #define ngx_http_upstream_rr_peer_unlock(peers, peer)                         \
  115.                                                                               \
  116.     if (peers->shpool) {                                                      \
  117.         ngx_rwlock_unlock(&peer->lock);                                       \
  118.     }


  119. #define ngx_http_upstream_rr_peer_ref(peers, peer)                            \
  120.     (peer)->refs++;


  121. static ngx_inline void
  122. ngx_http_upstream_rr_peer_free_locked(ngx_http_upstream_rr_peers_t *peers,
  123.     ngx_http_upstream_rr_peer_t *peer)
  124. {
  125.     if (peer->refs) {
  126.         peer->zombie = 1;
  127.         return;
  128.     }

  129.     ngx_slab_free_locked(peers->shpool, peer->sockaddr);
  130.     ngx_slab_free_locked(peers->shpool, peer->name.data);
  131. #if (NGX_HTTP_UPSTREAM_SID)
  132.     ngx_slab_free_locked(peers->shpool, peer->sid.data);
  133. #endif

  134.     if (peer->server.data) {
  135.         ngx_slab_free_locked(peers->shpool, peer->server.data);
  136.     }

  137. #if (NGX_HTTP_SSL)
  138.     if (peer->ssl_session) {
  139.         ngx_slab_free_locked(peers->shpool, peer->ssl_session);
  140.     }
  141. #endif

  142.     ngx_slab_free_locked(peers->shpool, peer);
  143. }


  144. static ngx_inline void
  145. ngx_http_upstream_rr_peer_free(ngx_http_upstream_rr_peers_t *peers,
  146.     ngx_http_upstream_rr_peer_t *peer)
  147. {
  148.     ngx_shmtx_lock(&peers->shpool->mutex);
  149.     ngx_http_upstream_rr_peer_free_locked(peers, peer);
  150.     ngx_shmtx_unlock(&peers->shpool->mutex);
  151. }


  152. static ngx_inline ngx_int_t
  153. ngx_http_upstream_rr_peer_unref(ngx_http_upstream_rr_peers_t *peers,
  154.     ngx_http_upstream_rr_peer_t *peer)
  155. {
  156.     peer->refs--;

  157.     if (peers->shpool == NULL) {
  158.         return NGX_OK;
  159.     }

  160.     if (peer->refs == 0 && peer->zombie) {
  161.         ngx_http_upstream_rr_peer_free(peers, peer);
  162.         return NGX_DONE;
  163.     }

  164.     return NGX_OK;
  165. }

  166. #else

  167. #define ngx_http_upstream_rr_peers_rlock(peers)
  168. #define ngx_http_upstream_rr_peers_wlock(peers)
  169. #define ngx_http_upstream_rr_peers_unlock(peers)
  170. #define ngx_http_upstream_rr_peer_lock(peers, peer)
  171. #define ngx_http_upstream_rr_peer_unlock(peers, peer)
  172. #define ngx_http_upstream_rr_peer_ref(peers, peer)
  173. #define ngx_http_upstream_rr_peer_unref(peers, peer)  NGX_OK

  174. #endif


  175. typedef struct {
  176.     ngx_uint_t                      config;
  177.     ngx_http_upstream_rr_peers_t   *peers;
  178.     ngx_http_upstream_rr_peer_t    *current;
  179.     uintptr_t                      *tried;
  180.     uintptr_t                       data;
  181. } ngx_http_upstream_rr_peer_data_t;


  182. /* exponential moving average + rounding */
  183. #define ngx_http_upstream_response_time_avg(avg, v)                            \
  184.     *(avg) = (*(avg) ? (0.5 + ((double) (v) * 0.05 + (double) (*(avg)) * 0.95))\
  185.                      : (v))


  186. ngx_int_t ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
  187.     ngx_http_upstream_srv_conf_t *us);
  188. ngx_int_t ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r,
  189.     ngx_http_upstream_srv_conf_t *us);
  190. ngx_int_t ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
  191.     ngx_http_upstream_resolved_t *ur);
  192. ngx_int_t ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc,
  193.     void *data);
  194. void ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc,
  195.     void *data, ngx_uint_t state);
  196. void ngx_http_upstream_free_round_robin_peer_locked(ngx_peer_connection_t *pc,
  197.     void *data, ngx_uint_t state);

  198. #if (NGX_HTTP_SSL)
  199. ngx_int_t
  200.     ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc,
  201.     void *data);
  202. void ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc,
  203.     void *data);
  204. #endif

  205. #if (NGX_HTTP_UPSTREAM_SID)

  206. #define ngx_http_upstream_copy_round_robin_sid(dst, src)                      \
  207.     ngx_http_upstream_init_round_robin_sid(dst,                               \
  208.                                            (src)->route ? &(src)->sid : NULL)

  209. void ngx_http_upstream_init_round_robin_sid(ngx_http_upstream_rr_peer_t *peer,
  210.     ngx_str_t *route);
  211. ngx_http_upstream_rr_peer_t *ngx_http_upstream_get_rr_peer_by_sid(
  212.     ngx_http_upstream_rr_peer_data_t *rrp, ngx_str_t *hint, ngx_uint_t *p,
  213.     ngx_uint_t lock);

  214. #endif


  215. #endif /* _NGX_HTTP_UPSTREAM_ROUND_ROBIN_H_INCLUDED_ */