拦截信号Golang应用优雅关闭的操作方法

Golang不是像C语言的系统级编程语言,但仍提供了以下特性帮助开发者与底层操作系统进行交互,如信号(singals),os/singals包实现了这些功能。相对于其他语言处理OS信号采用复杂或冗余的方法,Golang内置OS包提供了易用的方法处理Unix信号,非常直接、简单。事实上,类unix系统中通常处理singal是必要的,本文介绍singal概念,并通过示例说明其应用场景。

从示例开始

假设场景:Golang应用在关闭时打印消息:“Thank you for using Golang.” 首先新建main函数,函数体模拟执行一些业务,直到接收到结束命令。

func main() {
 for {
 fmt.Println("Doing Work")
 time.Sleep(1 * time.Second)
 }
}

当运行应用,然后从OS发送执行kill信号(Ctrl + C),可能输出结果类似这样:

Doing Work
Doing Work
Doing Work
Process finished with exit code 2

现在我们希望拦截kill信号并定义处理逻辑:打印必要的关闭信息。

接收信号

首先创建channe接收来自操作系统的命令,os包提供了signal接口处理基于OS规范信号实现。

killSignal := make(chan os.Signal, 1)

为了通知killSignal,需使用signal包中提供的Notify函数,第一个参数为Signal类型的channel,下一个参数接收一组发给通道的信号列表。

Notify(c chan<- os.Signal, sig ...os.Signal)

我们也可以使用syscall包通知特定命令的信号:

signal.Notify(c chan<- os.Signal, syscall.SIGINT, syscall.SIGTERM)

为了处理信号,我们在main函数中使用killSignal通道等待interrupt信号,一旦接收到来自OS的命令,则打印结束消息并接收应用。现在把处理业务的循环代码移入独立的goroute中,这里定义一个匿名函数:

go func() {
 for {
 fmt.Println("Doing Work")
 time.Sleep(1 * time.Second)
 }
}()

因为业务代码运行在独立routine中,main函数实现等待killSignal信号并在结束之前打印消息。

<-killSignal
fmt.Println("Thanks for using Golang!")

完整代码

下面把几个部分组装在一起,完整代码如下:

package main

import (
	"fmt"
	"os"
	"os/signal"
	"time"
)

func main() {

	killSignal := make(chan os.Signal, 1)
	signal.Notify(killSignal, os.Interrupt)
	go func() {
	for {
	fmt.Println("Doing Work")
	time.Sleep(1 * time.Second)
	}
	}()
	<-killSignal
	fmt.Println("Thanks for using Golang!")
}

运行程序,看到一直执行业务循环,直到接收到interrupt信号,打印消息并结束:

Doing Work
Doing Work
Doing Work
Thanks for using Golang!

常见信号

对于命令行程序,当按CTRL+C时,会发送SIGINT信号,SIGINT是与指定特定信号数字相关联的名称。信号实际是可以有程序生成的软件中断,也是一种异步处理事件机制。通常为了安全起见,信号采用名称而不是数值生成。

大多数信号及其具体动作都是由操作系统定义的,并还在操作系统权限范围内执行相应功能。因此能够处理某些事件并不能让用户自由地使用系统,有些信号即使生成了也会被操作系统忽略,而由操作系统决定程序允许处理哪些信号。

举例,SIGKILL 和 SIGSTOP 信号也可以被捕获、阻塞或忽略。因为这些信号对于保持操作系统功能的"健全"至关重要,在极端条件下为内核和root用户提供了停止进程的方法。与SIGKILL相关联的数字是9,Linux提供了kill命令发送SIGTERM信号,终止正在运行的程序。可以使用kill -l命令查看完整的支持信号:

图片

有许多信号可用,但并非所有信号都能被程序处理。像SIGKILL和SIGSTOP这样的信号既不能被程序调用,也不能被程序忽略。原因很简单:它们太重要了,不允许被恶意程序滥用。

系统信号分为同步信号和异步信号。其中同步信号是程序执行中的错误触发的信号,如SIGBUS, SIGFPE, SIGSEGV,在 Golang 程序中,同步信号通常会被转换为 runtime panic。同步信号需要事件及事件时间,相反异步信号不需要于时钟同步,异步信号是系统内核或其它程序发送的信号。举例,SIGHUP表示它控制的终端被关闭,常用的是当按CTRL+C时,会发送SIGINT信号。当SIGINT信号有键盘产生时,实际上不是终止应用程序,而是生成特定键盘中断,并且处理该中断的程序开始执行,其缺省行为时关闭应用程序,但我们可以覆盖缺省行为,写我们自己的业务处理逻辑。

