Resolve race condition issues

This commit is contained in:
Ben Sarmiento
2023-11-04 14:13:24 +01:00
parent b2e957cb4c
commit 4136310622
8 changed files with 136 additions and 26 deletions

View File

@@ -1,12 +1,17 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/mount"
"github.com/debridmediamanager.com/zurg/internal/net"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/hashicorp/golang-lru/v2/expirable"
@@ -26,9 +31,31 @@ func main() {
net.Router(mux, config, t, cache)
addr := fmt.Sprintf(":%s", config.GetPort())
log.Printf("Starting server on %s\n", addr)
err := http.ListenAndServe(addr, mux)
if err != nil {
log.Panicf("Failed to start server: %v", err)
}
server := &http.Server{Addr: addr, Handler: mux}
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
go func() {
log.Printf("Starting server on %s\n", addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Panicf("Failed to start server: %v", err)
}
}()
// Start the mount in a goroutine.
go func() {
if err := mount.Mount(config.GetMountPath()); err != nil {
log.Panicf("Failed to mount: %v", err)
}
}()
<-shutdown
log.Println("zurg signing off...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(ctx)
}