Skip to content

Commit

Permalink
Removed lua table from PGconn and made on_notify just a lua reference
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmolot committed Jan 13, 2025
1 parent 2b07410 commit 7876e3a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 58 deletions.
4 changes: 1 addition & 3 deletions source/async_postgres.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ namespace async_postgres {

struct Connection {
pg::conn conn;
GLua::AutoReference lua_table;
std::queue<Query> queries;
std::optional<ResetEvent> reset_event;
bool receive_notifications =
false; // enabled if on_notify lua field is set
GLua::AutoReference on_notify;

Connection(GLua::ILuaInterface* lua, pg::conn&& conn);
~Connection();
Expand Down
3 changes: 0 additions & 3 deletions source/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ std::vector<Connection*> async_postgres::connections = {};

Connection::Connection(GLua::ILuaInterface* lua, pg::conn&& conn)
: conn(std::move(conn)) {
lua->CreateTable();
this->lua_table = GLua::AutoReference(lua);

// add connection to global list
connections.push_back(this);
}
Expand Down
51 changes: 14 additions & 37 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,6 @@ namespace async_postgres::lua {
return 0;
}

lua_protected_fn(__index) {
auto state = lua_connection_state();

state->lua_table.Push();
lua->Push(2);
lua->GetTable(-2);
if (!lua->IsType(-1, GLua::Type::Nil)) {
return 1;
}

// is it alright if I don't pop previous stack values?

lua->PushMetaTable(async_postgres::connection_meta);
lua->Push(2);
lua->GetTable(-2);

return 1;
}

lua_protected_fn(__newindex) {
auto state = lua_connection_state();

state->lua_table.Push();
lua->Push(2);
lua->Push(3);
lua->SetTable(-3);

auto key = get_string(lua, 2);
if (key == "on_notify") {
state->receive_notifications = !lua->IsType(3, GLua::Type::Nil);
}

return 1;
}

lua_protected_fn(loop) {
async_postgres::process_pending_connections(lua);

Expand Down Expand Up @@ -126,6 +91,16 @@ namespace async_postgres::lua {

return 0;
}

lua_protected_fn(setNotifyCallback) {
lua->CheckType(1, async_postgres::connection_meta);
lua->CheckType(2, GLua::Type::Function);

auto state = lua_connection_state();
state->on_notify = GLua::AutoReference(lua, 2);

return 0;
}
} // namespace async_postgres::lua

#define register_lua_fn(name) \
Expand All @@ -135,12 +110,14 @@ namespace async_postgres::lua {
void register_connection_mt(GLua::ILuaInterface* lua) {
async_postgres::connection_meta = lua->CreateMetaTable("PGconn");

register_lua_fn(__index);
register_lua_fn(__newindex);
lua->Push(-1);
lua->SetField(-2, "__index");

register_lua_fn(__gc);
register_lua_fn(query);
register_lua_fn(queryParams);
register_lua_fn(reset);
register_lua_fn(setNotifyCallback);

async_postgres::register_misc_connection_functions(lua);

Expand Down
16 changes: 2 additions & 14 deletions source/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,14 @@

using namespace async_postgres;

bool push_on_notify(GLua::ILuaInterface* lua, Connection* state) {
state->lua_table.Push();
lua->GetField(-1, "on_notify");
lua->Remove(-2); // remove the connection table

if (lua->IsType(-1, GLua::Type::Nil)) {
lua->Pop();
return false;
}
return true;
}

void async_postgres::process_notifications(GLua::ILuaInterface* lua,
Connection* state) {
if (state->reset_event) {
// don't process notifications while reconnecting
return;
}

if (!state->receive_notifications) {
if (!state->on_notify) {
return;
}

Expand All @@ -34,7 +22,7 @@ void async_postgres::process_notifications(GLua::ILuaInterface* lua,
}

while (auto notify = pg::getNotify(state->conn)) {
if (push_on_notify(lua, state)) {
if (state->on_notify.Push()) {
lua->PushString(notify->relname); // arg 1 channel name
lua->PushString(notify->extra); // arg 2 payload
lua->PushNumber(notify->be_pid); // arg 3 backend pid
Expand Down
3 changes: 2 additions & 1 deletion source/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ ParamValues async_postgres::array_to_params(GLua::ILuaInterface* lua,
#define poll WSAPoll
#else
#include <poll.h>
#define SOCKET int
#endif

SocketStatus async_postgres::check_socket_status(PGconn* conn) {
SocketStatus status = {};
int fd = PQsocket(conn);
SOCKET fd = PQsocket(conn);
if (fd < 0) {
status.failed = true;
return status;
Expand Down

0 comments on commit 7876e3a

Please sign in to comment.