Skip to content

Commit 27e8163

Browse files
shubh-stripecodex
andcommitted
Fix connection success cache cleanup
Pass the cleanup interval through as the time.Duration supplied by callers. Multiplying it by time.Second overflows realistic duration values and prevents expired destination attempts from being removed from memory.\n\nAdd coverage for physical cache eviction so logical expiration cannot hide retained entries. Committed-By-Agent: codex Co-authored-by: codex <noreply@openai.com>
1 parent a3294a6 commit 27e8163

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

pkg/smokescreen/conntrack/conn_tracker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type ConnSuccessRateStats struct {
6565
// - cleanupInterval is how often expired items (e.g., items older than the calculationWindow) will be deleted from memory.
6666
func StartNewConnSuccessRateTracker(calculationInterval time.Duration, calculationWindow time.Duration, cleanupInterval time.Duration, statsc metrics.MetricsClientInterface) *ConnSuccessRateTracker {
6767
newSuccessTracker := &ConnSuccessRateTracker{
68-
ConnAttempts: cache.New(calculationWindow, time.Second*cleanupInterval),
68+
ConnAttempts: cache.New(calculationWindow, cleanupInterval),
6969
}
7070
newSuccessTracker.ConnSuccessRateStats.Store(ConnSuccessRateStats{CalculatedAt: time.Now(), ConnSuccessRate: 100, TotalConns: 0})
7171

pkg/smokescreen/conntrack/conn_tracker_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,29 @@ func TestConnSuccessRateTracker(t *testing.T) {
113113
}
114114
}
115115

116+
func TestConnSuccessRateTrackerDeletesExpiredAttempts(t *testing.T) {
117+
sd := atomic.Value{}
118+
sd.Store(false)
119+
tracker := NewTracker(
120+
time.Second,
121+
metrics.NewNoOpMetricsClient(),
122+
logrus.New(),
123+
sd,
124+
StartNewConnSuccessRateTracker(
125+
time.Hour,
126+
50*time.Millisecond,
127+
10*time.Millisecond,
128+
metrics.NewNoOpMetricsClient(),
129+
),
130+
)
131+
132+
tracker.RecordAttempt("example.com", true)
133+
assert.Equal(t, 1, tracker.SuccessRateTracker.ConnAttempts.ItemCount())
134+
assert.Eventually(t, func() bool {
135+
return tracker.SuccessRateTracker.ConnAttempts.ItemCount() == 0
136+
}, time.Second, 10*time.Millisecond)
137+
}
138+
116139
func TestNoConnSuccessRateTracker(t *testing.T) {
117140
assert := assert.New(t)
118141
tracker := NewTestTracker(time.Second)

0 commit comments

Comments
 (0)