1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

block main goroutine with chan communication

This commit is contained in:
George 2017-10-14 11:38:56 +08:00
parent c128dd84db
commit ed5b504e5b
4 changed files with 21 additions and 22 deletions

View File

@ -7,7 +7,6 @@ import (
"log"
"net/http"
"net/url"
"os"
"runtime/debug"
"strconv"
"strings"
@ -19,19 +18,13 @@ import (
type DNSPodHandler struct{}
func (handler *DNSPodHandler) DomainLoop(domain *Domain) {
func (handler *DNSPodHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) {
defer func() {
if err := recover(); err != nil {
panicCount++
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
fmt.Println(identifyPanic())
log.Print(identifyPanic())
if panicCount < PANIC_MAX {
log.Println("Got panic in goroutine, will start a new one... :", panicCount)
go handler.DomainLoop(domain)
} else {
os.Exit(1)
}
panicChan <- *domain
}
}()

View File

@ -22,7 +22,6 @@ var (
configuration Settings
optConf = flag.String("c", "./config.json", "Specify a config file")
optHelp = flag.Bool("h", false, "Show help")
panicCount = 0
)
func main() {
@ -53,10 +52,24 @@ func main() {
}
func dnsLoop() {
panicChan := make(chan Domain)
handler := createHandler(configuration.Provider)
for _, domain := range configuration.Domains {
go handler.DomainLoop(&domain)
go handler.DomainLoop(&domain, panicChan)
}
select {}
panicCount := 0
for {
select {
case failDomain := <-panicChan:
log.Println("Got panic in goroutine, will start a new one... :", panicCount)
go handler.DomainLoop(&failDomain, panicChan)
}
panicCount++
if panicCount >= PANIC_MAX {
os.Exit(1)
}
}
}

View File

@ -1,7 +1,7 @@
package main
type IHandler interface {
DomainLoop(domain *Domain)
DomainLoop(domain *Domain, panicChan chan<- Domain)
}
func createHandler(provider string) IHandler {

View File

@ -6,7 +6,6 @@ import (
"log"
"net/http"
"net/url"
"os"
"runtime/debug"
"strings"
"time"
@ -20,19 +19,13 @@ var (
type HEHandler struct{}
func (handler *HEHandler) DomainLoop(domain *Domain) {
func (handler *HEHandler) DomainLoop(domain *Domain, panicChan chan<- Domain) {
defer func() {
if err := recover(); err != nil {
panicCount++
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
fmt.Println(identifyPanic())
log.Print(identifyPanic())
if panicCount < PANIC_MAX {
log.Println("Got panic in goroutine, will start a new one... :", panicCount)
go handler.DomainLoop(domain)
} else {
os.Exit(1)
}
panicChan <- *domain
}
}()