Skip to content

Commit

Permalink
Reload list of proxy URLs on SIGHUP.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Aug 31, 2020
1 parent eeb3d35 commit 116188c
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 32 deletions.
48 changes: 40 additions & 8 deletions src/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"runtime"
runtimepprof "runtime/pprof"
"strings"
"syscall"
"time"

"github.com/dlintw/goconf"
Expand All @@ -47,7 +48,7 @@ import (
var (
version = "unreleased"

config = flag.String("config", "server.conf", "config file to use")
configFlag = flag.String("config", "server.conf", "config file to use")

cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

Expand Down Expand Up @@ -98,8 +99,9 @@ func main() {
os.Exit(0)
}

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
signal.Notify(sigChan, syscall.SIGHUP)

if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
Expand Down Expand Up @@ -127,7 +129,7 @@ func main() {

log.Printf("Starting up version %s/%s as pid %d", version, runtime.Version(), os.Getpid())

config, err := goconf.ReadConfigFile(*config)
config, err := goconf.ReadConfigFile(*configFlag)
if err != nil {
log.Fatal("Could not read configuration: ", err)
}
Expand Down Expand Up @@ -184,8 +186,22 @@ func main() {
log.Printf("Could not initialize %s MCU at %s (%s) will retry in %s", mcuType, mcuUrl, err, mcuRetry)
mcuRetryTimer.Reset(mcuRetry)
select {
case <-interrupt:
log.Fatalf("Cancelled")
case sig := <-sigChan:
switch sig {
case os.Interrupt:
log.Fatalf("Cancelled")
case syscall.SIGHUP:
log.Printf("Received SIGHUP, reloading %s", *configFlag)
if config, err = goconf.ReadConfigFile(*configFlag); err != nil {
log.Printf("Could not read configuration from %s: %s", *configFlag, err)
} else {
mcuUrl, _ = config.GetString("mcu", "url")
mcuType, _ = config.GetString("mcu", "type")
if mcuType == "" {
mcuType = signaling.McuTypeDefault
}
}
}
case <-mcuRetryTimer.C:
// Retry connection
mcuRetry = mcuRetry * 2
Expand Down Expand Up @@ -290,6 +306,22 @@ func main() {
}
}

<-interrupt
log.Println("Interrupted")
loop:
for {
select {
case sig := <-sigChan:
switch sig {
case os.Interrupt:
log.Println("Interrupted")
break loop
case syscall.SIGHUP:
log.Printf("Received SIGHUP, reloading %s", *configFlag)
if config, err := goconf.ReadConfigFile(*configFlag); err != nil {
log.Printf("Could not read configuration from %s: %s", *configFlag, err)
} else {
hub.Reload(config)
}
}
}
}
}
6 changes: 6 additions & 0 deletions src/signaling/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ func (h *Hub) Stop() {
}
}

func (h *Hub) Reload(config *goconf.ConfigFile) {
if h.mcu != nil {
h.mcu.Reload(config)
}
}

func reverseSessionId(s string) (string, error) {
// Note that we are assuming base64 encoded strings here.
decoded, err := base64.URLEncoding.DecodeString(s)
Expand Down
3 changes: 3 additions & 0 deletions src/signaling/mcu_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package signaling
import (
"fmt"

"github.com/dlintw/goconf"

"golang.org/x/net/context"
)

Expand Down Expand Up @@ -55,6 +57,7 @@ type McuInitiator interface {
type Mcu interface {
Start() error
Stop()
Reload(config *goconf.ConfigFile)

SetOnConnected(func())
SetOnDisconnected(func())
Expand Down
3 changes: 3 additions & 0 deletions src/signaling/mcu_janus.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ func (m *mcuJanus) Stop() {
m.reconnectTimer.Stop()
}

func (m *mcuJanus) Reload(config *goconf.ConfigFile) {
}

func (m *mcuJanus) SetOnConnected(f func()) {
if f == nil {
f = emptyOnConnected
Expand Down
Loading

0 comments on commit 116188c

Please sign in to comment.