Commit 996a895 1 parent fbad720 commit 996a895 Copy full SHA for 996a895
File tree 2 files changed +46
-9
lines changed
2 files changed +46
-9
lines changed Original file line number Diff line number Diff line change 6
6
7
7
"github.com/chromedp/cdproto/page"
8
8
"github.com/chromedp/chromedp"
9
+ "github.com/markoczy/crawler/types"
9
10
"golang.org/x/exp/errors/fmt"
10
11
)
11
12
@@ -19,30 +20,26 @@ type NavigateAndWaitLoadedAction struct {
19
20
}
20
21
21
22
func (action * NavigateAndWaitLoadedAction ) Do (ctx context.Context ) error {
22
- loadErr := make ( chan error , 10 )
23
+ chErr := types . NewErrorSwitchChannel ( )
23
24
go func () {
24
- defer recoverChannelClosed ()
25
25
time .Sleep (action .timeout )
26
- loadErr <- fmt .Errorf ("Timeout while loading DOM Content" )
26
+ chErr . Send ( fmt .Errorf ("Timeout while loading DOM Content" ) )
27
27
}()
28
28
chromedp .ListenTarget (ctx , func (v interface {}) {
29
- defer recoverChannelClosed ()
30
29
switch v .(type ) {
31
30
case * page.EventDomContentEventFired :
32
- loadErr <- nil
31
+ chErr . Send ( nil )
33
32
}
34
33
})
35
34
go func () {
36
- defer recoverChannelClosed ()
37
35
err := chromedp .Run (ctx , chromedp.Tasks {
38
36
chromedp .Navigate (action .url ),
39
37
})
40
38
if err != nil {
41
- loadErr <- err
39
+ chErr . Send ( err )
42
40
}
43
41
}()
44
- err := <- loadErr
45
- close (loadErr )
42
+ err := chErr .Receive ()
46
43
return err
47
44
}
48
45
Original file line number Diff line number Diff line change
1
+ package types
2
+
3
+ import (
4
+ "sync"
5
+ )
6
+
7
+ type ErrorSwitchChannel interface {
8
+ Send (err error )
9
+ Receive () error
10
+ }
11
+
12
+ type errorSwitchChannel struct {
13
+ done bool
14
+ err error
15
+ mux sync.Mutex
16
+ errCh chan error
17
+ }
18
+
19
+ func (ch * errorSwitchChannel ) Send (err error ) {
20
+ ch .mux .Lock ()
21
+ if ! ch .done {
22
+ ch .errCh <- err
23
+ close (ch .errCh )
24
+ ch .done = true
25
+ }
26
+ ch .mux .Unlock ()
27
+ }
28
+
29
+ func (ch * errorSwitchChannel ) Receive () error {
30
+ return <- ch .errCh
31
+ }
32
+
33
+ func NewErrorSwitchChannel () ErrorSwitchChannel {
34
+ return & errorSwitchChannel {
35
+ done : false ,
36
+ err : nil ,
37
+ mux : sync.Mutex {},
38
+ errCh : make (chan error , 1 ),
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments