douli1306 2019-05-05 19:14
浏览 177
已采纳

发送自定义信号到curreng Golang过程

I'm creating a HTTP server using Go. And whenever I work on a database maintenance, I want the server to redirect all the traffic to "current working on maintenance" page.

Currently, this is done by secret admin page (eg. http://myhome/secret) but I'm wondering if this can be done with signal -similar to TERM signal, but temporarily redirect rather than actually terminate the process.

Eg.

/home/myhome> nohup startServer &
... 
/home/myhome> changeMyServerStatus "maintenance"

I assume there will be two executable.. "startServer" and "changeMyServerStatus"

So, this is similar to a service. (like reload) But, is this possible? If so, can you give me some hint please?

Thank you

  • 写回答

1条回答 默认 最新

  • ds211107 2019-05-06 14:11
    关注

    As noted in the comments, signals may not be the best way to accomplish this. I’m assuming that you do want signals nonetheless.

    You can use the standard user signals: SIGUSR1 to enable maintenance and SIGUSR2 to disable it.

    Use os/signal to get notified of these signals and update program state:

    // Brief example code. Real code might be structured differently
    // (perhaps pack up maint and http.Server in one type MyServer).
    
    var maint uint32 // atomic: 1 if in maintenance mode
    
    func handleMaintSignals() {
        ch := make(chan os.Signal, 1)
        go func() { // FIXME: use Server.RegisterOnShutdown to terminate this
            for sig := range ch {
                switch sig { // FIXME: add logging
                case syscall.SIGUSR1:
                    atomic.StoreUint32(&maint, 1)
                case syscall.SIGUSR2:
                    atomic.StoreUint32(&maint, 0)
                }
            }
        }()
        signal.Notify(ch, syscall.SIGUSR1, syscall.SIGUSR2)
    }
    

    Have a middleware look at that state and respond accordingly:

    func withMaint(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            if atomic.LoadUint32(&maint) == 1 {
                http.Error(w, "Down for maintenance", http.StatusServiceUnavailable)
                return
            }
            next.ServeHTTP(w, r)
        })
    }
    

    You can apply this middleware on a per-route basis, or directly to the server’s root handler:

    func main() {
        handleMaintSignals()
        srv := http.Server{
            Addr:    ":17990",
            Handler: withMaint(http.DefaultServeMux),
        }
        srv.ListenAndServe()
    }
    

    You don’t need a second executable like changeMyServerStatus. Use your operating system’s tools to send signals, such as pkill:

    $ nohup myserver &
    
    $ curl http://localhost:17990/
    404 page not found
    
    $ pkill -USR1 myserver
    
    $ curl http://localhost:17990/
    Down for maintenance
    
    $ pkill -USR2 myserver
    
    $ curl http://localhost:17990/
    404 page not found
    

    But manually juggling nohup and pkill is tedious and error-prone. Instead, use a service manager such as systemd to manage your process. Systemd lets you send arbitrary signals with systemctl kill:

    systemctl kill -s SIGUSR1 myserver
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题