-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
56 lines (48 loc) · 1.26 KB
/
pool.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
/*
This file use a const of workers ( i.e. goroutines )
for concurrently download the episodes.
Create two channels, scrape the episode URL (with FetchEpisodes),
and pass to the goroutines the Anime struct, which contains the
name and the URL of the mp4.
Then the goroutines download the file and send a signal when end.
*/
package main
import (
"fmt"
"strings"
)
// Number of goroutines
const workers = 10
// Spawn n goroutines that concurrently download the files
// streamed to the channel "in"
func Pool(epToDownload []string, anime *string) {
in := make(chan Anime)
done := make(chan struct{})
// Call the goroutines and let them in "listen"
// the channel in, when the goroutines finish send
// a message to "done" channel
for i := 0; i < workers; i++ {
go func() {
for ep := range in {
ok := strings.Contains(ep.URL, "m3u")
if ok {
DownloadM3U(ep)
} else {
DownloadMP4(ep)
}
}
done <- struct{}{}
}()
}
// Fetch the episode selected and stream the struct
for _, ep := range epToDownload {
FetchEpisodes(ep, in, *anime)
}
// Close the channel when finish the stream
close(in)
// Wait until all the goroutines send a "done" signal
for a := 0; a < workers; a++ {
<-done
}
fmt.Println("\nDONE!")
}