优雅地停止服务

5年以前  |  阅读数:285 次  |  编程语言:Golang 
package main

import (
    "fmt"
    "net"
    "net/http"
    "os"
    "os/signal"
    "sync/atomic"
    "syscall"
    "time"
)

func slow(res http.ResponseWriter, req *http.Request) {
    fmt.Println("respond at=start")
    time.Sleep(time.Second * 5)
    res.Header().Set("Content-Type", "text/plain")
    fmt.Fprintln(res, "Finally.")
    fmt.Println("respond at=finish")
}

var connCount int64 = 0

type watchedConn struct {
    net.Conn
}

func (w *watchedConn) Close() error {
    atomic.AddInt64(&connCount, -1)
    return w.Conn.Close()
}

type watchedListener struct {
    net.Listener
}

func (l *watchedListener) Accept() (net.Conn, error) {
    conn, err := l.Listener.Accept()
    if err != nil {
        return nil, err
    }
    atomic.AddInt64(&connCount, 1)
    return &watchedConn{Conn: conn}, nil
}

func main() {
    stop := make(chan bool, 1)
    sig := make(chan os.Signal, 1)

    handler := http.HandlerFunc(slow)
    server := &http.Server{Handler: handler}
    fmt.Println("listen at=start")
    listener, listenErr := net.Listen("tcp", ":5000")
    if listenErr != nil {
        panic(listenErr)
    }
    wListener := &watchedListener{Listener: listener}
    fmt.Println("listen at=finish")

    go func() {
        <-stop
        fmt.Println("close at=start")
        closeErr := wListener.Close()
        if closeErr != nil {
            panic(closeErr)
        }
        fmt.Println("close at=finish")
    }()

    go func() {
        signal.Notify(
            sig, syscall.SIGINT,
            syscall.SIGTERM)
        fmt.Println("trap at=start")
        <-sig
        stop <- true
        fmt.Println("trap at=finish")
    }()

    fmt.Println("serve at=start")
    server.Serve(wListener)
    fmt.Println("serve at=finish")
    for {
        connCountCurrent := atomic.LoadInt64(&connCount)
        if connCountCurrent > 0 {
            fmt.Println("wait at=pending remaining=",
                connCountCurrent)
            time.Sleep(time.Second)
        } else {
            fmt.Println("wait at=finish remaining=",
                connCountCurrent)
            return
        }
    }
}
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
贪吃蛇
文件下载
双位运算符
发送邮件
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
提取后缀名
年的日历图
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例