]> git.sesse.net Git - greproxy/commitdiff
Timeout packets in the reordering buffer after 100 ms. Helps in situations with seque...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 7 Feb 2015 15:30:30 +0000 (16:30 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 7 Feb 2015 15:30:30 +0000 (16:30 +0100)
reorderer.cpp
reorderer.h

index e18b63f6007689161bdbf88ef3af03444ab786c2..7b19d1d3435ab9e5b1ba702b38c6e70d295e3408 100644 (file)
@@ -4,9 +4,16 @@
 #include "protocol.h"
 
 #define PACKET_BUFFER_SIZE 100
+#define TIMEOUT_SEC 0.100
 
 using namespace std;
 
+double tdiff(const timeval& a, const timeval& b)
+{
+       return b.tv_sec - a.tv_sec +
+               1e-6 * (b.tv_usec - a.tv_usec);
+}
+
 Reorderer::Reorderer(Sender* sender)
        : sender(sender), last_seq(-1)
 {
@@ -14,18 +21,30 @@ Reorderer::Reorderer(Sender* sender)
 
 void Reorderer::send_packet(uint16_t proto, const string& data, int seq)
 {
+       timeval now;
+       gettimeofday(&now, NULL);
+
        bool silence = false;
        if (packet_buffer.size() >= PACKET_BUFFER_SIZE) {
-               printf("Gave up waiting for packets [%d,%d>\n",
+               printf("Gave up waiting for packets [%d,%d> (buffer full)\n",
+                       last_seq + 1, packet_buffer.top().seq);
+               silence = true;
+               last_seq = packet_buffer.top().seq - 1;
+       } else if (!packet_buffer.empty() &&
+                  tdiff(packet_buffer.top().ts, now) > TIMEOUT_SEC) {
+               printf("Gave up waiting for packets [%d,%d> (timeout)\n",
                        last_seq + 1, packet_buffer.top().seq);
                silence = true;
                last_seq = packet_buffer.top().seq - 1;
+               // TODO: Rerun immediately after we've cleared out,
+               // in case there are more timeouts.
        }
 
        GREPacket packet;
        packet.seq = seq;
        packet.proto = proto;
        packet.data = data;
+       packet.ts = now;
        packet_buffer.push(packet);
 
        while (!packet_buffer.empty() &&
index cdde8146a090a04ddd7d44b7ce836cef4a4850e0..f58a9327673ac1850c67b8e5f4b91ae5d72eb1f4 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <assert.h>
 #include <stdint.h>
+#include <sys/time.h>
 
 #include <algorithm>
 #include <map>
@@ -16,6 +17,7 @@ struct GREPacket {
        int seq;
        uint16_t proto;
        std::string data;
+       timeval ts;
 
        bool operator> (const GREPacket &other) const {
                return seq > other.seq;