summaryrefslogtreecommitdiff
path: root/classic.go
blob: 9adb5bfa5af41fbc164ec3c6b3c42d20a1e7a0d9 (plain)
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
package main

import (
	"fmt"
	"log"
	"net/url"
)

func classic(startURL url.URL, maxConcurrency, maxURLs int) {
	worklist := make(chan []url.URL)
	var numPendingSends int

	numPendingSends++
	go func() {
		worklist <- []url.URL{startURL}
	}()

	// Crawl the web concurrently.
	seen := make(map[url.URL]bool)
	count := 1

	for ; numPendingSends > 0; numPendingSends-- {
		batch := <-worklist
		for _, u := range batch {
			if !seen[u] {
				fmt.Printf("%d. %s\n", count, &u)
				count++

				seen[u] = true

				numPendingSends++
				go func() {
					worklist <- getBatch(u)
				}()
			}
		}
	}
}

func getBatch(u url.URL) []url.URL {
	doc, err := fetch(u)
	if err != nil {
		log.Print(err)
	}

	batch := findURLs(u, doc)

	return batch
}