@@ -249,13 +249,17 @@ class Connection : public ObjectWrap {
249
249
bool ioInitialized_;
250
250
bool copyOutMode_;
251
251
bool copyInMode_;
252
+ bool reading_;
253
+ bool writing_;
252
254
Connection () : ObjectWrap ()
253
255
{
254
256
connection_ = NULL ;
255
257
connecting_ = false ;
256
258
ioInitialized_ = false ;
257
259
copyOutMode_ = false ;
258
260
copyInMode_ = false ;
261
+ reading_ = false ;
262
+ writing_ = false ;
259
263
TRACE (" Initializing ev watchers" );
260
264
read_watcher_.data = this ;
261
265
write_watcher_.data = this ;
@@ -304,20 +308,23 @@ class Connection : public ObjectWrap {
304
308
305
309
int Send (const char *queryText)
306
310
{
311
+ TRACE (" js::Send" )
307
312
int rv = PQsendQuery (connection_, queryText);
308
313
StartWrite ();
309
314
return rv;
310
315
}
311
316
312
317
int SendQueryParams (const char *command, const int nParams, const char * const *paramValues)
313
318
{
319
+ TRACE (" js::SendQueryParams" )
314
320
int rv = PQsendQueryParams (connection_, command, nParams, NULL , paramValues, NULL , NULL , 0 );
315
321
StartWrite ();
316
322
return rv;
317
323
}
318
324
319
325
int SendPrepare (const char *name, const char *command, const int nParams)
320
326
{
327
+ TRACE (" js::SendPrepare" )
321
328
int rv = PQsendPrepare (connection_, name, command, nParams, NULL );
322
329
StartWrite ();
323
330
return rv;
@@ -430,7 +437,7 @@ class Connection : public ObjectWrap {
430
437
if (PQconsumeInput (connection_) == 0 ) {
431
438
End ();
432
439
EmitLastError ();
433
- LOG (" Something happened, consume input is 0" );
440
+ // LOG("Something happened, consume input is 0");
434
441
return ;
435
442
}
436
443
@@ -476,7 +483,8 @@ class Connection : public ObjectWrap {
476
483
if (revents & UV_WRITABLE) {
477
484
TRACE (" revents & UV_WRITABLE" );
478
485
if (PQflush (connection_) == 0 ) {
479
- StopWrite ();
486
+ // nothing left to write, poll the socket for more to read
487
+ StartRead ();
480
488
}
481
489
}
482
490
}
@@ -669,12 +677,10 @@ class Connection : public ObjectWrap {
669
677
switch (status) {
670
678
case PGRES_POLLING_READING:
671
679
TRACE (" Polled: PGRES_POLLING_READING" );
672
- StopWrite ();
673
680
StartRead ();
674
681
break ;
675
682
case PGRES_POLLING_WRITING:
676
683
TRACE (" Polled: PGRES_POLLING_WRITING" );
677
- StopRead ();
678
684
StartWrite ();
679
685
break ;
680
686
case PGRES_POLLING_FAILED:
@@ -712,30 +718,42 @@ class Connection : public ObjectWrap {
712
718
713
719
void StopWrite ()
714
720
{
715
- TRACE (" Stoping write watcher " );
721
+ TRACE (" write STOP " );
716
722
if (ioInitialized_) {
717
723
uv_poll_stop (&write_watcher_);
724
+ writing_ = false ;
718
725
}
719
726
}
720
727
721
728
void StartWrite ()
722
729
{
723
- TRACE (" Starting write watcher" );
730
+ TRACE (" write START" );
731
+ if (reading_) {
732
+ TRACE (" stop READ to start WRITE" );
733
+ StopRead ();
734
+ }
724
735
uv_poll_start (&write_watcher_, UV_WRITABLE, io_event);
736
+ writing_ = true ;
725
737
}
726
738
727
739
void StopRead ()
728
740
{
729
- TRACE (" Stoping read watcher " );
741
+ TRACE (" read STOP " );
730
742
if (ioInitialized_) {
731
743
uv_poll_stop (&read_watcher_);
744
+ reading_ = false ;
732
745
}
733
746
}
734
747
735
748
void StartRead ()
736
749
{
737
- TRACE (" Starting read watcher" );
750
+ TRACE (" read START" );
751
+ if (writing_) {
752
+ TRACE (" stop WRITE to start READ" );
753
+ StopWrite ();
754
+ }
738
755
uv_poll_start (&read_watcher_, UV_READABLE, io_event);
756
+ reading_ = true ;
739
757
}
740
758
// Converts a v8 array to an array of cstrings
741
759
// the result char** array must be free() when it is no longer needed
0 commit comments