事实上许多信号处理都可以被覆盖,Go开发者可以编写自己的处理程序来自定义信号处理行为。然而,除非有非常好的理由,否则改变信号的默认行为并不是明智的想法。这里引用列举常用linux信号:

SIGHUP: ‘HUP’ = ‘hung up’. This signal is generated when a controlling process dies or hangs up detected on the controlling terminal.
SIGINT: When a process is interrupted from keyboard by pressing CTRL+C
SIGQUIT: Quit from keyboard
SIGILL: Illegal instruction. A synonym for SIGPWR() – power failure
SIGABRT: Program calls the abort() function – an emergency stop.
SIGBUS: Bad memory access. Attempt was made to access memory inappropriately
SIGFPE: FPE = Floating point exception
SIGKILL: The kill signal. The process was explicitly killed.
SIGUSR1: This signal is open for programmers to write a custom behavior.
SIGSEGV: Invalid memory reference. In C when we try to access memory beyond array limit, this signal is generated.
SIGUSR2: This signal is open for programmers to write a custom behavior.
SIGPIPE: This signals us open for programmers to write a custom behavior.
SIGALRM: Process requested a wake up call by the operating system such as by calling the alarm() function.
SIGTERM: A process is killed

处理多个信号

下面再看一个示例,较上面的示例增加了对SIGTERM信号的处理:

package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
)

func handler(signal os.Signal) {
	if signal == syscall.SIGTERM {
	fmt.Println("Got kill signal. ")
	fmt.Println("Program will terminate now.")
	os.Exit(0)
	} else if signal == syscall.SIGINT {
	fmt.Println("Got CTRL+C signal")
	fmt.Println("Closing.")
	os.Exit(0)
	} else {
	fmt.Println("Ignoring signal: ", signal)
	}
}

func main() {
	sigchnl := make(chan os.Signal, 1)
	signal.Notify(sigchnl)
 // signal.Notify(sigchnl, os.Interrupt, syscall.SIGTERM)
	exitchnl := make(chan int)

	go func() {
	for {
	s := <-sigchnl
	handler(s)
	}
	}()

	exitcode := <-exitchnl
	os.Exit(exitcode)
}

首先创建通道接收响应信号,我们也可以指定信号类型:signal.Notify(sigchnl, os.Interrupt, syscall.SIGTERM),否则会接收所有信号,包括命令窗口改变大小信号。然后在handle函数判断信号类型并分别进行处理。

程序运行后,linux可以通过ps -h 查看go程序,然后执行kill命令。

NotifyContext示例

Go.1.16提供了signal.NotifyContext函数,可以实现更优雅的关闭功能。下面示例实现简单web服务器,示例handle需要10s处理时间,如果正在运行的web服务,客户端通过postman或cURL发送请求,然后在服务端立刻通过ctrl+C发送终止信号。
我们希望看到服务器在终止之前通过响应终止请求而优雅地关闭。如果关闭时间过长,可以发送另一个中断信号立即退出,或者暂停将在5秒后开始。

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"time"
)

var (
	server http.Server
)

func main() {
	// Create context that listens for the interrupt signal from the OS.
	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()

	server = http.Server{
	Addr: ":8080",
	}

	// Perform application startup.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	time.Sleep(time.Second * 10)
	fmt.Fprint(w, "Hello world!")
	})

	// Listen on a different Goroutine so the application doesn't stop here.
	go server.ListenAndServe()

	// Listen for the interrupt signal.
	<-ctx.Done()

	// Restore default behavior on the interrupt signal and notify user of shutdown.
	stop()
	fmt.Println("shutting down gracefully, press Ctrl+C again to force")

	// Perform application shutdown with a maximum timeout of 5 seconds.
	timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := server.Shutdown(timeoutCtx); err != nil {
	fmt.Println(err)
	}
}

总结

本文介绍了信号的概念及常用信号,并给出了应用广泛的几个示例,例如优雅地关闭应用服务、在命令行应用中接收终止命令。

作者:梦想画家原文地址:https://blog.csdn.net/neweastsun/article/details/128812053

%s 个评论

要回复文章请先登录注册