@@ -48,10 +48,8 @@ func NewSocketLine(pid uint32, fd uint64) *SocketLine {
4848func (nl * SocketLine ) AddValue (timestamp uint64 , sockInfo * SockInfo ) {
4949 nl .mu .Lock ()
5050 defer nl .mu .Unlock ()
51- nl .Values = append (nl .Values , TimestampedSocket {Timestamp : timestamp , SockInfo : sockInfo })
52- sort .Slice (nl .Values , func (i , j int ) bool {
53- return nl .Values [i ].Timestamp < nl .Values [j ].Timestamp
54- })
51+
52+ nl .Values = insertIntoSortedSlice (nl .Values , TimestampedSocket {Timestamp : timestamp , SockInfo : sockInfo })
5553}
5654
5755func (nl * SocketLine ) GetValue (timestamp uint64 ) (* SockInfo , error ) {
@@ -92,47 +90,42 @@ func (nl *SocketLine) GetValue(timestamp uint64) (*SockInfo, error) {
9290
9391func (nl * SocketLine ) DeleteUnused () {
9492 // Delete socket lines that are not in use
95- ticker := time .NewTicker (1 * time .Minute )
96-
97- for range ticker .C {
98- func () {
99- nl .mu .Lock ()
100- defer nl .mu .Unlock ()
93+ nl .mu .Lock ()
94+ defer nl .mu .Unlock ()
10195
102- if len (nl .Values ) == 0 {
103- return
104- }
96+ if len (nl .Values ) == 0 {
97+ return
98+ }
10599
106- var lastMatchedReqTime uint64 = 0
100+ var lastMatchedReqTime uint64 = 0
107101
108- // traverse the slice backwards
109- for i := len (nl .Values ) - 1 ; i >= 0 ; i -- {
110- if nl .Values [i ].LastMatch != 0 && nl .Values [i ].LastMatch > lastMatchedReqTime {
111- lastMatchedReqTime = nl .Values [i ].LastMatch
112- }
113- }
102+ // traverse the slice backwards
103+ for i := len (nl .Values ) - 1 ; i >= 0 ; i -- {
104+ if nl .Values [i ].LastMatch != 0 && nl .Values [i ].LastMatch > lastMatchedReqTime {
105+ lastMatchedReqTime = nl .Values [i ].LastMatch
106+ }
107+ }
114108
115- if lastMatchedReqTime == 0 {
116- return
117- }
109+ if lastMatchedReqTime == 0 {
110+ return
111+ }
118112
119- // assumedInterval is inversely proportional to the number of requests being discarded
120- assumedInterval := uint64 (5 * time .Minute )
113+ // assumedInterval is inversely proportional to the number of requests being discarded
114+ assumedInterval := uint64 (5 * time .Minute )
121115
122- // delete all values that
123- // closed and its LastMatch + assumedInterval < lastMatchedReqTime
124- for i := len (nl .Values ) - 1 ; i >= 1 ; i -- {
125- if nl .Values [i ].SockInfo == nil &&
126- nl .Values [i - 1 ].SockInfo != nil &&
127- nl .Values [i - 1 ].LastMatch + assumedInterval < lastMatchedReqTime {
116+ // delete all values that
117+ // closed and its LastMatch + assumedInterval < lastMatchedReqTime
118+ for i := len (nl .Values ) - 1 ; i >= 1 ; i -- {
119+ if nl .Values [i ].SockInfo == nil &&
120+ nl .Values [i - 1 ].SockInfo != nil &&
121+ nl .Values [i - 1 ].LastMatch + assumedInterval < lastMatchedReqTime {
128122
129- // delete these two values
130- nl .Values = append (nl .Values [:i - 1 ], nl .Values [i + 1 :]... )
131- i -- // we deleted two values, so we need to decrement i by 2
132- }
133- }
134- }()
123+ // delete these two values
124+ nl .Values = append (nl .Values [:i - 1 ], nl .Values [i + 1 :]... )
125+ i -- // we deleted two values, so we need to decrement i by 2
126+ }
135127 }
128+
136129}
137130
138131func (nl * SocketLine ) GetAlreadyExistingSockets () {
@@ -305,3 +298,16 @@ const (
305298 stateEstablished = "01"
306299 stateListen = "0A"
307300)
301+
302+ func insertIntoSortedSlice (sortedSlice []TimestampedSocket , newItem TimestampedSocket ) []TimestampedSocket {
303+ idx := sort .Search (len (sortedSlice ), func (i int ) bool {
304+ return sortedSlice [i ].Timestamp >= newItem .Timestamp
305+ })
306+
307+ // Insert the new item at the correct position.
308+ sortedSlice = append (sortedSlice , TimestampedSocket {})
309+ copy (sortedSlice [idx + 1 :], sortedSlice [idx :])
310+ sortedSlice [idx ] = newItem
311+
312+ return sortedSlice
313+ }
0 commit comments