feat(core): add multithreading
This commit is contained in:
parent
3ac9b475e0
commit
65ee6e91f5
2 changed files with 53 additions and 6 deletions
57
app.go
57
app.go
|
@ -3,15 +3,60 @@ package main
|
|||
import (
|
||||
"GofileScrapper/api"
|
||||
"GofileScrapper/misc"
|
||||
"GofileScrapper/structs"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
id := misc.GetRandomContent()
|
||||
guest, err := api.GetGuest()
|
||||
if err != nil {
|
||||
misc.Logger.Error().Msg(err.Error())
|
||||
func run(guest structs.Guest) []structs.Content {
|
||||
var contents []structs.Content
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
id := misc.GetRandomContent()
|
||||
content, err := api.GetContent(guest, id)
|
||||
|
||||
if err != nil {
|
||||
misc.Logger.Error().Msg(err.Error())
|
||||
} else {
|
||||
contents = append(contents, content)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(api.GetContent(guest, id))
|
||||
return contents
|
||||
}
|
||||
|
||||
func main() {
|
||||
var guests []structs.Guest
|
||||
|
||||
for i := 0; i < misc.GUEST_COUNT; i++ {
|
||||
guest, err := api.GetGuest()
|
||||
if err != nil {
|
||||
misc.Logger.Error().Msg(err.Error())
|
||||
} else {
|
||||
guests = append(guests, guest)
|
||||
}
|
||||
}
|
||||
|
||||
results := make(chan structs.Content)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, guest := range guests {
|
||||
wg.Add(1)
|
||||
go func(guest structs.Guest) {
|
||||
defer wg.Done()
|
||||
for _, content := range run(guest) {
|
||||
results <- content
|
||||
}
|
||||
}(guest)
|
||||
}
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(results)
|
||||
}()
|
||||
|
||||
for content := range results {
|
||||
fmt.Println("Content:", content)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,3 +2,5 @@ package misc
|
|||
|
||||
var CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
var API_URL = "https://api.gofile.io"
|
||||
|
||||
var GUEST_COUNT = 5
|
||||
|
|
Loading…
Reference in a new issue