MINOR: hlua_fcn: alternative to old proxy and server attributes
This patch adds new lua methods:
- "Proxy.get_uuid()"
- "Proxy.get_name()"
- "Server.get_puid()"
- "Server.get_name()"
These methods will be equivalent to their old analog Proxy.{uuid,name}
and Server.{puid,name} attributes, but this will be the new preferred
way to fetch such infos as it duplicates memory only when necessary and
thus reduce the overall lua Server/Proxy objects memory footprint.
Legacy attributes (now superseded by the explicit getters) are expected
to be removed some day.
Co-authored-by: Aurelien DARRAGON <adarragon@haproxy.com>
diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 4b6e5af..d7e9f89 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -919,10 +919,18 @@
Contain the name of the proxy.
+.. js:function:: Proxy.get_name()
+
+ Returns the name of the proxy.
+
.. js:attribute:: Proxy.uuid
Contain the unique identifier of the proxy.
+.. js:function:: Proxy.get_uuid()
+
+ Returns the unique identifier of the proxy.
+
.. js:attribute:: Proxy.servers
Contain a table with the attached servers. The table is indexed by server
@@ -1006,10 +1014,18 @@
Contain the name of the server.
+.. js:function:: Server.get_name(sv)
+
+ Returns the name of the server.
+
.. js:attribute:: Server.puid
Contain the proxy unique identifier of the server.
+.. js:function:: Server.get_puid(sv)
+
+ Returns the proxy unique identifier of the server.
+
.. js:function:: Server.is_draining(sv)
Return true if the server is currently draining sticky connections.
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index f67ac84..c12300f 100644
--- a/src/hlua_fcn.c
+++ b/src/hlua_fcn.c
@@ -982,6 +982,27 @@
return 1;
}
+int hlua_server_get_puid(lua_State *L)
+{
+ struct server *srv;
+ char buffer[12];
+
+ srv = hlua_check_server(L, 1);
+
+ snprintf(buffer, sizeof(buffer), "%d", srv->puid);
+ lua_pushstring(L, buffer);
+ return 1;
+}
+
+int hlua_server_get_name(lua_State *L)
+{
+ struct server *srv;
+
+ srv = hlua_check_server(L, 1);
+ lua_pushstring(L, srv->id);
+ return 1;
+}
+
int hlua_server_is_draining(lua_State *L)
{
struct server *srv;
@@ -1305,6 +1326,26 @@
return hlua_checkudata(L, ud, class_proxy_ref);
}
+int hlua_proxy_get_name(lua_State *L)
+{
+ struct proxy *px;
+
+ px = hlua_check_proxy(L, 1);
+ lua_pushstring(L, px->id);
+ return 1;
+}
+
+int hlua_proxy_get_uuid(lua_State *L)
+{
+ struct proxy *px;
+ char buffer[17];
+
+ px = hlua_check_proxy(L, 1);
+ snprintf(buffer, sizeof(buffer), "%d", px->uuid);
+ lua_pushstring(L, buffer);
+ return 1;
+}
+
int hlua_proxy_pause(lua_State *L)
{
struct proxy *px;
@@ -1780,6 +1821,8 @@
lua_newtable(L);
lua_pushstring(L, "__index");
lua_newtable(L);
+ hlua_class_function(L, "get_name", hlua_server_get_name);
+ hlua_class_function(L, "get_puid", hlua_server_get_puid);
hlua_class_function(L, "is_draining", hlua_server_is_draining);
hlua_class_function(L, "set_maxconn", hlua_server_set_maxconn);
hlua_class_function(L, "get_maxconn", hlua_server_get_maxconn);
@@ -1808,6 +1851,8 @@
lua_newtable(L);
lua_pushstring(L, "__index");
lua_newtable(L);
+ hlua_class_function(L, "get_name", hlua_proxy_get_name);
+ hlua_class_function(L, "get_uuid", hlua_proxy_get_uuid);
hlua_class_function(L, "pause", hlua_proxy_pause);
hlua_class_function(L, "resume", hlua_proxy_resume);
hlua_class_function(L, "stop", hlua_proxy_stop);