Added concurrency
This commit is contained in:
parent
b965115cb3
commit
4e51cfeba5
1 changed files with 30 additions and 9 deletions
|
|
@ -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 {
|
||||||
name := parseName(container)
|
go processCard(container, prices, finished)
|
||||||
edition := parseEdition(container)
|
}
|
||||||
price := parsePrice(container)
|
processed := 0
|
||||||
link := parseLink(container)
|
for {
|
||||||
cardPrice := buildCardPrice(name, edition, price, link)
|
c := <-finished
|
||||||
if isValidPrice(&cardPrice) {
|
processed++
|
||||||
prices = append(prices, cardPrice)
|
if c {
|
||||||
|
cardPrices = append(cardPrices, <-prices)
|
||||||
|
}
|
||||||
|
if processed == length {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return prices, nil
|
return cardPrices, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func processCard(container *html.Node, prices chan CardPrice, finished chan bool) {
|
||||||
|
name := parseName(container)
|
||||||
|
edition := parseEdition(container)
|
||||||
|
price := parsePrice(container)
|
||||||
|
link := parseLink(container)
|
||||||
|
cardPrice := buildCardPrice(name, edition, price, link)
|
||||||
|
if isValidPrice(&cardPrice) {
|
||||||
|
prices <- cardPrice
|
||||||
|
finished <- true
|
||||||
|
} else {
|
||||||
|
finished <- false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isValidPrice(price *CardPrice) bool {
|
func isValidPrice(price *CardPrice) bool {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue