MINOR: listeners: introduce listener_set_state()
This function is used as a wrapper to set a listener's state everywhere.
We'll use it later to maintain some counters in a consistent state when
switching state so it's capital that all state changes go through it.
No functional change was made beyond calling the wrapper.
diff --git a/include/haproxy/listener.h b/include/haproxy/listener.h
index 68020c3..3233ad5 100644
--- a/include/haproxy/listener.h
+++ b/include/haproxy/listener.h
@@ -30,6 +30,9 @@
#include <haproxy/list.h>
#include <haproxy/listener-t.h>
+/* adjust the listener's state and its proxy's listener counters if needed */
+void listener_set_state(struct listener *l, enum li_state st);
+
/* This function tries to temporarily disable a listener, depending on the OS
* capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
* SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
diff --git a/src/listener.c b/src/listener.c
index f6ab73c..1ad017d 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -218,6 +218,12 @@
#endif // USE_THREAD
+/* adjust the listener's state */
+void listener_set_state(struct listener *l, enum li_state st)
+{
+ l->state = st;
+}
+
/* This function adds the specified listener's file descriptor to the polling
* lists if it is in the LI_LISTEN state. The listener enters LI_READY or
* LI_FULL state depending on its number of connections. In daemon mode, we
@@ -237,15 +243,15 @@
do_unbind_listener(listener, 1);
else {
do_unbind_listener(listener, 0);
- listener->state = LI_LISTEN;
+ listener_set_state(listener, LI_LISTEN);
}
}
else if (!listener->maxconn || listener->nbconn < listener->maxconn) {
fd_want_recv(listener->rx.fd);
- listener->state = LI_READY;
+ listener_set_state(listener, LI_READY);
}
else {
- listener->state = LI_FULL;
+ listener_set_state(listener, LI_FULL);
}
}
/* if this listener is supposed to be only in the master, close it in the workers */
@@ -269,7 +275,7 @@
if (listener->state == LI_READY)
fd_stop_recv(listener->rx.fd);
MT_LIST_DEL(&listener->wait_queue);
- listener->state = LI_LISTEN;
+ listener_set_state(listener, LI_LISTEN);
end:
HA_SPIN_UNLOCK(LISTENER_LOCK, &listener->lock);
}
@@ -308,7 +314,7 @@
MT_LIST_DEL(&l->wait_queue);
fd_stop_recv(l->rx.fd);
- l->state = LI_PAUSED;
+ listener_set_state(l, LI_PAUSED);
end:
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
return ret;
@@ -372,12 +378,12 @@
goto end;
if (l->maxconn && l->nbconn >= l->maxconn) {
- l->state = LI_FULL;
+ listener_set_state(l, LI_FULL);
goto end;
}
fd_want_recv(l->rx.fd);
- l->state = LI_READY;
+ listener_set_state(l, LI_READY);
end:
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
return ret;
@@ -393,7 +399,7 @@
MT_LIST_DEL(&l->wait_queue);
if (l->state != LI_FULL) {
fd_stop_recv(l->rx.fd);
- l->state = LI_FULL;
+ listener_set_state(l, LI_FULL);
}
}
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
@@ -408,7 +414,7 @@
if (l->state == LI_READY) {
MT_LIST_TRY_ADDQ(list, &l->wait_queue);
fd_stop_recv(l->rx.fd);
- l->state = LI_LIMITED;
+ listener_set_state(l, LI_LIMITED);
}
HA_SPIN_UNLOCK(LISTENER_LOCK, &l->lock);
}
@@ -486,7 +492,7 @@
MT_LIST_DEL(&listener->wait_queue);
if (listener->state >= LI_PAUSED) {
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
fd_stop_both(listener->rx.fd);
}
@@ -547,7 +553,7 @@
l->rx.fd = fd;
memcpy(&l->rx.addr, ss, sizeof(*ss));
MT_LIST_INIT(&l->wait_queue);
- l->state = LI_INIT;
+ listener_set_state(l, LI_INIT);
proto->add(l, port);
@@ -575,7 +581,7 @@
HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
HA_SPIN_LOCK(LISTENER_LOCK, &listener->lock);
if (listener->state == LI_ASSIGNED) {
- listener->state = LI_INIT;
+ listener_set_state(listener, LI_INIT);
LIST_DEL(&listener->rx.proto_list);
listener->rx.proto->nb_listeners--;
_HA_ATOMIC_SUB(&jobs, 1);
diff --git a/src/proto_sockpair.c b/src/proto_sockpair.c
index fc26a80..eb7ef86 100644
--- a/src/proto_sockpair.c
+++ b/src/proto_sockpair.c
@@ -89,7 +89,7 @@
{
if (listener->state != LI_INIT)
return;
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
listener->rx.proto = &proto_sockpair;
LIST_ADDQ(&proto_sockpair.listeners, &listener->rx.proto_list);
proto_sockpair.nb_listeners++;
@@ -175,7 +175,7 @@
goto err_return;
}
- listener->state = LI_LISTEN;
+ listener_set_state(listener, LI_LISTEN);
return err;
err_return:
diff --git a/src/proto_tcp.c b/src/proto_tcp.c
index 138dd68..e77d50e 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -676,7 +676,7 @@
#endif
/* the socket is ready */
- listener->state = LI_LISTEN;
+ listener_set_state(listener, LI_LISTEN);
goto tcp_return;
tcp_close_return:
@@ -702,7 +702,7 @@
{
if (listener->state != LI_INIT)
return;
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
listener->rx.proto = &proto_tcpv4;
((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
LIST_ADDQ(&proto_tcpv4.listeners, &listener->rx.proto_list);
@@ -720,7 +720,7 @@
{
if (listener->state != LI_INIT)
return;
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
listener->rx.proto = &proto_tcpv6;
((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
LIST_ADDQ(&proto_tcpv6.listeners, &listener->rx.proto_list);
diff --git a/src/proto_udp.c b/src/proto_udp.c
index 2333b5d..af75cc9 100644
--- a/src/proto_udp.c
+++ b/src/proto_udp.c
@@ -114,7 +114,7 @@
goto udp_return;
}
- listener->state = LI_LISTEN;
+ listener_set_state(listener, LI_LISTEN);
udp_return:
if (msg && errlen) {
@@ -134,7 +134,7 @@
{
if (listener->state != LI_INIT)
return;
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
listener->rx.proto = &proto_udp4;
((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
LIST_ADDQ(&proto_udp4.listeners, &listener->rx.proto_list);
@@ -149,7 +149,7 @@
{
if (listener->state != LI_INIT)
return;
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
listener->rx.proto = &proto_udp6;
((struct sockaddr_in *)(&listener->rx.addr))->sin_port = htons(port);
LIST_ADDQ(&proto_udp6.listeners, &listener->rx.proto_list);
diff --git a/src/proto_uxst.c b/src/proto_uxst.c
index 6cf35b7..bafe97d 100644
--- a/src/proto_uxst.c
+++ b/src/proto_uxst.c
@@ -118,7 +118,7 @@
}
/* the socket is now listening */
- listener->state = LI_LISTEN;
+ listener_set_state(listener, LI_LISTEN);
return err;
uxst_close_return:
@@ -142,7 +142,7 @@
{
if (listener->state != LI_INIT)
return;
- listener->state = LI_ASSIGNED;
+ listener_set_state(listener, LI_ASSIGNED);
listener->rx.proto = &proto_unix;
LIST_ADDQ(&proto_unix.listeners, &listener->rx.proto_list);
proto_unix.nb_listeners++;