@@ -12,14 +12,11 @@ import (
1212 "time"
1313)
1414
15- const EvictionInterval = 15 * time .Minute
16- const GCInterval = 1 * time .Minute
17-
1815type AsyncSearchStorageInMemory struct {
1916 idToResult * concurrent.Map [string , * AsyncRequestResult ]
2017}
2118
22- func NewAsyncSearchStorageInMemory () AsyncSearchStorageInMemory {
19+ func NewAsyncSearchStorageInMemory () AsyncSearchStorageInMemory { // change result type to AsyncRequestResultStorage interface
2320 return AsyncSearchStorageInMemory {
2421 idToResult : concurrent .NewMap [string , * AsyncRequestResult ](),
2522 }
@@ -54,6 +51,19 @@ func (s AsyncSearchStorageInMemory) SizeInBytes() int {
5451 return size
5552}
5653
54+ func (s AsyncSearchStorageInMemory ) evict (timeFun func (time.Time ) time.Duration ) {
55+ var ids []asyncQueryIdWithTime
56+ s .Range (func (key string , value * AsyncRequestResult ) bool {
57+ if timeFun (value .added ) > EvictionInterval {
58+ ids = append (ids , asyncQueryIdWithTime {id : key , time : value .added })
59+ }
60+ return true
61+ })
62+ for _ , id := range ids {
63+ s .Delete (id .id )
64+ }
65+ }
66+
5767type AsyncQueryContextStorageInMemory struct {
5868 idToContext * concurrent.Map [string , * AsyncQueryContext ]
5969}
@@ -68,40 +78,9 @@ func (s AsyncQueryContextStorageInMemory) Store(id string, context *AsyncQueryCo
6878 s .idToContext .Store (id , context )
6979}
7080
71- type AsyncQueriesEvictor struct {
72- ctx context.Context
73- cancel context.CancelFunc
74- AsyncRequestStorage AsyncSearchStorageInMemory
75- AsyncQueriesContexts AsyncQueryContextStorageInMemory
76- }
77-
78- func NewAsyncQueriesEvictor (AsyncRequestStorage AsyncSearchStorageInMemory , AsyncQueriesContexts AsyncQueryContextStorageInMemory ) * AsyncQueriesEvictor {
79- ctx , cancel := context .WithCancel (context .Background ())
80- return & AsyncQueriesEvictor {ctx : ctx , cancel : cancel , AsyncRequestStorage : AsyncRequestStorage , AsyncQueriesContexts : AsyncQueriesContexts }
81- }
82-
83- func elapsedTime (t time.Time ) time.Duration {
84- return time .Since (t )
85- }
86-
87- type asyncQueryIdWithTime struct {
88- id string
89- time time.Time
90- }
91-
92- func (e * AsyncQueriesEvictor ) tryEvictAsyncRequests (timeFun func (time.Time ) time.Duration ) {
93- var ids []asyncQueryIdWithTime
94- e .AsyncRequestStorage .Range (func (key string , value * AsyncRequestResult ) bool {
95- if timeFun (value .added ) > EvictionInterval {
96- ids = append (ids , asyncQueryIdWithTime {id : key , time : value .added })
97- }
98- return true
99- })
100- for _ , id := range ids {
101- e .AsyncRequestStorage .idToResult .Delete (id .id )
102- }
81+ func (s AsyncQueryContextStorageInMemory ) evict (timeFun func (time.Time ) time.Duration ) {
10382 var asyncQueriesContexts []* AsyncQueryContext
104- e . AsyncQueriesContexts .idToContext .Range (func (key string , value * AsyncQueryContext ) bool {
83+ s .idToContext .Range (func (key string , value * AsyncQueryContext ) bool {
10584 if timeFun (value .added ) > EvictionInterval {
10685 if value != nil {
10786 asyncQueriesContexts = append (asyncQueriesContexts , value )
@@ -111,7 +90,7 @@ func (e *AsyncQueriesEvictor) tryEvictAsyncRequests(timeFun func(time.Time) time
11190 })
11291 evictedIds := make ([]string , 0 )
11392 for _ , asyncQueryContext := range asyncQueriesContexts {
114- e . AsyncQueriesContexts .idToContext .Delete (asyncQueryContext .id )
93+ s .idToContext .Delete (asyncQueryContext .id )
11594 if asyncQueryContext .cancel != nil {
11695 evictedIds = append (evictedIds , asyncQueryContext .id )
11796 asyncQueryContext .cancel ()
@@ -122,22 +101,13 @@ func (e *AsyncQueriesEvictor) tryEvictAsyncRequests(timeFun func(time.Time) time
122101 }
123102}
124103
125- func (e * AsyncQueriesEvictor ) AsyncQueriesGC () {
126- defer recovery .LogPanic ()
127- for {
128- select {
129- case <- e .ctx .Done ():
130- logger .Debug ().Msg ("evictor stopped" )
131- return
132- case <- time .After (GCInterval ):
133- e .tryEvictAsyncRequests (elapsedTime )
134- }
135- }
104+ func elapsedTime (t time.Time ) time.Duration {
105+ return time .Since (t )
136106}
137107
138- func ( e * AsyncQueriesEvictor ) Close () {
139- e . cancel ()
140- logger . Info (). Msg ( "AsyncQueriesEvictor Stopped" )
108+ type asyncQueryIdWithTime struct {
109+ id string
110+ time time. Time
141111}
142112
143113type AsyncQueryTraceLoggerEvictor struct {
0 commit comments