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>
This commit is contained in:
Thierry Fournier 2022-10-07 12:07:24 +02:00 committed by Christopher Faulet
parent 467913c84e
commit b0467730a0
2 changed files with 61 additions and 0 deletions

View 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.

View 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);