Skip to content
Snippets Groups Projects
Commit 0983ca7c authored by Stephen R. van den Berg's avatar Stephen R. van den Berg
Browse files

pgsql: Expedite filedescriptor close when leaving scope.

parent a08aebc9
No related branches found
No related tags found
No related merge requests found
...@@ -597,6 +597,10 @@ private int datarowdebugcount; ...@@ -597,6 +597,10 @@ private int datarowdebugcount;
#endif #endif
final void _processloop(.pgsql_util.conxion ci) { final void _processloop(.pgsql_util.conxion ci) {
if(c && (!ci || c!=ci)) // If we are switching or dropping connections
c->close(); // force a close on the old socket
if(!this) // Oops, current object already destructed
return;
(c=ci)->socket->set_id(procmessage); (c=ci)->socket->set_id(procmessage);
cancelsecret=0; cancelsecret=0;
portal=0; portal=0;
...@@ -651,6 +655,10 @@ private void procmessage() { ...@@ -651,6 +655,10 @@ private void procmessage() {
if(!sizeof(cr)) { // Preliminary check, fast path if(!sizeof(cr)) { // Preliminary check, fast path
Thread.MutexKey lock=cr->fillreadmux->lock(); Thread.MutexKey lock=cr->fillreadmux->lock();
if(!sizeof(cr)) { // Check for real if(!sizeof(cr)) { // Check for real
if(!cr->fillread) {
lock=0;
throw(MAGICTERMINATE); // Force proper termination
}
cr->procmsg=1; cr->procmsg=1;
lock=0; lock=0;
return; // Terminate thread, wait for callback return; // Terminate thread, wait for callback
...@@ -1196,8 +1204,9 @@ private void procmessage() { ...@@ -1196,8 +1204,9 @@ private void procmessage() {
//! This function is PostgreSQL-specific, and thus it is not available //! This function is PostgreSQL-specific, and thus it is not available
//! through the generic SQL-interface. //! through the generic SQL-interface.
/*semi*/final void close() { /*semi*/final void close() {
if(qportals && qportals->size())
catch(cancelquery()); catch(cancelquery());
catch(c->sendterminate()); catch(c->close());
c=0; c=0;
destruct(waitforauthready); destruct(waitforauthready);
} }
...@@ -1253,7 +1262,7 @@ private int reconnect() { ...@@ -1253,7 +1262,7 @@ private int reconnect() {
#ifdef PG_STATS #ifdef PG_STATS
prepstmtused=0; prepstmtused=0;
#endif #endif
c->sendterminate(); c->close();
c=0; c=0;
PD("Flushing old cache\n"); PD("Flushing old cache\n");
foreach(_prepareds;;mapping tp) foreach(_prepareds;;mapping tp)
......
...@@ -335,19 +335,23 @@ outer: ...@@ -335,19 +335,23 @@ outer:
catch(connectfail()); catch(connectfail());
} }
final void sendterminate() {
Thread.MutexKey lock=i->fillreadmux->lock();
if(i->fillread) // Delayed close() after flushing the output buffer
i->fillread.signal(), i->fillread=0;
lock=0;
}
final int close() { final int close() {
int ret; int ret;
{ Thread.MutexKey lock=i->fillreadmux->lock();
if(i->fillread) { // Delayed close() after flushing the output buffer
i->fillread.signal();
i->fillread=0;
lock=0;
PD("%d>Close socket read, flush write\n",socket->query_fd());
ret=socket->close("r");
i->read_cb(socket->query_id(),0);
return ret;
}
lock=0;
}
destruct(nostash); destruct(nostash);
PD("%d>Close socket\n",socket->query_fd()); PD("%d>Close socket\n",socket->query_fd());
ret=socket->close(); ret=socket->close();
sendterminate();
foreach(closecallbacks;function(void|mixed:void) closecb;) foreach(closecallbacks;function(void|mixed:void) closecb;)
closecb(); closecb();
closecallbacks=(<>); closecallbacks=(<>);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment