Added concurrency
This commit is contained in:
parent
b965115cb3
commit
4e51cfeba5
1 changed files with 30 additions and 9 deletions
|
|
@ -28,19 +28,40 @@ func preprocessNameForSearch(name string) string {
|
|||
|
||||
func fetchPrices(doc *html.Node) ([]CardPrice, error) {
|
||||
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 {
|
||||
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)
|
||||
edition := parseEdition(container)
|
||||
price := parsePrice(container)
|
||||
link := parseLink(container)
|
||||
cardPrice := buildCardPrice(name, edition, price, link)
|
||||
if isValidPrice(&cardPrice) {
|
||||
prices = append(prices, cardPrice)
|
||||
prices <- cardPrice
|
||||
finished <- true
|
||||
} else {
|
||||
finished <- false
|
||||
}
|
||||
}
|
||||
return prices, nil
|
||||
}
|
||||
|
||||
func isValidPrice(price *CardPrice) bool {
|
||||
isValid := true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue