Last active
December 1, 2020 23:58
-
-
Save mdnurahmed/887baf985be2109aa410b650dc973638 to your computer and use it in GitHub Desktop.
peculiar crawler - 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"sync" | |
"time" | |
) | |
const ( | |
noOfWorker = 10000 | |
userRequiredd = 350 | |
minAge = 10 | |
maxAge = 12 | |
timeLimit = 3 | |
) | |
type User struct { | |
Name string | |
Age int | |
} | |
func worker(id int, wg *sync.WaitGroup, ch chan User) { | |
defer wg.Done() | |
url := fmt.Sprintf("http://localhost:8080/user/%d", id) | |
client := http.Client{ | |
Timeout: 5 * time.Second, | |
} | |
res, err := client.Get(url) | |
if err != nil { | |
return | |
} | |
data, err := ioutil.ReadAll(res.Body) | |
defer res.Body.Close() | |
if err != nil { | |
return | |
} | |
var user User | |
err = json.Unmarshal(data, &user) | |
if err != nil { | |
return | |
} | |
if user.Age >= minAge && user.Age <= maxAge { | |
ch <- user | |
} | |
} | |
func terminator(wg *sync.WaitGroup, termination chan bool) { | |
wg.Wait() | |
termination <- true | |
} | |
func printResult(myusers []User) { | |
fmt.Printf("total users found : %d\n", len(myusers)) | |
for _, user := range myusers { | |
fmt.Printf("user name :%s \nage :%d\n\n", user.Name, user.Age) | |
} | |
} | |
func main() { | |
var myusers []User | |
ch := make(chan User) | |
termination := make(chan bool) | |
var wg sync.WaitGroup | |
for i := 1; i <= noOfWorker; i++ { | |
wg.Add(1) | |
go worker(i, &wg, ch) | |
} | |
go terminator(&wg, termination) | |
for true { | |
select { | |
case res := <-ch: | |
myusers = append(myusers, res) | |
if len(myusers) == userRequiredd { | |
printResult(myusers) | |
return | |
} | |
case <-termination: | |
log.Println("All Worker Finished") | |
printResult(myusers) | |
return | |
case <-time.After(timeLimit * time.Second): | |
log.Println("Time Up") | |
printResult(myusers) | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment