]> granicus.if.org Git - pdns/commitdiff
second take of busy waiting fix, now with blocking io
authorWieger Opmeer <opmeer@strato-rz.de>
Mon, 2 Mar 2015 12:21:44 +0000 (13:21 +0100)
committerAki Tuomi <cmouse@desteem.org>
Thu, 5 Mar 2015 17:08:31 +0000 (19:08 +0200)
modules/remotebackend/unixconnector.cc

index 6f8beedba9c2d04caf0d740e4193d11fe68552d9..402b2b8b11fcd8eabba5eeb1ec0b8c580a9ad19c 100644 (file)
@@ -53,6 +53,14 @@ int UnixsocketConnector::recv_message(rapidjson::Document &output) {
   s_output = "";       
 
   while((t.tv_sec - t0.tv_sec)*1000 + (t.tv_usec - t0.tv_usec)/1000 < this->timeout) { 
+    int avail = waitForData(this->fd, 0, this->timeout * 500); // use half the timeout as poll timeout
+    if (avail < 0) // poll error
+      return -1;
+    if (avail == 0) { // timeout
+      gettimeofday(&t, NULL);
+      continue;
+    }
+
     std::string temp;
     temp.clear();
 
@@ -110,7 +118,7 @@ ssize_t UnixsocketConnector::write(const std::string &data) {
     nbuf = data.copy(buf, sizeof buf, pos); // copy data and write
     nwrite = ::write(fd, buf, nbuf);
     pos = pos + sizeof(buf);
-    if (nwrite == -1) {
+    if (nwrite 1) {
       connected = false;
       close(fd);
       return -1;
@@ -141,17 +149,7 @@ void UnixsocketConnector::reconnect() {
      return;
   }
 
-  if (fcntl(fd, F_SETFL, O_NONBLOCK, &fd)) {
-     connected = false;
-     L<<Logger::Error<<"Cannot manipulate socket: " << strerror(errno) << std::endl;;
-     close(fd);
-     return;
-  }
-
-  if((rv = connect(fd, reinterpret_cast<struct sockaddr*>(&sock), sizeof sock))==-1 && (errno == EINPROGRESS)) {
-    waitForData(fd, 0, -1);
-    rv = connect(fd, reinterpret_cast<struct sockaddr*>(&sock), sizeof sock);
-  }
+  rv = connect(fd, reinterpret_cast<struct sockaddr*>(&sock), sizeof sock);
 
   if (rv != 0 && errno != EISCONN && errno != 0) {
      L<<Logger::Error<<"Cannot connect to socket: " << strerror(errno) << std::endl;