]> git.sesse.net Git - cubemap/blobdiff - client.cpp
Fix timeout behavior with persistent connections.
[cubemap] / client.cpp
index c2cbcf5033bf76c86ccdf36e28078d525f969841..23892ac19038406e584241a0ee462583c9447c0b 100644 (file)
@@ -20,11 +20,6 @@ Client::Client(int sock)
 {
        request.reserve(1024);
 
-       if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) {
-               log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)");
-               return;
-       }
-
        // Find the remote address, and convert it to ASCII.
        sockaddr_in6 addr;
        socklen_t addr_len = sizeof(addr);
@@ -54,7 +49,7 @@ Client::Client(int sock)
        }
 }
        
-Client::Client(const ClientProto &serialized, Stream *stream)
+Client::Client(const ClientProto &serialized, const vector<shared_ptr<const string>> &short_responses, Stream *stream)
        : sock(serialized.sock()),
          remote_addr(serialized.remote_addr()),
          referer(serialized.referer()),
@@ -63,7 +58,6 @@ Client::Client(const ClientProto &serialized, Stream *stream)
          request(serialized.request()),
          url(serialized.url()),
          stream(stream),
-         header_or_short_response(serialized.header_or_short_response()),
          header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
          stream_pos(serialized.stream_pos()),
          stream_pos_end(serialized.stream_pos_end()),
@@ -78,6 +72,16 @@ Client::Client(const ClientProto &serialized, Stream *stream)
                        }
                }
        }
+
+       if (serialized.has_header_or_short_response_old()) {
+               // Pre-1.4.0.
+               header_or_short_response_holder = serialized.header_or_short_response_old();
+               header_or_short_response = &header_or_short_response_holder;
+       } else if (serialized.has_header_or_short_response_index()) {
+               assert(size_t(serialized.header_or_short_response_index()) < short_responses.size());
+               header_or_short_response_ref = short_responses[serialized.header_or_short_response_index()];
+               header_or_short_response = header_or_short_response_ref.get();
+       }
        connect_time.tv_sec = serialized.connect_time_sec();
        connect_time.tv_nsec = serialized.connect_time_nsec();
 
@@ -106,7 +110,7 @@ Client::Client(const ClientProto &serialized, Stream *stream)
        }
 }
 
-ClientProto Client::serialize() const
+ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) const
 {
        ClientProto serialized;
        serialized.set_sock(sock);
@@ -118,7 +122,14 @@ ClientProto Client::serialize() const
        serialized.set_state(state);
        serialized.set_request(request);
        serialized.set_url(url);
-       serialized.set_header_or_short_response(header_or_short_response);
+
+       if (header_or_short_response != nullptr) {
+               // See if this string is already in the pool (deduplicated by the pointer); if not, insert it.
+               auto iterator_and_inserted = short_response_pool->emplace(
+                       header_or_short_response, short_response_pool->size());
+               serialized.set_header_or_short_response_index(iterator_and_inserted.first->second);
+       }
+
        serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
        serialized.set_stream_pos(stream_pos);
        serialized.set_stream_pos_end(stream_pos_end);