Skip to content

Commit 73233ce

Browse files
committed
Improved connection reset and query management
1 parent a60e658 commit 73233ce

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

source/connection.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,23 @@ void async_postgres::process_reset(GLua::ILuaInterface* lua,
126126

127127
event.status = PQresetPoll(state->conn.get());
128128
if (event.status == PGRES_POLLING_OK) {
129-
for (auto& callback : event.callbacks) {
129+
auto callbacks = std::move(event.callbacks);
130+
state->reset_event.reset();
131+
132+
for (auto& callback : callbacks) {
130133
callback.Push();
131134
lua->PushBool(true);
132135
pcall(lua, 1, 0);
133136
}
134-
135-
state->reset_event.reset();
136137
} else if (event.status == PGRES_POLLING_FAILED) {
137-
for (auto& callback : event.callbacks) {
138+
auto callbacks = std::move(event.callbacks);
139+
state->reset_event.reset();
140+
141+
for (auto& callback : callbacks) {
138142
callback.Push();
139143
lua->PushBool(false);
140144
lua->PushString(PQerrorMessage(state->conn.get()));
141145
pcall(lua, 2, 0);
142146
}
143-
144-
state->reset_event.reset();
145147
}
146148
}

source/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace async_postgres::lua {
2121
continue;
2222
}
2323

24-
async_postgres::process_reset(lua, state);
2524
async_postgres::process_notifications(lua, state);
2625
async_postgres::process_query(lua, state);
26+
async_postgres::process_reset(lua, state);
2727
}
2828

2929
return 0;

source/notifications.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ using namespace async_postgres;
44

55
void async_postgres::process_notifications(GLua::ILuaInterface* lua,
66
Connection* state) {
7-
if (state->reset_event) {
8-
// don't process notifications while reconnecting
9-
return;
10-
}
11-
127
if (!state->on_notify) {
138
return;
149
}

source/query.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ void query_failed(GLua::ILuaInterface* lua, Connection* state) {
2626
}
2727

2828
auto query = std::move(*state->query);
29-
lua->Msg("[async_postgres] query failed: current query %d\n",
30-
state->query.has_value());
31-
3229
state->query.reset();
3330

3431
if (query.callback) {
@@ -118,7 +115,7 @@ void process_result(GLua::ILuaInterface* lua, Connection* state,
118115

119116
void async_postgres::process_query(GLua::ILuaInterface* lua,
120117
Connection* state) {
121-
if (!state->query || state->reset_event) {
118+
if (!state->query) {
122119
// no queries to process
123120
// don't process queries while reconnecting
124121
return;
@@ -135,10 +132,14 @@ void async_postgres::process_query(GLua::ILuaInterface* lua,
135132
query.flushed = PQflush(state->conn.get()) == 0;
136133
}
137134

138-
if (!poll_query(state->conn.get(), query)) {
139-
query_failed(lua, state);
140-
return process_query(lua, state);
141-
}
135+
// if (!poll_query(state->conn.get(), query)) {
136+
// query_failed(lua, state);
137+
// return process_query(lua, state);
138+
// }
139+
140+
// probably failed poll should not result
141+
// in query failure
142+
poll_query(state->conn.get(), query);
142143

143144
// ensure that getting result won't block
144145
if (!pg::isBusy(state->conn)) {

0 commit comments

Comments
 (0)