/
session_manager.go
140 lines (126 loc) · 3.15 KB
/
session_manager.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"fmt"
"log/slog"
"sync"
"time"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
const (
clientTimeout = 10 * time.Minute
)
// var errDuplicateSession = errors.New("duplicate capture session")
type sessionRequest struct {
replyChan chan<- replyStruct
src string
mode string
}
type sessionMgr struct {
muSessions sync.RWMutex
ctrlChan chan<- *sessionRequest
sessions map[uuid.UUID]*session
parent *ghoeqServer
}
func NewSessionManager() *sessionMgr {
return &sessionMgr{
sessions: make(map[uuid.UUID]*session),
}
}
func (s *sessionMgr) genSessionID() uuid.UUID {
return uuid.New()
}
// Go manages goroutine lifetime for the sessions and message brokers
func (sm *sessionMgr) Go(ctx context.Context, gs *ghoeqServer) error {
sm.parent = gs
g, wctx := errgroup.WithContext(ctx)
g.Go(func() error {
return sm.requestHandler(wctx)
})
return g.Wait()
}
// requestHandler runs the handler loop to manage capture sessions.
func (s *sessionMgr) requestHandler(ctx context.Context) error {
g, wctx := errgroup.WithContext(ctx)
sc := make(chan *sessionRequest)
s.ctrlChan = sc
var done bool
for !done {
select {
case <-wctx.Done():
done = true
case r, ok := <-sc:
if !ok {
break
}
if err := s.handleRequest(wctx, r, g); err != nil {
return err
}
}
}
return g.Wait()
}
func (sm *sessionMgr) handleRequest(ctx context.Context, r *sessionRequest, g *errgroup.Group) error {
switch r.mode {
case "Start":
// TODO: add duplicate checking
g.Go(func() error {
return sm.runCapture(ctx, r.src, r.replyChan) // TODO: test capture first so we can return error to client instead of killing server. :)
})
return nil
case "Stop":
r.replyChan <- replyStruct{
err: fmt.Errorf("unimplemented"),
}
close(r.replyChan)
}
return fmt.Errorf("unimplemented")
}
// runCapture does the actual work of starting a capture session and holding the work goroutines.
func (sm *sessionMgr) runCapture(ctx context.Context, src string, c chan<- replyStruct) error {
sm.muSessions.Lock()
index := sm.genSessionID()
s := NewSession(index, src)
sm.sessions[index] = s
sm.muSessions.Unlock()
defer sm.cleanSession(index)
c <- replyStruct{
reply: index,
}
close(c)
slog.Info("starting capture", "source", src)
return s.Run(ctx, src)
}
// cleanSession removes the capture session from tracking and any cleanup.
func (s *sessionMgr) cleanSession(i uuid.UUID) {
s.muSessions.Lock()
delete(s.sessions, i)
s.muSessions.Unlock()
}
func (sm *sessionMgr) GracefulStop() {
sm.muSessions.RLock()
defer sm.muSessions.RUnlock()
for _, ses := range sm.sessions {
ses.Close()
}
}
func (sm *sessionMgr) AttachToSession(ctx context.Context, sid string) (*sessionClient, error) {
var once sync.Once
sm.muSessions.RLock()
defer once.Do(sm.muSessions.RUnlock)
if sid == "0" {
// Add default session if possible
return nil, fmt.Errorf("default session logic not implemented yet")
}
uid, err := uuid.Parse(sid)
if err != nil {
return nil, err
}
s, ok := sm.sessions[uid]
if !ok {
return nil, fmt.Errorf("session %s does not exist", sid)
}
once.Do(sm.muSessions.RUnlock)
return s.AddClient(ctx)
}