summaryrefslogtreecommitdiffhomepage
path: root/src/http/modules/ngx_http_upstream_random_module.c
diff options
context:
space:
mode:
authorVladimir Homutov <vl@nginx.com>2013-04-03 01:44:36 +0400
committerAleksei Bavshin <a.bavshin@f5.com>2026-03-09 11:08:30 -0600
commit104734f21888cfec6994e092073f51a0d4b0fb47 (patch)
tree7b63045b3921ee8ae2cacd5e6a79c7be829d507b /src/http/modules/ngx_http_upstream_random_module.c
parentdff46cd1ae0095922e7eb9cf5b32ebe1e68a5706 (diff)
downloadnginx-104734f21888cfec6994e092073f51a0d4b0fb47.tar.gz
nginx-104734f21888cfec6994e092073f51a0d4b0fb47.tar.bz2
Upstream: added sticky sessions support for upstreams.
Sticky sessions allow to route the same client to the same upstream server. - upstream structures are extended to keep session-related information - existing balancing modules are updated to provide an id of the selected server (SID) in pc->sid, and to select the server, given it's SID. - other balancing modules are allowed to set the pc->hint value to choose the desired peer. The sticky module will not change the hint if it's already set. - the feature is enabled by default and can be disabled with the "--without-http_upstream_sticky" switch of the configure script. The following configuration can be used to enable sticky sessions for supported balancing modules: upstream u1 { server 127.0.0.1:8080; server 127.0.0.1:8081; sticky cookie server_id expires=1h domain=.example.com path=/; } Co-authored-by: Ruslan Ermilov <ru@nginx.com> Co-authored-by: Roman Arutyunyan <arut@nginx.com> Co-authored-by: Maxim Dounin <mdounin@mdounin.ru>
Diffstat (limited to 'src/http/modules/ngx_http_upstream_random_module.c')
-rw-r--r--src/http/modules/ngx_http_upstream_random_module.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/http/modules/ngx_http_upstream_random_module.c b/src/http/modules/ngx_http_upstream_random_module.c
index 74714874b..72885cde8 100644
--- a/src/http/modules/ngx_http_upstream_random_module.c
+++ b/src/http/modules/ngx_http_upstream_random_module.c
@@ -249,6 +249,17 @@ ngx_http_upstream_get_random_peer(ngx_peer_connection_t *pc, void *data)
now = ngx_time();
+#if (NGX_HTTP_UPSTREAM_SID)
+ peer = ngx_http_upstream_get_rr_peer_by_sid(rrp, pc->hint, &i, 1);
+
+ if (peer) {
+ n = i / (8 * sizeof(uintptr_t));
+ m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
+
+ goto found;
+ }
+#endif
+
for ( ;; ) {
i = ngx_http_upstream_peek_random_peer(peers, rp);
@@ -292,6 +303,10 @@ ngx_http_upstream_get_random_peer(ngx_peer_connection_t *pc, void *data)
}
}
+#if (NGX_HTTP_UPSTREAM_SID)
+found:
+#endif
+
rrp->current = peer;
ngx_http_upstream_rr_peer_ref(peers, peer);
@@ -303,6 +318,10 @@ ngx_http_upstream_get_random_peer(ngx_peer_connection_t *pc, void *data)
pc->socklen = peer->socklen;
pc->name = &peer->name;
+#if (NGX_HTTP_UPSTREAM_SID)
+ pc->sid = &peer->sid;
+#endif
+
peer->conns++;
ngx_http_upstream_rr_peer_unlock(peers, peer);
@@ -357,6 +376,17 @@ ngx_http_upstream_get_random2_peer(ngx_peer_connection_t *pc, void *data)
p = 0;
#endif
+#if (NGX_HTTP_UPSTREAM_SID)
+ peer = ngx_http_upstream_get_rr_peer_by_sid(rrp, pc->hint, &i, 0);
+
+ if (peer) {
+ n = i / (8 * sizeof(uintptr_t));
+ m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
+
+ goto found;
+ }
+#endif
+
for ( ;; ) {
i = ngx_http_upstream_peek_random_peer(peers, rp);
@@ -410,6 +440,10 @@ ngx_http_upstream_get_random2_peer(ngx_peer_connection_t *pc, void *data)
}
}
+#if (NGX_HTTP_UPSTREAM_SID)
+found:
+#endif
+
rrp->current = peer;
ngx_http_upstream_rr_peer_ref(peers, peer);
@@ -421,6 +455,10 @@ ngx_http_upstream_get_random2_peer(ngx_peer_connection_t *pc, void *data)
pc->socklen = peer->socklen;
pc->name = &peer->name;
+#if (NGX_HTTP_UPSTREAM_SID)
+ pc->sid = &peer->sid;
+#endif
+
peer->conns++;
ngx_http_upstream_rr_peers_unlock(peers);