-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (125 loc) · 2.48 KB
/
main.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
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
package main
import (
"bufio"
"context"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// url with status
type uws struct {
url string
status int
}
var (
maxConns = 2
maxCheckWorkers = 2
)
var (
connworkers = make(chan *connWorker, maxConns)
checkworkers = make(chan *checkWorker, maxCheckWorkers)
check = make(chan string, 10000)
checked = make(chan uws, 10000)
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatalf("couldn't start tcp server: %s", err)
}
log.Printf("Listening for tcp connections on :8080")
go writer()
go dispatcher()
for i := 0; i < maxConns; i++ {
connworkers <- &connWorker{}
}
for {
w := <-connworkers
conn, _ := ln.Accept()
go w.handle(conn)
}
}
type connWorker struct{}
func (w *connWorker) handle(c net.Conn) {
defer c.Close()
defer func() { connworkers <- w }()
for {
u, err := bufio.NewReader(c).ReadString('\n')
if err == io.EOF {
return
}
if err != nil {
return
}
u = strings.TrimSuffix(u, "\n")
check <- u
}
}
func dispatcher() {
// start up the checking workers
for i := 0; i < maxCheckWorkers; i++ {
w := &checkWorker{
id: i,
c: make(chan string),
}
go w.start()
}
for {
select {
case u := <-check:
w := <-checkworkers // wait for a worker
w.c <- u // send them a url to check
}
}
}
type checkWorker struct {
id int
c chan string
}
func (c *checkWorker) start() {
for {
checkworkers <- c // signal that we're ready for work
select {
case u := <-c.c:
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
status := c.check(ctx, u) // check the url
checked <- uws{u, status} // push to the results channel for writing
cancel()
}
}
}
func (c *checkWorker) check(ctx context.Context, u string) int {
fmt.Printf("[cw %d] checking url %s\n", c.id, u)
if _, err := url.Parse(u); err != nil {
return -1
}
req, err := http.NewRequest(http.MethodHead, u, nil)
if err != nil {
return -1
}
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return -1
}
return resp.StatusCode
}
func writer() {
fo, err := os.Create("urls.txt")
if err != nil {
log.Fatalf("couldn't open output file for writing: %s", err)
}
defer fo.Close()
w := bufio.NewWriter(fo)
for {
url := <-checked
w.WriteString(fmt.Sprintf("%s,%d\n", url.url, url.status))
// TODO: move flush to be periodic instead of after every string
w.Flush()
}
}