github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_server_admin_unix.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  //go:build !windows
     8  // +build !windows
     9  
    10  package ghttp
    11  
    12  import (
    13  	"context"
    14  	"github.com/gogf/gf/internal/intlog"
    15  	"os"
    16  	"os/signal"
    17  	"syscall"
    18  )
    19  
    20  // procSignalChan is the channel for listening the signal.
    21  var procSignalChan = make(chan os.Signal)
    22  
    23  // handleProcessSignal handles all signal from system.
    24  func handleProcessSignal() {
    25  	var sig os.Signal
    26  	signal.Notify(
    27  		procSignalChan,
    28  		syscall.SIGINT,
    29  		syscall.SIGQUIT,
    30  		syscall.SIGKILL,
    31  		syscall.SIGTERM,
    32  		syscall.SIGABRT,
    33  		syscall.SIGUSR1,
    34  		syscall.SIGUSR2,
    35  	)
    36  	for {
    37  		sig = <-procSignalChan
    38  		intlog.Printf(context.TODO(), `signal received: %s`, sig.String())
    39  		switch sig {
    40  		// Shutdown the servers.
    41  		case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGABRT:
    42  			shutdownWebServers(sig.String())
    43  			return
    44  
    45  		// Shutdown the servers gracefully.
    46  		// Especially from K8S when running server in POD.
    47  		case syscall.SIGTERM:
    48  			shutdownWebServersGracefully(sig.String())
    49  			return
    50  
    51  		// Restart the servers.
    52  		case syscall.SIGUSR1:
    53  			if err := restartWebServers(sig.String()); err != nil {
    54  				intlog.Error(context.TODO(), err)
    55  			}
    56  			return
    57  
    58  		default:
    59  		}
    60  	}
    61  }