Skip to content

Commit eb28184

Browse files
committed
chore: trim closed connections faster
1 parent cf92843 commit eb28184

File tree

1 file changed

+1
-28
lines changed

1 file changed

+1
-28
lines changed

experimental/libbox/command_types.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ const (
6969
)
7070

7171
const (
72-
maxClosedConnections = 1000
73-
closedConnectionMaxAge = int64(time.Hour / time.Millisecond)
72+
closedConnectionMaxAge = int64((5 * time.Minute) / time.Millisecond)
7473
)
7574

7675
type ConnectionEvent struct {
@@ -104,11 +103,6 @@ type Connections struct {
104103
filterApplied bool
105104
}
106105

107-
type closedConnection struct {
108-
id string
109-
closedAt int64
110-
}
111-
112106
func NewConnections() *Connections {
113107
return &Connections{
114108
connectionMap: make(map[string]*Connection),
@@ -168,34 +162,13 @@ func (c *Connections) ApplyEvents(events *ConnectionEvents) {
168162
}
169163

170164
func (c *Connections) evictClosedConnections(nowMilliseconds int64) {
171-
var closedConnections []closedConnection
172165
for id, conn := range c.connectionMap {
173166
if conn.ClosedAt == 0 {
174167
continue
175168
}
176169
if nowMilliseconds-conn.ClosedAt > closedConnectionMaxAge {
177170
delete(c.connectionMap, id)
178-
continue
179171
}
180-
closedConnections = append(closedConnections, closedConnection{
181-
id: id,
182-
closedAt: conn.ClosedAt,
183-
})
184-
}
185-
if len(closedConnections) <= maxClosedConnections {
186-
return
187-
}
188-
slices.SortFunc(closedConnections, func(left, right closedConnection) int {
189-
if left.closedAt < right.closedAt {
190-
return -1
191-
}
192-
if left.closedAt > right.closedAt {
193-
return 1
194-
}
195-
return strings.Compare(left.id, right.id)
196-
})
197-
for i := 0; i < len(closedConnections)-maxClosedConnections; i++ {
198-
delete(c.connectionMap, closedConnections[i].id)
199172
}
200173
}
201174

0 commit comments

Comments
 (0)