File tree 2 files changed +14
-5
lines changed
2 files changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ func main() {
15
15
target := flag .String ("t" , "" , "target url to proxy" )
16
16
size := flag .Int ("s" , 2 , "size of the pool" )
17
17
maxWait := flag .Duration ("w" , 3 * time .Second , "max wait time for a page rendering" )
18
+ autoFree := flag .Duration ("f" , 10 * time .Minute , "auto close each headless browser after the specified time" )
18
19
19
20
var bypassUAs StringsFlag = bartender .DefaultBypassUserAgentNames
20
21
flag .Var (& bypassUAs , "u" , "bypass the specified user-agent names" )
@@ -38,7 +39,7 @@ func main() {
38
39
b .BypassUserAgentNames (bypassUAs ... )
39
40
b .MaxWait (* maxWait )
40
41
b .WarmUp ()
41
- b .AutoFree ()
42
+ b .AutoFree (* autoFree )
42
43
43
44
err := http .ListenAndServe (* port , b )
44
45
if err != nil {
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import (
12
12
"time"
13
13
14
14
"github.com/go-rod/rod"
15
+ "github.com/go-rod/rod/lib/launcher"
15
16
"github.com/go-rod/rod/lib/proto"
16
17
"github.com/mileusna/useragent"
17
18
)
@@ -78,7 +79,10 @@ func (b *Bartender) getPage() *rod.Page {
78
79
}
79
80
80
81
func (b * Bartender ) newPage () * rod.Page {
81
- page := rod .New ().MustConnect ().MustPage ()
82
+ l := launcher .New ()
83
+ go l .Cleanup ()
84
+
85
+ page := rod .New ().ControlURL (l .MustLaunch ()).MustConnect ().MustPage ()
82
86
83
87
if len (b .blockRequests ) > 0 {
84
88
router := page .HijackRequests ()
@@ -106,18 +110,22 @@ func (b *Bartender) WarmUp() {
106
110
107
111
// AutoFree automatically closes the each headless browser after a period of time.
108
112
// It prevent the memory leak of the headless browser.
109
- func (b * Bartender ) AutoFree () {
113
+ func (b * Bartender ) AutoFree (interval time. Duration ) {
110
114
go func () {
111
115
for {
112
- time .Sleep (10 * time .Minute )
116
+ time .Sleep (interval )
117
+
118
+ page := b .getPage ()
119
+ browser := page .Browser ()
113
120
114
- err := b . getPage (). Browser () .Close ()
121
+ err := browser .Close ()
115
122
if err != nil {
116
123
log .Println ("failed to close browser:" , err )
117
124
118
125
continue
119
126
}
120
127
b .pool .Put (nil )
128
+ log .Println ("headless browser freed:" , page .SessionID )
121
129
}
122
130
}()
123
131
}
You can’t perform that action at this time.
0 commit comments