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

View File

@ -22,7 +22,6 @@ var (
configuration Settings configuration Settings
optConf = flag.String("c", "./config.json", "Specify a config file") optConf = flag.String("c", "./config.json", "Specify a config file")
optHelp = flag.Bool("h", false, "Show help") optHelp = flag.Bool("h", false, "Show help")
panicCount = 0
) )
func main() { func main() {
@ -53,10 +52,24 @@ func main() {
} }
func dnsLoop() { func dnsLoop() {
panicChan := make(chan Domain)
handler := createHandler(configuration.Provider) handler := createHandler(configuration.Provider)
for _, domain := range configuration.Domains { 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 package main
type IHandler interface { type IHandler interface {
DomainLoop(domain *Domain) DomainLoop(domain *Domain, panicChan chan<- Domain)
} }
func createHandler(provider string) IHandler { func createHandler(provider string) IHandler {

View File

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