feat(core): add multithreading

This commit is contained in:
Romain J. 2024-03-19 17:39:10 +01:00
parent 3ac9b475e0
commit 65ee6e91f5
2 changed files with 53 additions and 6 deletions

49
app.go
View file

@ -3,15 +3,60 @@ package main
import ( import (
"GofileScrapper/api" "GofileScrapper/api"
"GofileScrapper/misc" "GofileScrapper/misc"
"GofileScrapper/structs"
"fmt" "fmt"
"sync"
) )
func main() { func run(guest structs.Guest) []structs.Content {
var contents []structs.Content
for i := 0; i < 10; i++ {
id := misc.GetRandomContent() id := misc.GetRandomContent()
content, err := api.GetContent(guest, id)
if err != nil {
misc.Logger.Error().Msg(err.Error())
} else {
contents = append(contents, content)
}
}
return contents
}
func main() {
var guests []structs.Guest
for i := 0; i < misc.GUEST_COUNT; i++ {
guest, err := api.GetGuest() guest, err := api.GetGuest()
if err != nil { if err != nil {
misc.Logger.Error().Msg(err.Error()) misc.Logger.Error().Msg(err.Error())
} else {
guests = append(guests, guest)
}
} }
fmt.Println(api.GetContent(guest, id)) 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)
}
} }

View file

@ -2,3 +2,5 @@ package misc
var CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" var CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var API_URL = "https://api.gofile.io" var API_URL = "https://api.gofile.io"
var GUEST_COUNT = 5