src/stream/ngx_stream_upstream_round_robin.h - nginx-1.31.3 nginx/ @ 42f8df65b

Data types defined

Functions defined

Macros defined

Source code


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


  5. #ifndef _NGX_STREAM_UPSTREAM_ROUND_ROBIN_H_INCLUDED_
  6. #define _NGX_STREAM_UPSTREAM_ROUND_ROBIN_H_INCLUDED_


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


  10. #define NGX_STREAM_UPSTREAM_FAILED      1


  11. typedef struct ngx_stream_upstream_rr_peers_s  ngx_stream_upstream_rr_peers_t;
  12. typedef struct ngx_stream_upstream_rr_peer_s   ngx_stream_upstream_rr_peer_t;


  13. #if (NGX_STREAM_UPSTREAM_ZONE)

  14. typedef struct {
  15.     ngx_event_t                      event;         /* must be first */
  16.     ngx_uint_t                       worker;
  17.     ngx_str_t                        name;
  18.     ngx_str_t                        service;
  19.     time_t                           valid;
  20.     ngx_stream_upstream_rr_peers_t  *peers;
  21.     ngx_stream_upstream_rr_peer_t   *peer;
  22. } ngx_stream_upstream_host_t;

  23. #endif


  24. struct ngx_stream_upstream_rr_peer_s {
  25.     struct sockaddr                 *sockaddr;
  26.     socklen_t                        socklen;
  27.     ngx_str_t                        name;
  28.     ngx_str_t                        server;

  29.     ngx_int_t                        current_weight;
  30.     ngx_int_t                        effective_weight;
  31.     ngx_int_t                        weight;

  32.     ngx_uint_t                       conns;
  33.     ngx_uint_t                       max_conns;

  34.     ngx_uint_t                       fails;
  35.     time_t                           accessed;
  36.     time_t                           checked;

  37.     ngx_uint_t                       max_fails;
  38.     time_t                           fail_timeout;
  39.     ngx_msec_t                       slow_start;
  40.     ngx_msec_t                       start_time;

  41.     ngx_uint_t                       down;

  42.     void                            *ssl_session;
  43.     int                              ssl_session_len;

  44. #if (NGX_STREAM_UPSTREAM_ZONE)
  45.     unsigned                         zombie:1;

  46.     ngx_atomic_t                     lock;
  47.     ngx_uint_t                       refs;
  48.     ngx_stream_upstream_host_t      *host;
  49. #endif

  50.     ngx_stream_upstream_rr_peer_t   *next;

  51. #if (NGX_STREAM_UPSTREAM_LEAST_TIME || NGX_COMPAT)
  52.     ngx_msec_t                       connect_time;
  53.     ngx_msec_t                       first_byte_time;
  54.     ngx_msec_t                       response_time;
  55.     ngx_msec_t                       inflight_time;
  56.     ngx_msec_t                       inflight_last;
  57.     ngx_msec_t                       inflight_reqs_changed;
  58.     ngx_uint_t                       inflight_reqs;
  59. #endif

  60.     NGX_COMPAT_BEGIN(7)
  61.     NGX_COMPAT_END
  62. };


  63. struct ngx_stream_upstream_rr_peers_s {
  64.     ngx_uint_t                       number;

  65. #if (NGX_STREAM_UPSTREAM_ZONE)
  66.     ngx_slab_pool_t                 *shpool;
  67.     ngx_atomic_t                     rwlock;
  68.     ngx_uint_t                      *config;
  69.     ngx_stream_upstream_rr_peer_t   *resolve;
  70.     ngx_stream_upstream_rr_peers_t  *zone_next;
  71. #endif

  72.     ngx_uint_t                       total_weight;
  73.     ngx_uint_t                       tries;

  74.     unsigned                         single:1;
  75.     unsigned                         weighted:1;

  76.     ngx_str_t                       *name;

  77.     ngx_stream_upstream_rr_peers_t  *next;

  78.     ngx_stream_upstream_rr_peer_t   *peer;
  79. };


  80. #if (NGX_STREAM_UPSTREAM_ZONE)

  81. #define ngx_stream_upstream_rr_peers_rlock(peers)                             \
  82.                                                                               \
  83.     if (peers->shpool) {                                                      \
  84.         ngx_rwlock_rlock(&peers->rwlock);                                     \
  85.     }

  86. #define ngx_stream_upstream_rr_peers_wlock(peers)                             \
  87.                                                                               \
  88.     if (peers->shpool) {                                                      \
  89.         ngx_rwlock_wlock(&peers->rwlock);                                     \
  90.     }

  91. #define ngx_stream_upstream_rr_peers_unlock(peers)                            \
  92.                                                                               \
  93.     if (peers->shpool) {                                                      \
  94.         ngx_rwlock_unlock(&peers->rwlock);                                    \
  95.     }


  96. #define ngx_stream_upstream_rr_peer_lock(peers, peer)                         \
  97.                                                                               \
  98.     if (peers->shpool) {                                                      \
  99.         ngx_rwlock_wlock(&peer->lock);                                        \
  100.     }

  101. #define ngx_stream_upstream_rr_peer_unlock(peers, peer)                       \
  102.                                                                               \
  103.     if (peers->shpool) {                                                      \
  104.         ngx_rwlock_unlock(&peer->lock);                                       \
  105.     }


  106. #define ngx_stream_upstream_rr_peer_ref(peers, peer)                          \
  107.     (peer)->refs++;


  108. static ngx_inline void
  109. ngx_stream_upstream_rr_peer_free_locked(ngx_stream_upstream_rr_peers_t *peers,
  110.     ngx_stream_upstream_rr_peer_t *peer)
  111. {
  112.     if (peer->refs) {
  113.         peer->zombie = 1;
  114.         return;
  115.     }

  116.     ngx_slab_free_locked(peers->shpool, peer->sockaddr);
  117.     ngx_slab_free_locked(peers->shpool, peer->name.data);

  118.     if (peer->server.data) {
  119.         ngx_slab_free_locked(peers->shpool, peer->server.data);
  120.     }

  121. #if (NGX_STREAM_SSL)
  122.     if (peer->ssl_session) {
  123.         ngx_slab_free_locked(peers->shpool, peer->ssl_session);
  124.     }
  125. #endif

  126.     ngx_slab_free_locked(peers->shpool, peer);
  127. }


  128. static ngx_inline void
  129. ngx_stream_upstream_rr_peer_free(ngx_stream_upstream_rr_peers_t *peers,
  130.     ngx_stream_upstream_rr_peer_t *peer)
  131. {
  132.     ngx_shmtx_lock(&peers->shpool->mutex);
  133.     ngx_stream_upstream_rr_peer_free_locked(peers, peer);
  134.     ngx_shmtx_unlock(&peers->shpool->mutex);
  135. }


  136. static ngx_inline ngx_int_t
  137. ngx_stream_upstream_rr_peer_unref(ngx_stream_upstream_rr_peers_t *peers,
  138.     ngx_stream_upstream_rr_peer_t *peer)
  139. {
  140.     peer->refs--;

  141.     if (peers->shpool == NULL) {
  142.         return NGX_OK;
  143.     }

  144.     if (peer->refs == 0 && peer->zombie) {
  145.         ngx_stream_upstream_rr_peer_free(peers, peer);
  146.         return NGX_DONE;
  147.     }

  148.     return NGX_OK;
  149. }

  150. #else

  151. #define ngx_stream_upstream_rr_peers_rlock(peers)
  152. #define ngx_stream_upstream_rr_peers_wlock(peers)
  153. #define ngx_stream_upstream_rr_peers_unlock(peers)
  154. #define ngx_stream_upstream_rr_peer_lock(peers, peer)
  155. #define ngx_stream_upstream_rr_peer_unlock(peers, peer)
  156. #define ngx_stream_upstream_rr_peer_ref(peers, peer)
  157. #define ngx_stream_upstream_rr_peer_unref(peers, peer)  NGX_OK

  158. #endif


  159. typedef struct {
  160.     ngx_uint_t                       config;
  161.     ngx_stream_upstream_rr_peers_t  *peers;
  162.     ngx_stream_upstream_rr_peer_t   *current;
  163.     uintptr_t                       *tried;
  164.     uintptr_t                        data;
  165. } ngx_stream_upstream_rr_peer_data_t;


  166. /* exponential moving average + rounding */
  167. #define ngx_stream_upstream_response_time_avg(avg, v)                          \
  168.     *(avg) = (*(avg) ? (0.5 + ((double) (v) * 0.05 + (double) (*(avg)) * 0.95))\
  169.                      : (v))


  170. ngx_int_t ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
  171.     ngx_stream_upstream_srv_conf_t *us);
  172. ngx_int_t ngx_stream_upstream_init_round_robin_peer(ngx_stream_session_t *s,
  173.     ngx_stream_upstream_srv_conf_t *us);
  174. ngx_int_t ngx_stream_upstream_create_round_robin_peer(ngx_stream_session_t *s,
  175.     ngx_stream_upstream_resolved_t *ur);
  176. ngx_int_t ngx_stream_upstream_get_round_robin_peer(ngx_peer_connection_t *pc,
  177.     void *data);
  178. void ngx_stream_upstream_free_round_robin_peer(ngx_peer_connection_t *pc,
  179.     void *data, ngx_uint_t state);
  180. void ngx_stream_upstream_free_round_robin_peer_locked(ngx_peer_connection_t *pc,
  181.     void *data, ngx_uint_t state);
  182. void ngx_stream_upstream_notify_round_robin_peer_locked(
  183.     ngx_peer_connection_t *pc, void *data, ngx_uint_t type);


  184. #endif /* _NGX_STREAM_UPSTREAM_ROUND_ROBIN_H_INCLUDED_ */