热更新

endless

基本使用

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/fvbock/endless"
)

func main() {
    tStart := time.Now()

    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "系统启动时间: %s\n", tStart.Format(time.DateTime))
    })

    panic(endless.NewServer(":8080", mux).ListenAndServe())
}

编译后 使用 kill -1 pid 即可实现热更新

ps aux | grep "<执行文件名称>" | grep -v grep | awk '{print $2}' | xargs -i kill -1

如何保证其他协程也能优雅退出

  • 使用 context.WithCancel 来实现其他协程优雅的退出
package main

import (
    "context"
    "fmt"
    "net/http"
    "sync"
    "time"

    "github.com/fvbock/endless"
)

func main() {
    tStart := time.Now()

    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    var wg sync.WaitGroup

    wg.Add(1)
    go func() {

        t := time.NewTicker(time.Second)

        for {
            select {
            case <-t.C:
                fmt.Printf("timer中 当前启动时间: %s 当前时间:%s\n", tStart.Format(time.DateTime), time.Now().Format(time.DateTime))
            case <-ctx.Done():
                fmt.Println("timer cancel")
                wg.Done()
                return
            }
        }

    }()

    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "系统启动时间: %s\n", tStart.Format(time.DateTime))
    })

    server := endless.NewServer(":8080", mux)

    fmt.Println(server.ListenAndServe())
    cancel()
    wg.Wait()
}

results matching ""

    No results matching ""