MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / GC

Method GC

internal/utils/counters/counter.go:148–195  ·  view source on GitHub ↗

GC garbage expired items

()

Source from the content-addressed store, hash-verified

146
147// GC garbage expired items
148func (this *Counter[T]) GC() {
149 this.gcLocker.Lock()
150 var gcIndex = this.gcIndex
151
152 this.gcIndex++
153 if this.gcIndex >= int(this.countMaps) {
154 this.gcIndex = 0
155 }
156
157 this.gcLocker.Unlock()
158
159 var currentTime = fasttime.Now().Unix()
160
161 this.locker.RLock(gcIndex)
162 var itemMap = this.itemMaps[gcIndex]
163 var expiredKeys = []uint64{}
164 for key, item := range itemMap {
165 if item.IsExpired(currentTime) {
166 expiredKeys = append(expiredKeys, key)
167 }
168 }
169 var tooManyItems = len(itemMap) > maxItemsPerGroup // prevent too many items
170 this.locker.RUnlock(gcIndex)
171
172 if len(expiredKeys) > 0 {
173 this.locker.Lock(gcIndex)
174 for _, key := range expiredKeys {
175 delete(itemMap, key)
176 }
177 this.locker.Unlock(gcIndex)
178 }
179
180 if tooManyItems {
181 this.locker.Lock(gcIndex)
182 var count = len(itemMap) - maxItemsPerGroup
183 if count > 0 {
184 itemMap = this.itemMaps[gcIndex]
185 for key := range itemMap {
186 delete(itemMap, key)
187 count--
188 if count < 0 {
189 break
190 }
191 }
192 }
193 this.locker.Unlock(gcIndex)
194 }
195}
196
197func (this *Counter[T]) CountMaps() int {
198 return int(this.countMaps)

Callers 1

WithGCMethod · 0.95

Calls 7

NowFunction · 0.92
UnixMethod · 0.80
RLockMethod · 0.80
RUnlockMethod · 0.80
LockMethod · 0.45
UnlockMethod · 0.45
IsExpiredMethod · 0.45

Tested by

no test coverage detected