-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_stream_state.go
More file actions
57 lines (49 loc) · 1.48 KB
/
base_stream_state.go
File metadata and controls
57 lines (49 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package agentremote
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/beeper/agentremote/turns"
)
// BaseStreamState provides the common stream session fields and lifecycle
// methods shared across bridges that use turns.
type BaseStreamState struct {
StreamMu sync.Mutex
StreamSessions map[string]*turns.StreamSession
StreamFallbackToDebounced atomic.Bool
streamClosing atomic.Bool
}
// InitStreamState initialises the StreamSessions map. Call this during client
// construction.
func (s *BaseStreamState) InitStreamState() {
s.StreamSessions = make(map[string]*turns.StreamSession)
s.streamClosing.Store(false)
}
func (s *BaseStreamState) BeginStreamShutdown() {
s.streamClosing.Store(true)
}
func (s *BaseStreamState) ResetStreamShutdown() {
s.streamClosing.Store(false)
}
func (s *BaseStreamState) IsStreamShuttingDown() bool {
return s.streamClosing.Load()
}
// CloseAllSessions ends every active stream session and clears the map.
func (s *BaseStreamState) CloseAllSessions() {
s.BeginStreamShutdown()
s.StreamMu.Lock()
sessions := make([]*turns.StreamSession, 0, len(s.StreamSessions))
for _, sess := range s.StreamSessions {
if sess != nil {
sessions = append(sessions, sess)
}
}
s.StreamSessions = make(map[string]*turns.StreamSession)
s.StreamMu.Unlock()
for _, sess := range sessions {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
sess.End(ctx, turns.EndReasonDisconnect)
cancel()
}
}