close channels so that everything dies properly

This commit is contained in:
stryan 2021-01-03 16:19:49 -05:00
parent 77cf328955
commit 665d42fc51
3 changed files with 33 additions and 4 deletions

View File

@ -68,8 +68,20 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
<-ticker.C
if mumble.State() != 2 {
log.Println("Lost mumble connection " + strconv.Itoa(int(mumble.State())))
die <- true
m.Close <- true
select {
default:
close(die)
case <-die:
//die is already closed
}
select {
default:
close(m.Close)
case <-m.Close:
//m.Close is already closed
}
return
}
}
}()
@ -81,6 +93,9 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
log.Println("\nGot internal die request. Terminating Mumble-Bridge")
dgv.Disconnect()
det.Detach()
close(die)
close(m.Close)
close(toMumble)
Bridge.Connected = false
}
}

View File

@ -137,8 +137,14 @@ func discordReceivePCM(v *discordgo.VoiceConnection, die chan bool) {
lastReady = true
readyTimeout.Stop()
}
p, ok := <-v.OpusRecv
var ok bool
var p *discordgo.Packet
select {
case p, ok = <-v.OpusRecv:
case <-die:
log.Println("killing discord ReceivePCM")
return
}
if !ok {
log.Println("Opus not ok")
continue

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
@ -101,6 +102,13 @@ func main() {
userCount := make(chan int)
go pingMumble(*mumbleAddr, strconv.Itoa(*mumblePort), userCount)
go discordStatusUpdate(discord, userCount)
go func() {
for {
time.Sleep(3 * time.Second)
log.Println(runtime.NumGoroutine())
//pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}
}()
if *autoMode {
Bridge.AutoChan = make(chan bool)
go AutoBridge(discord)