Fix issue with client reconnect and improve client resiliency

This commit is contained in:
2024-06-27 11:54:14 +02:00
parent 51a94b6636
commit 7966b06f1b
2 changed files with 105 additions and 41 deletions

View File

@@ -8,8 +8,31 @@ import (
const WEBSOCKET_SERVER = "ws://youtube-download-ws-server.site.quack-lab.dev/ws"
const WEBSOCKET_SERVER_ALT = "ws://localhost:8080/ws"
func main() {
func init() {
// log.SetFlags(log.Lmicroseconds | log.Lshortfile)
log.SetFlags(log.Lmicroseconds)
}
// func instrument() {
// numGoroutines := runtime.NumGoroutine()
// var m runtime.MemStats
// runtime.ReadMemStats(&m)
// malloc := float64(m.Alloc)
// ramUsedMB := malloc / 1024 / 1024
// kbPerGoroutine := malloc / 1024 / float64(numGoroutines)
// log.Printf("Number of active goroutines: %d; RAM used: %.2f MB; KB per goroutine: %.2f", numGoroutines, ramUsedMB, kbPerGoroutine)
// }
func main() {
// go func() {
// for {
// instrument()
// time.Sleep(1 * time.Second)
// }
// }()
// res, err := http.Get(FULL_URL)
// if err != nil {
@@ -39,9 +62,18 @@ func main() {
// listener.Collections = []string{COLLECTION_NAME}
// listener.initialize()
ws := new(WSConnection)
ws.url = WEBSOCKET_SERVER
ws.Open()
var ws WSConnection
go func() {
for {
ws := WSConnection{
url: WEBSOCKET_SERVER_ALT,
}
ws.Open()
<-ws.Dead
log.Printf("Reconnecting in 5 seconds...")
time.Sleep(5 * time.Second)
}
}()
sem := make(chan struct{}, 4)
for {
@@ -50,7 +82,7 @@ func main() {
eventCopy := event
status := make(chan error)
sem <- struct{}{}
log.Printf("New event: %+v; semaphore at: %d", eventCopy, len(sem))
go func() {
defer func() {