From 52ea3cb39896cb5ce5f6de303865bc28a14aa545 Mon Sep 17 00:00:00 2001 From: 0xffffharry <95022881+0xffffharry@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:09:18 +0800 Subject: [PATCH] update --- cmd/cdns/main.go | 12 ++++++++++++ constant/debug.go | 3 +++ core/core.go | 14 +++++++------- listener/common.go | 15 +++++++++------ 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 constant/debug.go diff --git a/cmd/cdns/main.go b/cmd/cdns/main.go index 496a5a5..ba970aa 100644 --- a/cmd/cdns/main.go +++ b/cmd/cdns/main.go @@ -4,6 +4,7 @@ import ( "context" "os" "os/signal" + "strconv" "strings" "syscall" @@ -29,6 +30,14 @@ var MainCommand = &cobra.Command{ var configPath string func init() { + // + { + e, err := strconv.ParseBool(os.Getenv("CDNS_LISTENER_ENABLE_PANIC")) + if err == nil && e { + constant.ListenerEnablePainc = true + } + } + // MainCommand.PersistentFlags().StringVarP(&configPath, "config", "c", "config.yaml", "config file path") MainCommand.AddCommand(versionCommand) } @@ -55,6 +64,9 @@ func run() int { coreLogger.Infof("cdns %s", constant.Version) coreLogger.Infof("plugin matcher: %s", strings.Join(plugin.PluginMatcherTypes(), ", ")) coreLogger.Infof("plugin executor: %s", strings.Join(plugin.PluginExecutorTypes(), ", ")) + if constant.ListenerEnablePainc { + coreLogger.Infof("debug: listener enable painc") + } go signalHandle(cancel, coreLogger) err = c.Run() if err != nil { diff --git a/constant/debug.go b/constant/debug.go new file mode 100644 index 0000000..ebeca42 --- /dev/null +++ b/constant/debug.go @@ -0,0 +1,3 @@ +package constant + +var ListenerEnablePainc = false diff --git a/core/core.go b/core/core.go index 2ba482a..36c4174 100644 --- a/core/core.go +++ b/core/core.go @@ -256,7 +256,7 @@ func (c *Core) Run() error { err = starter.Start() if err != nil { err = fmt.Errorf("start upstream[%s] failed: %s", u.Tag(), err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } } @@ -267,7 +267,7 @@ func (c *Core) Run() error { err = c.ntpServer.Start() if err != nil { err = fmt.Errorf("start ntp server failed: %s", err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } defer func() { @@ -302,7 +302,7 @@ func (c *Core) Run() error { err = starter.Start() if err != nil { err = fmt.Errorf("start plugin matcher[%s] failed: %s", pm.Tag(), err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } } @@ -332,7 +332,7 @@ func (c *Core) Run() error { err = starter.Start() if err != nil { err = fmt.Errorf("start plugin executor[%s] failed: %s", pe.Tag(), err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } } @@ -343,7 +343,7 @@ func (c *Core) Run() error { err = w.Check() if err != nil { err = fmt.Errorf("check workflow[%s] failed: %s", w.Tag(), err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } } @@ -369,7 +369,7 @@ func (c *Core) Run() error { err = starter.Start() if err != nil { err = fmt.Errorf("start listener[%s] failed: %s", l.Tag(), err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } } @@ -387,7 +387,7 @@ func (c *Core) Run() error { err = c.apiServer.Start() if err != nil { err = fmt.Errorf("start api server failed: %s", err) - c.rootLogger.Fatal(err) + c.coreLogger.Fatal(err) return err } } diff --git a/listener/common.go b/listener/common.go index 1691df5..cca7a30 100644 --- a/listener/common.go +++ b/listener/common.go @@ -15,6 +15,7 @@ import ( "time" "github.com/rnetx/cdns/adapter" + "github.com/rnetx/cdns/constant" "github.com/rnetx/cdns/log" "github.com/rnetx/cdns/utils" @@ -157,12 +158,14 @@ func listenerHandle(ctx context.Context, listener string, logger log.Logger, wor ctx = adapter.SaveLogContext(ctx, dnsCtx) messageInfo := reqMessageInfo(req) logger.InfofContext(ctx, "new request: %s", messageInfo) - defer func() { - err := recover() - if err != nil { - logger.FatalfContext(ctx, "handle request failed: %s, error(painc): %s", messageInfo, err) - } - }() + if !constant.ListenerEnablePainc { + defer func() { + err := recover() + if err != nil { + logger.FatalfContext(ctx, "handle request failed: %s, error(painc): %s", messageInfo, err) + } + }() + } _, err := workflow.Exec(ctx, dnsCtx) if err != nil { logger.ErrorfContext(ctx, "handle request failed: %s, error: %s", messageInfo, err)