-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchrome_lifecycle.go
More file actions
303 lines (275 loc) · 8.14 KB
/
Copy pathchrome_lifecycle.go
File metadata and controls
303 lines (275 loc) · 8.14 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package browser
import (
"fmt"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
)
// getBrowser returns the current browser under a read lock.
func (m *ChromeManager) getBrowser() *rod.Browser {
m.mu.RLock()
defer m.mu.RUnlock()
return m.browser
}
// getGuard returns the current connection's egress guard under a read lock
// — reconnect() replaces both m.browser and m.guard together, so this must
// use the same locking discipline as getBrowser to avoid handing a caller a
// guard from a since-replaced connection.
func (m *ChromeManager) getGuard() *egressGuard {
m.mu.RLock()
defer m.mu.RUnlock()
return m.guard
}
// startDisconnectWatcher monitors the CDP connection for unexpected disconnects.
// When the browser's WebSocket closes (Chrome crash, network drop), rod's
// EachEvent goroutine exits. We detect this and close LostConnection unless
// Close() has already closed closingGracefully. chromedp pattern.
func (m *ChromeManager) startDisconnectWatcher() {
go func() {
b := m.getBrowser()
if b == nil {
return
}
// Wait for the browser's event loop to exit — this happens when the
// CDP WebSocket closes. rod doesn't expose a direct "disconnected" channel,
// but we can poll Connected() or use a heartbeat.
// Simpler: use a ticker to poll browser connectivity every 5s.
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-m.closingGracefully:
return
case <-ticker.C:
if !m.Connected() {
// Connection lost — check if it's intentional.
select {
case <-m.closingGracefully:
return // intentional close, don't fire LostConnection
default:
}
select {
case <-m.LostConnection:
// already closed
default:
close(m.LostConnection)
}
return
}
// Active health probe: try a no-op CDP call.
b := m.getBrowser()
if b == nil {
continue
}
if _, err := (&proto.BrowserGetVersion{}).Call(b); err != nil {
select {
case <-m.closingGracefully:
return
default:
}
select {
case <-m.LostConnection:
default:
close(m.LostConnection)
}
return
}
}
}
}()
}
// reconnect closes the old connection and establishes a new one.
// The slow CDP work (discover, connect, install guard) runs WITHOUT holding m.mu
// so concurrent getBrowser()/getGuard() callers are not blocked and never see a
// nil browser during the reconnect window. The old browser stays readable until
// the new one is ready, then we atomically swap both browser and guard under lock.
func (m *ChromeManager) reconnect() error {
// Serialize reconnect — only one at a time.
m.reconnectMu.Lock()
defer m.reconnectMu.Unlock()
// Snapshot the old browser under lock, then release the lock for the slow work.
m.mu.RLock()
oldBrowser := m.browser
m.mu.RUnlock()
// Close the old browser outside the lock — may take seconds.
if oldBrowser != nil {
_ = oldBrowser.Close()
}
debuggerURL, err := discoverWSURL(m.wsURL)
if err != nil {
return fmt.Errorf("discover ws url: %w", err)
}
b := rod.New().ControlURL(debuggerURL)
if err := b.Connect(); err != nil {
return fmt.Errorf("connect: %w", err)
}
// Re-install the egress guard on the fresh connection — it does NOT
// carry over from the closed browser (see egress_guard.go). Fail the
// reconnect rather than resume operation unguarded.
guard, err := installEgressGuard(b)
if err != nil {
_ = b.Close()
return fmt.Errorf("%w", err)
}
// Atomically swap browser + guard under lock. Callers never see nil.
m.mu.Lock()
m.browser = b
m.guard = guard
m.keepaliveCtxID = ""
pool := m.pool
// Recreate LostConnection for the new connection — the old one may have been closed.
m.LostConnection = make(chan struct{})
m.mu.Unlock()
if pool != nil {
pool.UpdateBrowser(b)
} else {
// No pool yet — create one. This is the only path that creates a new pool.
m.mu.Lock()
if m.pool == nil {
m.pool = NewContextPool(b)
}
m.mu.Unlock()
}
// Restart the disconnect watcher on the new connection.
m.startDisconnectWatcher()
return nil
}
// Connected reports whether the Chrome connection is active.
func (m *ChromeManager) Connected() bool {
m.mu.RLock()
defer m.mu.RUnlock()
return m.browser != nil
}
// HealthStatus is the result of a ChromeManager health probe.
// #49: Returned by HealthCheck() for the /health endpoint and external monitors.
type HealthStatus struct {
Connected bool `json:"connected"`
WsURL string `json:"ws_url"`
LatencyMs int64 `json:"latency_ms"`
ContextPool *PoolStats `json:"context_pool,omitempty"`
}
// PoolStats is a snapshot of the ContextPool state for health reporting.
type PoolStats struct {
Contexts int `json:"contexts"`
Pages int `json:"pages"`
Generation uint64 `json:"generation"`
}
// HealthCheck performs an active CDP health probe and returns the status.
// If the browser is not connected, returns a disconnected status without
// attempting a CDP call (which would block).
func (m *ChromeManager) HealthCheck() HealthStatus {
m.mu.RLock()
b := m.browser
m.mu.RUnlock()
status := HealthStatus{
Connected: b != nil,
WsURL: m.wsURL,
}
if b == nil {
return status
}
// Active health probe: measure CDP round-trip latency.
start := time.Now()
if _, err := (&proto.BrowserGetVersion{}).Call(b); err != nil {
// CDP call failed — connection is stale even though browser != nil.
status.Connected = false
return status
}
status.LatencyMs = time.Since(start).Milliseconds()
// Gather pool stats if available.
if pool := m.Pool(); pool != nil {
pool.contextsMu.RLock()
ctxCount := len(pool.contexts)
pageCount := 0
for _, mc := range pool.contexts {
mc.Mu.Lock()
pageCount += len(mc.Pages)
mc.Mu.Unlock()
}
pool.contextsMu.RUnlock()
status.ContextPool = &PoolStats{
Contexts: ctxCount,
Pages: pageCount,
Generation: pool.generation.Load(),
}
}
return status
}
// Close disconnects from Chrome and releases resources.
// Signals closingGracefully so the disconnect watcher doesn't fire LostConnection.
// Sets browser to nil under lock so concurrent callers see the shutdown state,
// then closes the old browser outside the lock (may take seconds).
func (m *ChromeManager) Close() {
// Signal intentional close — disconnect watcher checks this before firing LostConnection.
select {
case <-m.closingGracefully:
default:
close(m.closingGracefully)
}
m.mu.Lock()
b := m.browser
m.browser = nil
m.guard = nil
ctxID := m.keepaliveCtxID
m.keepaliveCtxID = ""
m.mu.Unlock()
if b != nil {
if ctxID != "" {
_ = proto.TargetDisposeBrowserContext{BrowserContextID: ctxID}.Call(b)
}
_ = b.Close()
}
}
// DefaultContext returns the browser's default context (persistent profile).
// Cookies, localStorage, and other state from manual login sessions are available.
// No isolation — all requests share the same cookies.
func (m *ChromeManager) DefaultContext() (*rod.Browser, error) {
b := m.getBrowser()
if b == nil {
return nil, ErrUnavailable
}
scoped := b.NoDefaultDevice()
scoped.BrowserContextID = "" // empty = default context (persistent profile)
return scoped, nil
}
// PageInfo describes an open browser page/tab.
type PageInfo struct {
ID string `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
Title string `json:"title"`
Context string `json:"context"` // "default", "keepalive", or context ID
}
// ListPages returns all open pages/tabs with their context info.
func (m *ChromeManager) ListPages() ([]PageInfo, error) {
b := m.getBrowser()
if b == nil {
return nil, ErrUnavailable
}
targets, err := proto.TargetGetTargets{}.Call(b)
if err != nil {
return nil, fmt.Errorf("list targets: %w", err)
}
var pages []PageInfo
for _, t := range targets.TargetInfos {
if t.Type != "page" {
continue
}
ctx := "default"
if t.BrowserContextID != "" {
if t.BrowserContextID == m.keepaliveCtxID {
ctx = "keepalive"
} else {
ctx = string(t.BrowserContextID)
}
}
pages = append(pages, PageInfo{
ID: string(t.TargetID),
Type: string(t.Type),
URL: t.URL,
Title: t.Title,
Context: ctx,
})
}
return pages, nil
}