124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
const WEBSOCKET_SERVER = "ws://youtube-download-ws-server.site.quack-lab.dev/ws"
|
|
const WEBSOCKET_SERVER_ALT = "ws://localhost:8080/ws"
|
|
|
|
func init() {
|
|
// log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
|
log.SetFlags(log.Lmicroseconds)
|
|
}
|
|
|
|
// func instrument() {
|
|
// numGoroutines := runtime.NumGoroutine()
|
|
|
|
// var m runtime.MemStats
|
|
// runtime.ReadMemStats(&m)
|
|
// // log.Printf("%+v", m)
|
|
|
|
// sys := float64(m.Sys)
|
|
// ramUsedMB := sys / 1024 / 1024
|
|
// kbPerGoroutine := sys / 1024 / float64(numGoroutines)
|
|
|
|
// var numGoroutinesPretty string
|
|
// switch {
|
|
// case numGoroutines >= 1_000_000:
|
|
// numGoroutinesPretty = fmt.Sprintf("%.2fM", float64(numGoroutines)/1_000_000)
|
|
// case numGoroutines >= 1_000:
|
|
// numGoroutinesPretty = fmt.Sprintf("%.2fk", float64(numGoroutines)/1_000)
|
|
// default:
|
|
// numGoroutinesPretty = fmt.Sprintf("%d", numGoroutines)
|
|
// }
|
|
|
|
// log.Printf("Number of active goroutines: %d (%s); RAM used: %.2f MB; KB per goroutine: %.2f", numGoroutines, numGoroutinesPretty, ramUsedMB, kbPerGoroutine)
|
|
// }
|
|
|
|
func main() {
|
|
// go func() {
|
|
// for {
|
|
// instrument()
|
|
// time.Sleep(1 * time.Second)
|
|
// }
|
|
// }()
|
|
|
|
// res, err := http.Get(FULL_URL)
|
|
// if err != nil {
|
|
// log.Fatal(err)
|
|
// }
|
|
// defer res.Body.Close()
|
|
// body, err := io.ReadAll(res.Body)
|
|
// if err != nil {
|
|
// log.Printf("Error reading response body: %+v\n", err)
|
|
// return
|
|
// }
|
|
// if res.StatusCode != http.StatusOK {
|
|
// log.Printf("Non-OK HTTP status: %d\nResponse body: %s\n", res.StatusCode, body)
|
|
// return
|
|
// }
|
|
|
|
// var data APIResponse
|
|
// err = json.Unmarshal(body, &data)
|
|
// if err != nil {
|
|
// log.Printf("Error unmarshaling JSON: %+v\n", err)
|
|
// return
|
|
// }
|
|
// log.Printf("Data: %+v\n", data)
|
|
|
|
// listener := new(RealtimeListener)
|
|
// listener.Url = POCKETBASE_REALTIME
|
|
// listener.Collections = []string{COLLECTION_NAME}
|
|
// listener.initialize()
|
|
|
|
var ws WSConnection
|
|
read := make(chan string)
|
|
go func() {
|
|
for {
|
|
ws = WSConnection{
|
|
url: WEBSOCKET_SERVER_ALT,
|
|
}
|
|
ws.Open()
|
|
for {
|
|
msg, ok := <-ws.ReadChan
|
|
if !ok {
|
|
break
|
|
}
|
|
read <- msg
|
|
}
|
|
<-ws.Dead
|
|
log.Printf("Reconnecting in 5 seconds...")
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
}()
|
|
|
|
sem := make(chan struct{}, 4)
|
|
for {
|
|
select {
|
|
case event := <-read:
|
|
eventCopy := event
|
|
status := make(chan error)
|
|
sem <- struct{}{}
|
|
|
|
log.Printf("New event: %+v; semaphore at: %d", eventCopy, len(sem))
|
|
go func() {
|
|
defer func() {
|
|
<-sem
|
|
log.Printf("Semaphore at: %d", len(sem))
|
|
}()
|
|
// Download(eventCopy, status)
|
|
DownloadURL(eventCopy, status)
|
|
// go DownloadNative(event, status)
|
|
for status := range status {
|
|
log.Printf("Status: %s\n", status)
|
|
}
|
|
}()
|
|
case <-time.After(1 * time.Minute):
|
|
// Perform some action or simply continue to avoid deadlock
|
|
log.Println("Consumer is alive, but has no new events.")
|
|
}
|
|
}
|
|
}
|