diff --git a/README.md b/README.md index 480c837..df78a02 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ client never generates one, never reads or writes the file, and sends nothing. | **Returns immediately** | The HTTP call runs on one thread the client owns. A game loop can report from its main thread and no frame waits on the network. | | **Never throws** | Nothing in the client throws. A server that is down, slow, or rejecting the key — or a machine with no `curl` — is a dropped report, not an exception or a crash in your program. Drops are passed to the optional `Logger` (a `std::function`, last constructor argument), otherwise not mentioned at all. | | **Bounded** | At most 256 reports wait to be sent; past that, new ones are dropped. A trace server that is unreachable for a week costs a few kilobytes, not your memory. | -| **`close()` drains** | Reports already queued get up to the client timeout (5 s total) to be sent before the thread stops, so a CLI that reports and exits at once does not lose its event. Still bounded: an unreachable server delays exit by at most the timeout — a request still in flight then is abandoned and its `curl` process killed. `close(seconds)` takes a shorter bound. Once `close()` returns, the `Logger` is never called again. | +| **`close()` drains** | Reports already queued get up to the client timeout (5 s total) to be sent before the thread stops, so a CLI that reports and exits at once does not lose its event. Still bounded: an unreachable server delays exit by at most the timeout — a request still in flight then is abandoned and its `curl` process killed. `close(seconds)` takes another bound: a NaN or negative one waits for nothing, and `close(INFINITY)` waits until the queue is sent, each request still bounded by the timeout. Once `close()` returns, the `Logger` is never called again. | | **Fits the server's limits** | Names and tag keys/values are cut to 255 bytes (never mid-character), at most 32 tags are sent, blank tag keys are dropped, invalid UTF-8 becomes U+FFFD, and NaN or infinite values are left out — so a report is never rejected as a whole for one bad field. A report whose name is empty or blank is not an event at all: it is ignored, without a mention to the `Logger`. | ## Turning it off diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 313e2d9..a10972b 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -1050,6 +1050,37 @@ static void closeIsPromptAndIdempotent() { client.report("after-close"); // a no-op, not a crash } +static void closeWithABoundTooLongForTheClockSendsEverythingQueued() { + // Infinity, or a bound whose deadline the clock cannot hold, means "as + // long as it takes" -- not a deadline that wrapped round into the past. + const double bounds[] = {HUGE_VAL, std::numeric_limits::max(), 1e300, 1e12}; + StubServer server; + std::size_t expected = 0; + for (std::size_t i = 0; i < sizeof bounds / sizeof bounds[0]; ++i) { + trace_client::TraceClient client(server.baseUrl(), "MyCli", "1.2.3", "k"); + for (int n = 0; n < 5; ++n) client.report("startup", {{"run", std::to_string(n)}}); + client.close(bounds[i]); + expected += 5; + CHECK_EQ(expected, server.received().size()); + } +} + +static void closeWithANegativeOrNanBoundWaitsForNothing() { + const double bounds[] = {std::nan(""), -1.0, -HUGE_VAL}; + for (std::size_t i = 0; i < sizeof bounds / sizeof bounds[0]; ++i) { + StubServer server; + server.hold(); // never answers while held + trace_client::TraceClient client(server.baseUrl(), "MyCli", "1.2.3", "k"); + client.report("startup"); + client.report("second"); + std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); + client.close(bounds[i]); + CHECK(secondsSince(start) < 1.0); + CHECK(!client.isEnabled()); + server.release(); + } +} + // ---------------------------------------------------------------- installation ID static std::string tempBase() { @@ -1376,6 +1407,8 @@ int main() { {"everyAllocationTheConstructorMakesMayFailWithoutAborting", everyAllocationTheConstructorMakesMayFailWithoutAborting}, {"closeReturnsWithinTheTimeoutWhenTheServerHangs", closeReturnsWithinTheTimeoutWhenTheServerHangs}, {"closeIsPromptAndIdempotent", closeIsPromptAndIdempotent}, + {"closeWithABoundTooLongForTheClockSendsEverythingQueued", closeWithABoundTooLongForTheClockSendsEverythingQueued}, + {"closeWithANegativeOrNanBoundWaitsForNothing", closeWithANegativeOrNanBoundWaitsForNothing}, {"reportTagsEveryEventWithTheProgramVersionTrimmed", reportTagsEveryEventWithTheProgramVersionTrimmed}, {"anEventsOwnVersionTagWinsOverTheProgramVersion", anEventsOwnVersionTagWinsOverTheProgramVersion}, {"theCallersTagsAreNeverModified", theCallersTagsAreNeverModified}, diff --git a/trace_client.hpp b/trace_client.hpp index ded7b83..6d13ab2 100644 --- a/trace_client.hpp +++ b/trace_client.hpp @@ -1032,7 +1032,9 @@ class TraceClient { /** * Stops the sending thread, giving reports already queued up to * timeoutSeconds in total to be sent first; whatever is still in flight - * then is abandoned. Safe to call more than once, and on a disabled client. + * then is abandoned. A NaN or negative timeoutSeconds waits for nothing; + * an infinite one waits until the queue is sent. Safe to call more than + * once, and on a disabled client. */ void close(double timeoutSeconds = TIMEOUT_SECONDS) { std::shared_ptr shared; @@ -1047,10 +1049,17 @@ class TraceClient { std::unique_lock lock(shared->mutex); shared->closing = true; shared->changed.notify_all(); + // NaN and negative bounds wait for nothing. Past about three years + // (infinity included) the deadline would overflow the clock, so + // such a bound waits until the queue is sent; each request is + // still bounded by curl's own timeout. + double seconds = timeoutSeconds > 0 ? timeoutSeconds : 0; + bool unbounded = seconds > 1e8; std::chrono::steady_clock::time_point deadline = std::chrono::steady_clock::now() - + std::chrono::milliseconds(static_cast(timeoutSeconds * 1000)); + + std::chrono::milliseconds(unbounded ? 0 : static_cast(seconds * 1000)); while (!shared->done) { - if (shared->changed.wait_until(lock, deadline) == std::cv_status::timeout) break; + if (unbounded) shared->changed.wait(lock); + else if (shared->changed.wait_until(lock, deadline) == std::cv_status::timeout) break; } if (shared->done) { lock.unlock();