Commit 0a49dc45 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Store HTTP server in atomic.Value.

Keeps the race detector from complaining.
parent 4ff1151f
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
...@@ -25,7 +26,7 @@ import ( ...@@ -25,7 +26,7 @@ import (
"sfu/stats" "sfu/stats"
) )
var server *http.Server var server atomic.Value
var StaticRoot string var StaticRoot string
...@@ -50,19 +51,21 @@ func Serve(address string, dataDir string) error { ...@@ -50,19 +51,21 @@ func Serve(address string, dataDir string) error {
statsHandler(w, r, dataDir) statsHandler(w, r, dataDir)
}) })
server = &http.Server{ s := &http.Server{
Addr: address, Addr: address,
ReadHeaderTimeout: 60 * time.Second, ReadHeaderTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second, IdleTimeout: 120 * time.Second,
} }
server.RegisterOnShutdown(func() { s.RegisterOnShutdown(func() {
group.Range(func(g *group.Group) bool { group.Range(func(g *group.Group) bool {
go g.Shutdown("server is shutting down") go g.Shutdown("server is shutting down")
return true return true
}) })
}) })
err := server.ListenAndServeTLS( server.Store(s)
err := s.ListenAndServeTLS(
filepath.Join(dataDir, "cert.pem"), filepath.Join(dataDir, "cert.pem"),
filepath.Join(dataDir, "key.pem"), filepath.Join(dataDir, "key.pem"),
) )
...@@ -582,7 +585,12 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr ...@@ -582,7 +585,12 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr
} }
func Shutdown() { func Shutdown() {
v := server.Load()
if v == nil {
return
}
s := v.(*http.Server)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel() defer cancel()
server.Shutdown(ctx) s.Shutdown(ctx)
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment