Skip to content

Commit c4d6b0f

Browse files
committed
Now wait also waits for connection reset
1 parent be15a85 commit c4d6b0f

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

source/async_postgres.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,6 @@ namespace async_postgres {
152152
// Converts a lua array at given index to a ParamValues
153153
ParamValues array_to_params(GLua::ILuaInterface* lua, int index);
154154
SocketStatus check_socket_status(PGconn* conn);
155+
bool wait_for_socket(PGconn* conn, bool write = false, bool read = false,
156+
int timeout = -1);
155157
}; // namespace async_postgres

source/main.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ namespace async_postgres::lua {
4444
lua_protected_fn(query) {
4545
lua->CheckType(1, async_postgres::connection_meta);
4646
lua->CheckType(2, GLua::Type::String);
47-
lua->CheckType(3, GLua::Type::Function);
4847

4948
auto state = lua_connection_state();
5049
if (state->query) {
@@ -66,7 +65,6 @@ namespace async_postgres::lua {
6665
lua->CheckType(1, async_postgres::connection_meta);
6766
lua->CheckType(2, GLua::Type::String);
6867
lua->CheckType(3, GLua::Type::Table);
69-
lua->CheckType(4, GLua::Type::Function);
7068

7169
auto state = lua_connection_state();
7270
if (state->query) {
@@ -91,7 +89,6 @@ namespace async_postgres::lua {
9189
lua->CheckType(1, async_postgres::connection_meta);
9290
lua->CheckType(2, GLua::Type::String);
9391
lua->CheckType(3, GLua::Type::String);
94-
lua->CheckType(4, GLua::Type::Function);
9592

9693
auto state = lua_connection_state();
9794
if (state->query) {
@@ -114,7 +111,6 @@ namespace async_postgres::lua {
114111
lua->CheckType(1, async_postgres::connection_meta);
115112
lua->CheckType(2, GLua::Type::String);
116113
lua->CheckType(3, GLua::Type::Table);
117-
lua->CheckType(4, GLua::Type::Function);
118114

119115
auto state = lua_connection_state();
120116
if (state->query) {
@@ -138,7 +134,6 @@ namespace async_postgres::lua {
138134
lua_protected_fn(describePrepared) {
139135
lua->CheckType(1, async_postgres::connection_meta);
140136
lua->CheckType(2, GLua::Type::String);
141-
lua->CheckType(3, GLua::Type::Function);
142137

143138
auto state = lua_connection_state();
144139
if (state->query) {
@@ -159,7 +154,6 @@ namespace async_postgres::lua {
159154
lua_protected_fn(describePortal) {
160155
lua->CheckType(1, async_postgres::connection_meta);
161156
lua->CheckType(2, GLua::Type::String);
162-
lua->CheckType(3, GLua::Type::Function);
163157

164158
auto state = lua_connection_state();
165159
if (state->query) {
@@ -193,10 +187,11 @@ namespace async_postgres::lua {
193187

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

198191
auto state = lua_connection_state();
199-
state->on_notify = GLua::AutoReference(lua, 2);
192+
if (lua->IsType(2, GLua::Type::Function)) {
193+
state->on_notify = GLua::AutoReference(lua, 2);
194+
}
200195

201196
return 0;
202197
}
@@ -205,6 +200,25 @@ namespace async_postgres::lua {
205200
lua->CheckType(1, async_postgres::connection_meta);
206201

207202
auto state = lua_connection_state();
203+
if (state->reset_event) {
204+
auto& event = state->reset_event.value();
205+
while (state->reset_event.has_value() &&
206+
&event == &state->reset_event.value()) {
207+
bool write =
208+
state->reset_event->status == PGRES_POLLING_WRITING;
209+
bool read = state->reset_event->status == PGRES_POLLING_READING;
210+
if (!write && !read) {
211+
break;
212+
}
213+
214+
wait_for_socket(state->conn.get(), write, read);
215+
process_reset(lua, state);
216+
}
217+
218+
lua->PushBool(1);
219+
return 1;
220+
}
221+
208222
if (state->query) {
209223
auto& query = state->query.value();
210224

source/util.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,26 @@ SocketStatus async_postgres::check_socket_status(PGconn* conn) {
9696

9797
return status;
9898
}
99+
100+
bool async_postgres::wait_for_socket(PGconn* conn, bool write, bool read,
101+
int timeout) {
102+
SOCKET fd = PQsocket(conn);
103+
if (fd < 0) {
104+
return false;
105+
}
106+
107+
short events = 0;
108+
if (write) {
109+
events |= POLLOUT;
110+
}
111+
if (read) {
112+
events |= POLLIN;
113+
}
114+
115+
pollfd fds[1] = {{fd, events, 0}};
116+
if (poll(fds, 1, timeout) < 0) {
117+
return false;
118+
}
119+
120+
return true;
121+
}

0 commit comments

Comments
 (0)