-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (85 loc) · 1.64 KB
/
Copy pathmain.go
File metadata and controls
116 lines (85 loc) · 1.64 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
package main
import (
"context"
"time"
"fmt"
"sync"
"os"
"os/signal"
"syscall"
"concurget/cmd"
"concurget/downloader"
"concurget/internal"
"concurget/metrics"
"concurget/worker"
"concurget/logger"
)
func main() {
config := cmd.ParseFlags()
ctx, cancel := context.WithTimeout(
context.Background(),
30*time.Second,
)
defer cancel()
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
os.Interrupt,
syscall.SIGTERM,
)
go func() {
<-signalChan
fmt.Println("\nReceived interrupt signal")
cancel()
}()
urls, err := internal.ReadURLs(config.File)
if err != nil {
logger.Error.Println(err)
return
}
jobs := make(chan string)
results := make(chan downloader.Result)
var wg sync.WaitGroup
for i := 1; i <= config.Workers; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
worker.Start(ctx, id, jobs, results)
}(i)
}
// Producer
go func() {
defer close(jobs)
for _, url := range urls {
select {
case <-ctx.Done():
return
case jobs <- url:
}
}
}()
// Close results after all workers finish
go func() {
wg.Wait()
close(results)
}()
m := metrics.Metrics{}
for result := range results {
m.Attempted++
if result.Err != nil {
m.Failure++
logger.Error.Println(result.Err)
continue
}
m.Success++
m.Bytes += result.Bytes
logger.Info.Printf("Downloaded %s (%d bytes)\n",
result.Filename,
result.Bytes)
}
fmt.Println("----------- Summary -----------")
fmt.Printf("Attempted : %d\n", m.Attempted)
fmt.Printf("Success : %d\n", m.Success)
fmt.Printf("Failure : %d\n", m.Failure)
fmt.Printf("Bytes : %d\n", m.Bytes)
}