Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

fix: keep connection count for idle preexisting connections (#50)

luoleiee0d4cbf14ce
diff --git a/apps/agent/internal/agent/runner.go b/apps/agent/internal/agent/runner.go
index cb85751..fcc59d7 100644
--- a/apps/agent/internal/agent/runner.go
+++ b/apps/agent/internal/agent/runner.go
@@ -491,9 +491,11 @@ func (r *Runner) ingestSnapshots(snapshots []domain.FlowSnapshot, nowMs int64) {
// Opened before this agent started: the cumulative counters include
// traffic from before we were watching (likely already reported by
// the previous process). Baseline only; later deltas count normally.
+ // Only a flow that already moved bytes was counted as a connection
+ // before; an idle one still counts once it first moves traffic.
+ counted = s.Upload > 0 || s.Download > 0
deltaUp = 0
deltaDown = 0
- counted = true
} else if hasPrev {
if s.Upload < prev.LastUpload || s.Download < prev.LastDown {
// Counter reset (gateway restart / connection id reuse): the
diff --git a/apps/agent/internal/agent/runner_test.go b/apps/agent/internal/agent/runner_test.go
index 6b86001..72da24c 100644
--- a/apps/agent/internal/agent/runner_test.go
+++ b/apps/agent/internal/agent/runner_test.go
@@ -159,6 +159,7 @@ func TestIngestSnapshotsBaselinesFlowsOpenedBeforeStart(t *testing.T) {
{ID: "old", Upload: 17_000_000_000, Download: 1, StartMs: started - 3_600_000, Chains: []string{"Proxy"}},
{ID: "new", Upload: 300, Download: 400, StartMs: started + 1_000, Chains: []string{"Proxy"}},
{ID: "unknown-start", Upload: 7, Download: 8, Chains: []string{"Proxy"}},
+ {ID: "idle", Upload: 0, Download: 0, StartMs: started - 3_600_000, Chains: []string{"Idle"}},
}, started+2_000)
first := runner.takeBatch(10)
@@ -181,4 +182,14 @@ func TestIngestSnapshotsBaselinesFlowsOpenedBeforeStart(t *testing.T) {
if second[0].Connections != 0 {
t.Fatalf("expected baselined flow not re-counted as a connection, got %d", second[0].Connections)
}
+
+ // An idle preexisting flow was never counted, so its first bytes add one
+ // connection.
+ runner.ingestSnapshots([]domain.FlowSnapshot{
+ {ID: "idle", Upload: 50, Download: 0, StartMs: started - 3_600_000, Chains: []string{"Idle"}},
+ }, started+4_000)
+ third := runner.takeBatch(10)
+ if len(third) != 1 || third[0].Upload != 50 || third[0].Connections != 1 {
+ t.Fatalf("expected idle baselined flow to count 50 bytes and 1 connection, got %+v", third)
+ }
}
diff --git a/apps/collector/src/modules/collector/connection-baseline.test.ts b/apps/collector/src/modules/collector/connection-baseline.test.ts
index 872fcbc..c856bee 100644
--- a/apps/collector/src/modules/collector/connection-baseline.test.ts
+++ b/apps/collector/src/modules/collector/connection-baseline.test.ts
@@ -110,6 +110,7 @@ describe("gateway collector restart baseline (issue #50)", () => {
JSON.stringify({
connections: [
conn("old", oldStart, 17e9, 17e9),
+ conn("idle", oldStart, 0, 0),
conn("new", newStart, 300, 400),
],
}),
@@ -126,15 +127,21 @@ describe("gateway collector restart baseline (issue #50)", () => {
JSON.stringify({
connections: [
conn("old", oldStart, 17e9 + 1000, 17e9),
+ conn("idle", oldStart, 50, 0),
conn("new", newStart, 300, 400),
],
}),
);
- await vi.waitFor(() => expect(recordedBytes(spy, backendId)).toHaveLength(2), {
+ await vi.waitFor(() => expect(recordedBytes(spy, backendId)).toHaveLength(3), {
timeout: 2000,
interval: 20,
});
- expect(recordedBytes(spy, backendId)[1]).toEqual({ up: 1000, down: 0, conns: 0 });
+ expect(recordedBytes(spy, backendId).slice(1)).toEqual([
+ { up: 1000, down: 0, conns: 0 },
+ // An idle preexisting connection was never counted, so its first bytes
+ // still add one connection.
+ { up: 50, down: 0, conns: 1 },
+ ]);
});
});
@@ -180,6 +187,8 @@ describe("surge collector restart baseline (issue #50)", () => {
JSON.stringify({
requests: [
request("old", nowSec - 3600, oldBytes, 0),
+ // Completed before the restart but still in the recent list.
+ { ...request("done", nowSec - 600, 9e9, 9e9), completed: true },
request("new", nowSec + 1, 300, 400),
],
}),
diff --git a/apps/collector/src/modules/collector/connection-baseline.ts b/apps/collector/src/modules/collector/connection-baseline.ts
index 77b3ee9..90dcaeb 100644
--- a/apps/collector/src/modules/collector/connection-baseline.ts
+++ b/apps/collector/src/modules/collector/connection-baseline.ts
@@ -5,6 +5,11 @@
// recorded by the previous process. Counting them as fresh traffic produces a
// huge spike at restart (issue #50). Such connections only establish a
// baseline; later deltas are counted normally.
+//
+// This is a mitigation with known limits: it relies on the gateway reporting a
+// start time and on gateway/collector clocks being roughly in sync. Bytes a
+// preexisting connection moves between collector start and the first snapshot
+// are not counted.
// Allow for clock skew between the gateway and the collector host.
export const PREEXISTING_GRACE_MS = 5_000;
@@ -28,9 +33,9 @@ export function parseConnectionStartMs(value: unknown): number | null {
}
/**
- * True when the connection demonstrably started before the collector began
- * watching. Unknown start times return false so traffic is never dropped on
- * backends that omit the field.
+ * True when the connection started before the collector began watching.
+ * Unknown start times return false, keeping the previous behavior (count the
+ * first snapshot) for backends that omit the field.
*/
export function isPreexistingConnection(
startValue: unknown,
diff --git a/apps/collector/src/modules/collector/gateway.collector.ts b/apps/collector/src/modules/collector/gateway.collector.ts
index 4876058..c791d80 100644
--- a/apps/collector/src/modules/collector/gateway.collector.ts
+++ b/apps/collector/src/modules/collector/gateway.collector.ts
@@ -468,9 +468,12 @@ export function createCollector(
lastDownload: conn.download,
totalUpload: conn.upload,
totalDownload: conn.download,
- // A preexisting connection was already counted by whoever was
- // watching before us.
- counted: hasInitialTraffic || preexisting,
+ // A preexisting connection that already moved bytes was counted by
+ // whoever watched it before us; an idle one still counts once it
+ // first moves traffic.
+ counted:
+ hasInitialTraffic ||
+ (preexisting && (conn.upload > 0 || conn.download > 0)),
sourceIP,
lastSeen: now,
});
diff --git a/apps/collector/src/modules/collector/surge.collector.ts b/apps/collector/src/modules/collector/surge.collector.ts
index e38fdd2..4b85f83 100644
--- a/apps/collector/src/modules/collector/surge.collector.ts
+++ b/apps/collector/src/modules/collector/surge.collector.ts
@@ -574,7 +574,10 @@ export function createSurgeCollector(
completed: isCompleted,
disconnected: isDisconnected,
lastStatus: req.status,
- counted: hasInitialTraffic || preexisting,
+ // Preexisting requests with bytes were counted before the restart.
+ counted:
+ hasInitialTraffic ||
+ (preexisting && (currentUpload > 0 || currentDownload > 0)),
initialProcessed: true, // Mark as processed
});