]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hlua_fcn: alternative to old proxy and server attributes
authorThierry Fournier <tfournier@arpalert.org>
Fri, 7 Oct 2022 10:07:24 +0000 (12:07 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 5 Apr 2023 06:58:16 +0000 (08:58 +0200)
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>
doc/lua-api/index.rst
src/hlua_fcn.c

index 4b6e5af55a4797c739ec6008030ab42cd74f1de4..d7e9f89b18ed0ca039eeffaea812bbb459680c80 100644 (file)
@@ -919,10 +919,18 @@ Proxy class
 
   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 @@ Server class
 
   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.
index f67ac84fe52178a471d8860637b2559e5d98aa62..c12300fed7bce11fd675c56f77d1149e00b8a4f8 100644 (file)
@@ -982,6 +982,27 @@ int hlua_server_get_addr(lua_State *L)
        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 @@ static struct proxy *hlua_check_proxy(lua_State *L, int ud)
        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 @@ void hlua_fcn_reg_core_fcn(lua_State *L)
        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 @@ void hlua_fcn_reg_core_fcn(lua_State *L)
        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);