@@ -25,21 +25,23 @@ type AISession struct {
25
25
}
26
26
27
27
type AISessionPool struct {
28
- selector BroadcastSessionsSelector
29
- sessMap map [string ]* BroadcastSession
30
- inUseSess []* BroadcastSession
31
- suspender * suspender
32
- penalty int
33
- mu sync.RWMutex
28
+ selector BroadcastSessionsSelector
29
+ sessMap map [string ]* BroadcastSession
30
+ inUseSess []* BroadcastSession
31
+ sessionsOnHold map [string ][]* BroadcastSession
32
+ suspender * suspender
33
+ penalty int
34
+ mu sync.RWMutex
34
35
}
35
36
36
37
func NewAISessionPool (selector BroadcastSessionsSelector , suspender * suspender , penalty int ) * AISessionPool {
37
38
return & AISessionPool {
38
- selector : selector ,
39
- sessMap : make (map [string ]* BroadcastSession ),
40
- suspender : suspender ,
41
- penalty : penalty ,
42
- mu : sync.RWMutex {},
39
+ selector : selector ,
40
+ sessMap : make (map [string ]* BroadcastSession ),
41
+ sessionsOnHold : make (map [string ][]* BroadcastSession ),
42
+ suspender : suspender ,
43
+ penalty : penalty ,
44
+ mu : sync.RWMutex {},
43
45
}
44
46
}
45
47
@@ -135,6 +137,31 @@ func (pool *AISessionPool) Remove(sess *BroadcastSession) {
135
137
pool .suspender .suspend (sess .Transcoder (), penalty )
136
138
}
137
139
140
+ func (pool * AISessionPool ) PutSessionOnHold (id string , sess * BroadcastSession ) {
141
+ pool .mu .Lock ()
142
+ defer pool .mu .Unlock ()
143
+
144
+ var sessionList []* BroadcastSession
145
+ holdList , ok := pool .sessionsOnHold [id ]
146
+ if ok {
147
+ sessionList = holdList
148
+ }
149
+ pool .sessionsOnHold [id ] = append (sessionList , sess )
150
+
151
+ delete (pool .sessMap , sess .Transcoder ())
152
+ pool .inUseSess = removeSessionFromList (pool .inUseSess , sess )
153
+ }
154
+
155
+ func (pool * AISessionPool ) ReleaseSessionsOnHold (id string ) {
156
+ sessions , ok := pool .sessionsOnHold [id ]
157
+ if ! ok {
158
+ return
159
+ }
160
+
161
+ pool .Add (sessions )
162
+ delete (pool .sessionsOnHold , id )
163
+ }
164
+
138
165
func (pool * AISessionPool ) Size () int {
139
166
pool .mu .RLock ()
140
167
defer pool .mu .RUnlock ()
@@ -245,7 +272,8 @@ func (sel *AISessionSelector) Select(ctx context.Context) *AISession {
245
272
246
273
// Refresh if the # of sessions across warm and cold pools falls below the smaller of the maxRefreshSessionsThreshold and
247
274
// 1/2 the total # of orchs that can be queried during discovery
248
- if sel .warmPool .Size ()+ sel .coldPool .Size () < int (math .Min (maxRefreshSessionsThreshold , math .Ceil (float64 (discoveryPoolSize )/ 2.0 ))) {
275
+ size := sel .warmPool .Size () + sel .coldPool .Size ()
276
+ if size < int (math .Min (maxRefreshSessionsThreshold , math .Ceil (float64 (discoveryPoolSize )/ 2.0 ))) {
249
277
return true
250
278
}
251
279
@@ -293,6 +321,30 @@ func (sel *AISessionSelector) Remove(sess *AISession) {
293
321
}
294
322
}
295
323
324
+ func (sel * AISessionSelector ) PutSessionOnHold (id string , sess * AISession ) {
325
+ // start cleanup func on first session on hold for request
326
+ _ , warmPoolCleanupStarted := sel .warmPool .sessionsOnHold [id ]
327
+ _ , coldPoolCleanupStarted := sel .coldPool .sessionsOnHold [id ]
328
+ if ! warmPoolCleanupStarted && ! coldPoolCleanupStarted {
329
+ go func (id string , sel * AISessionSelector ) {
330
+ time .Sleep (sel .node .AIProcesssingRetryTimeout )
331
+ sel .ReleaseSessionsOnHold (id )
332
+ }(id , sel )
333
+ }
334
+
335
+ // put session on hold
336
+ if sess .Warm {
337
+ sel .warmPool .PutSessionOnHold (id , sess .BroadcastSession )
338
+ } else {
339
+ sel .coldPool .PutSessionOnHold (id , sess .BroadcastSession )
340
+ }
341
+ }
342
+
343
+ func (sel * AISessionSelector ) ReleaseSessionsOnHold (id string ) {
344
+ sel .warmPool .ReleaseSessionsOnHold (id )
345
+ sel .coldPool .ReleaseSessionsOnHold (id )
346
+ }
347
+
296
348
func (sel * AISessionSelector ) Refresh (ctx context.Context ) error {
297
349
// If we try to add new sessions to the pool the suspender
298
350
// should treat this as a refresh
@@ -421,6 +473,17 @@ func (c *AISessionManager) Remove(ctx context.Context, sess *AISession) error {
421
473
return nil
422
474
}
423
475
476
+ func (c * AISessionManager ) PutSessionOnHold (ctx context.Context , id string , sess * AISession ) error {
477
+ sel , err := c .getSelector (ctx , sess .Cap , sess .ModelID )
478
+ if err != nil {
479
+ return err
480
+ }
481
+
482
+ sel .PutSessionOnHold (id , sess )
483
+
484
+ return nil
485
+ }
486
+
424
487
func (c * AISessionManager ) Complete (ctx context.Context , sess * AISession ) error {
425
488
sel , err := c .getSelector (ctx , sess .Cap , sess .ModelID )
426
489
if err != nil {
0 commit comments