@@ -249,13 +249,17 @@ class Connection : public ObjectWrap {
249249 bool ioInitialized_;
250250 bool copyOutMode_;
251251 bool copyInMode_;
252+ bool reading_;
253+ bool writing_;
252254 Connection () : ObjectWrap ()
253255 {
254256 connection_ = NULL ;
255257 connecting_ = false ;
256258 ioInitialized_ = false ;
257259 copyOutMode_ = false ;
258260 copyInMode_ = false ;
261+ reading_ = false ;
262+ writing_ = false ;
259263 TRACE (" Initializing ev watchers" );
260264 read_watcher_.data = this ;
261265 write_watcher_.data = this ;
@@ -304,20 +308,23 @@ class Connection : public ObjectWrap {
304308
305309 int Send (const char *queryText)
306310 {
311+ TRACE (" js::Send" )
307312 int rv = PQsendQuery (connection_, queryText);
308313 StartWrite ();
309314 return rv;
310315 }
311316
312317 int SendQueryParams (const char *command, const int nParams, const char * const *paramValues)
313318 {
319+ TRACE (" js::SendQueryParams" )
314320 int rv = PQsendQueryParams (connection_, command, nParams, NULL , paramValues, NULL , NULL , 0 );
315321 StartWrite ();
316322 return rv;
317323 }
318324
319325 int SendPrepare (const char *name, const char *command, const int nParams)
320326 {
327+ TRACE (" js::SendPrepare" )
321328 int rv = PQsendPrepare (connection_, name, command, nParams, NULL );
322329 StartWrite ();
323330 return rv;
@@ -430,7 +437,7 @@ class Connection : public ObjectWrap {
430437 if (PQconsumeInput (connection_) == 0 ) {
431438 End ();
432439 EmitLastError ();
433- LOG (" Something happened, consume input is 0" );
440+ // LOG("Something happened, consume input is 0");
434441 return ;
435442 }
436443
@@ -476,7 +483,8 @@ class Connection : public ObjectWrap {
476483 if (revents & UV_WRITABLE) {
477484 TRACE (" revents & UV_WRITABLE" );
478485 if (PQflush (connection_) == 0 ) {
479- StopWrite ();
486+ // nothing left to write, poll the socket for more to read
487+ StartRead ();
480488 }
481489 }
482490 }
@@ -669,12 +677,10 @@ class Connection : public ObjectWrap {
669677 switch (status) {
670678 case PGRES_POLLING_READING:
671679 TRACE (" Polled: PGRES_POLLING_READING" );
672- StopWrite ();
673680 StartRead ();
674681 break ;
675682 case PGRES_POLLING_WRITING:
676683 TRACE (" Polled: PGRES_POLLING_WRITING" );
677- StopRead ();
678684 StartWrite ();
679685 break ;
680686 case PGRES_POLLING_FAILED:
@@ -712,30 +718,42 @@ class Connection : public ObjectWrap {
712718
713719 void StopWrite ()
714720 {
715- TRACE (" Stoping write watcher " );
721+ TRACE (" write STOP " );
716722 if (ioInitialized_) {
717723 uv_poll_stop (&write_watcher_);
724+ writing_ = false ;
718725 }
719726 }
720727
721728 void StartWrite ()
722729 {
723- TRACE (" Starting write watcher" );
730+ TRACE (" write START" );
731+ if (reading_) {
732+ TRACE (" stop READ to start WRITE" );
733+ StopRead ();
734+ }
724735 uv_poll_start (&write_watcher_, UV_WRITABLE, io_event);
736+ writing_ = true ;
725737 }
726738
727739 void StopRead ()
728740 {
729- TRACE (" Stoping read watcher " );
741+ TRACE (" read STOP " );
730742 if (ioInitialized_) {
731743 uv_poll_stop (&read_watcher_);
744+ reading_ = false ;
732745 }
733746 }
734747
735748 void StartRead ()
736749 {
737- TRACE (" Starting read watcher" );
750+ TRACE (" read START" );
751+ if (writing_) {
752+ TRACE (" stop WRITE to start READ" );
753+ StopWrite ();
754+ }
738755 uv_poll_start (&read_watcher_, UV_READABLE, io_event);
756+ reading_ = true ;
739757 }
740758 // Converts a v8 array to an array of cstrings
741759 // the result char** array must be free() when it is no longer needed
0 commit comments