zoukankan      html  css  js  c++  java
  • golang服务优雅的退出

    参考goframe框架,重要的以下几个信号量.

    // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
    //
    // This Source Code Form is subject to the terms of the MIT License.
    // If a copy of the MIT was not distributed with this file,
    // You can obtain one at https://github.com/gogf/gf.
    
    // +build !windows
    
    package ghttp
    
    import (
    	"context"
    	"github.com/gogf/gf/internal/intlog"
    	"os"
    	"os/signal"
    	"syscall"
    )
    
    // procSignalChan is the channel for listening the signal.
    var procSignalChan = make(chan os.Signal)
    
    // handleProcessSignal handles all signal from system.
    func handleProcessSignal() {
    	var sig os.Signal
    	signal.Notify(
    		procSignalChan,
    		syscall.SIGINT,
    		syscall.SIGQUIT,
    		syscall.SIGKILL,
    		syscall.SIGTERM,
    		syscall.SIGABRT,
    		syscall.SIGUSR1,
    		syscall.SIGUSR2,
    	)
    	for {
    		sig = <-procSignalChan
    		intlog.Printf(context.TODO(), `signal received: %s`, sig.String())
    		switch sig {
    		// Shutdown the servers.
    		case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGABRT:
    			shutdownWebServers(sig.String())
    			return
    
    		// Shutdown the servers gracefully.
    		// Especially from K8S when running server in POD.
    		case syscall.SIGTERM:
    			shutdownWebServersGracefully(sig.String())
    			return
    
    		// Restart the servers.
    		case syscall.SIGUSR1:
    			if err := restartWebServers(sig.String()); err != nil {
    				intlog.Error(context.TODO(), err)
    			}
    			return
    
    		default:
    		}
    	}
    }
    
  • 相关阅读:
    题解:艾米利亚的魔法
    tarjan求割点
    集合删数
    小测题解
    [考试]20141028
    铺地毯
    [考试]20141027
    大家好
    【DM642学习笔记一】关于Can't Initialize Target CPU的一种解决方法 : Error 0x80000240
    iOS开发之获取系统相册ALAssetLibrary
  • 原文地址:https://www.cnblogs.com/jiftle/p/15311449.html
Copyright © 2011-2022 走看看