Added concurrency

This commit is contained in:
Artyom Belousov 2019-11-22 17:39:44 +03:00
parent b965115cb3
commit 4e51cfeba5

View file

@ -28,18 +28,39 @@ func preprocessNameForSearch(name string) string {
func fetchPrices(doc *html.Node) ([]CardPrice, error) { func fetchPrices(doc *html.Node) ([]CardPrice, error) {
priceContainers := getPriceContainers(doc) priceContainers := getPriceContainers(doc)
prices := make([]CardPrice, 0) length := len(priceContainers)
prices := make(chan CardPrice, length)
finished := make(chan bool, length)
cardPrices := make([]CardPrice, 0)
for _, container := range priceContainers { for _, container := range priceContainers {
go processCard(container, prices, finished)
}
processed := 0
for {
c := <-finished
processed++
if c {
cardPrices = append(cardPrices, <-prices)
}
if processed == length {
break
}
}
return cardPrices, nil
}
func processCard(container *html.Node, prices chan CardPrice, finished chan bool) {
name := parseName(container) name := parseName(container)
edition := parseEdition(container) edition := parseEdition(container)
price := parsePrice(container) price := parsePrice(container)
link := parseLink(container) link := parseLink(container)
cardPrice := buildCardPrice(name, edition, price, link) cardPrice := buildCardPrice(name, edition, price, link)
if isValidPrice(&cardPrice) { if isValidPrice(&cardPrice) {
prices = append(prices, cardPrice) prices <- cardPrice
finished <- true
} else {
finished <- false
} }
}
return prices, nil
} }
func isValidPrice(price *CardPrice) bool { func isValidPrice(price *CardPrice) bool {