1
Fork 0

Simplify server startup.

We now create the server and the listener in the calling thread,
and only create a new goroutine before calling server.Serve.
This commit is contained in:
Juliusz Chroboczek 2024-04-14 02:12:46 +02:00
parent 8fb1c1556e
commit 31f89163a2
3 changed files with 23 additions and 38 deletions

View File

@ -126,14 +126,10 @@ func main() {
ice.Update()
defer turnserver.Stop()
serverDone := make(chan struct{})
go func() {
err := webserver.Serve(httpAddr, group.DataDirectory)
if err != nil {
log.Printf("Server: %v", err)
}
close(serverDone)
}()
err = webserver.Serve(httpAddr, group.DataDirectory)
if err != nil {
log.Fatalf("Server: %v", err)
}
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
@ -158,8 +154,6 @@ func main() {
case <-terminate:
webserver.Shutdown()
return
case <-serverDone:
os.Exit(1)
}
}
}

View File

@ -7,7 +7,6 @@ import (
"os"
"strings"
"sync"
"time"
"encoding/json"
"net/http"
@ -19,13 +18,10 @@ import (
var setupOnce = sync.OnceFunc(func() {
Insecure = true
go func() {
err := Serve("localhost:1234", "")
if err != nil {
panic("could not start server")
}
}()
time.Sleep(100 * time.Millisecond)
err := Serve("localhost:1234", "")
if err != nil {
panic("could not start server")
}
})
func setupTest(dir, datadir string) error {

View File

@ -17,7 +17,6 @@ import (
"path/filepath"
"sort"
"strings"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@ -28,7 +27,7 @@ import (
"github.com/jech/galene/rtpconn"
)
var server atomic.Value
var server *http.Server
var StaticRoot string
@ -67,7 +66,7 @@ func Serve(address string, dataDir string) error {
group.Shutdown("server is shutting down")
})
server.Store(s)
server = s
proto := "tcp"
if strings.HasPrefix(address, "/") {
@ -78,18 +77,15 @@ func Serve(address string, dataDir string) error {
if err != nil {
return err
}
defer listener.Close()
if !Insecure {
err = s.ServeTLS(listener, "", "")
} else {
err = s.Serve(listener)
}
if err == http.ErrServerClosed {
return nil
}
return err
go func() {
defer listener.Close()
if !Insecure {
err = s.ServeTLS(listener, "", "")
} else {
err = s.Serve(listener)
}
}()
return nil
}
func cspHeader(w http.ResponseWriter, connect string) {
@ -755,12 +751,11 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr
}
func Shutdown() {
v := server.Load()
if v == nil {
return
if server == nil {
log.Printf("Shutting down nonexistent server")
}
s := v.(*http.Server)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
s.Shutdown(ctx)
server.Shutdown(ctx)
server = nil
}