Skip to content

Commit

Permalink
LResolver: move code to libcommon
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Mar 8, 2024
1 parent 390affa commit c144479
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
2 changes: 1 addition & 1 deletion libcommon
36 changes: 6 additions & 30 deletions src/LResolver.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "lua/Error.hxx"
#include "lua/ForEach.hxx"
#include "lua/PushCClosure.hxx"
#include "lua/net/Resolver.hxx"
#include "lua/net/SocketAddress.hxx"
#include "net/Resolver.hxx"
#include "net/AddressInfo.hxx"
Expand All @@ -21,35 +22,6 @@ extern "C" {

#include <string.h>

static constexpr auto hints = MakeAddrInfo(0, AF_UNSPEC, SOCK_STREAM);

static int
l_mysql_resolve(lua_State *L)
{
if (lua_gettop(L) != 1)
return luaL_error(L, "Invalid parameter count");

const char *s = luaL_checkstring(L, 1);

if (*s == '/' || *s == '@') {
AllocatedSocketAddress address;
address.SetLocal(s);
Lua::NewSocketAddress(L, std::move(address));
return 1;
}

AddressInfoList ai;

try {
ai = Resolve(s, 3306, &hints);
} catch (...) {
Lua::RaiseCurrent(L);
}

Lua::NewSocketAddress(L, std::move(ai.GetBest()));
return 1;
}

static int
l_mysql_cluster(lua_State *L)
try {
Expand Down Expand Up @@ -89,7 +61,11 @@ void
RegisterLuaResolver(lua_State *L, EventLoop &event_loop)
{
Cluster::Register(L);
Lua::SetGlobal(L, "mysql_resolve", l_mysql_resolve);

static constexpr auto hints = MakeAddrInfo(0, AF_UNSPEC, SOCK_STREAM);
Lua::PushResolveFunction(L, hints, 3306);
lua_setglobal(L, "mysql_resolve");

Lua::SetGlobal(L, "mysql_cluster",
Lua::MakeCClosure(l_mysql_cluster,
Lua::LightUserData{&event_loop}));
Expand Down

0 comments on commit c144479

Please sign in to comment.