identd: Rework lowestSocketId handling
authorJan Alexander Steffens (heftig) <jan.steffens@gmail.com>
Sat, 19 May 2018 10:31:40 +0000 (12:31 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 18 Jun 2018 19:25:50 +0000 (21:25 +0200)
Since we're appending only strictly increasing integers, the lowest
socketId is always at the front of the list (if that exists).

hasSocketsBelowId also had a off-by-one error. This had no effect on the
code since a socketId equal to the current _socketId cannot be in the
_waiting list.

Reorder processWaiting's handling of requests:
  - If the socketId is one the request might have been waiting for,
    check responseAvailable.
  - Otherwise, if there is at least one socket we need to wait for,
    continue waiting.
  - Otherwise, responseUnavailable.

The former order was:
  - If there are no sockets left from before this request was made,
    responseUnavailable.
  - If the socketId is one the request might have been waiting for,
    check responseAvailable.
  - Otherwise, continue waiting.

The change was necessary as the lowestSocketId no longer includes the
socketId that was just removed. In addition, the new order also matches
the if-else chain in IdentServer::respond.

src/core/identserver.cpp
src/core/identserver.h

index 6d23a6c..ea1295e 100644 (file)
@@ -120,7 +120,7 @@ void IdentServer::respond() {
         responseUnavailable(request);
     } else if (responseAvailable(request)) {
         // success
-    } else if (hasSocketsBelowId(transactionId)) {
+    } else if (lowestSocketId() < transactionId) {
         _requestQueue.emplace_back(request);
     } else {
         responseUnavailable(request);
@@ -182,10 +182,12 @@ qint64 IdentServer::addWaitingSocket() {
     return newSocketId;
 }
 
-bool IdentServer::hasSocketsBelowId(qint64 id) {
-    return std::any_of(_waiting.begin(), _waiting.end(), [=](qint64 socketId) {
-        return socketId <= id;
-    });
+qint64 IdentServer::lowestSocketId() {
+    if (_waiting.empty()) {
+        return std::numeric_limits<qint64>::max();
+    }
+
+    return _waiting.front();
 }
 
 void IdentServer::removeWaitingSocket(qint64 socketId) {
@@ -193,21 +195,15 @@ void IdentServer::removeWaitingSocket(qint64 socketId) {
 }
 
 void IdentServer::processWaiting(qint64 socketId) {
-    qint64 lowestSocketId = std::numeric_limits<qint64 >::max();
-    for (qint64 id : _waiting) {
-        if (id < lowestSocketId) {
-            lowestSocketId = id;
-        }
-    }
     removeWaitingSocket(socketId);
     _requestQueue.remove_if([=](Request request) {
-        if (request.transactionId < lowestSocketId) {
-            responseUnavailable(request);
+        if (socketId < request.transactionId && responseAvailable(request)) {
             return true;
-        } else if (request.transactionId > socketId) {
-            return responseAvailable(request);
-        } else {
+        } else if (lowestSocketId() < request.transactionId) {
             return false;
+        } else {
+            responseUnavailable(request);
+            return true;
         }
     });
 }
index 4d4aecc..cff24b7 100644 (file)
@@ -58,7 +58,7 @@ private:
 
     QString sysIdentForIdentity(const CoreIdentity *identity) const;
 
-    bool hasSocketsBelowId(qint64 socketId);
+    qint64 lowestSocketId();
 
     void processWaiting(qint64 socketId);