Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upg.Exit() do not effective in go routine ? #71

Open
xiaobinqt opened this issue Aug 3, 2022 · 1 comment
Open

upg.Exit() do not effective in go routine ? #71

xiaobinqt opened this issue Aug 3, 2022 · 1 comment

Comments

@xiaobinqt
Copy link

	go func(upg *tableflip.Upgrader) {
		for {
			select {
			case <-upg.Exit():
				fmt.Println("Exit111111111111111111111111111")
				break
			}
		}
	}(upg)

in this case , upg.Exit() not triggered ?

complete example

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/cloudflare/tableflip"
)

// 當前程序的版本
const version = "v0.0.1"

func main() {
	upg, err := tableflip.New(tableflip.Options{})
	if err != nil {
		panic(err)
	}
	defer upg.Stop()

	// 爲了演示方便,爲程序啓動強行加入 1s 的延時,並在日誌中附上進程 pid
	time.Sleep(time.Second)
	log.SetPrefix(fmt.Sprintf("[PID: %d] ", os.Getpid()))

	// 監聽系統的 SIGHUP 信號,以此信號觸發進程重啓
	go func() {
		sig := make(chan os.Signal, 1)
		signal.Notify(sig, syscall.SIGHUP)
		for range sig {
			// 核心的 Upgrade 調用
			err := upg.Upgrade()
			if err != nil {
				log.Println("Upgrade failed:", err)
			}
		}
	}()

	// 注意必須使用 upg.Listen 對端口進行監聽
	ln, err := upg.Listen("tcp", ":8080")
	if err != nil {
		log.Fatalln("Can't listen:", err)
	}

	// 創建一個簡單的 http server,/version 返回當前的程序版本
	mux := http.NewServeMux()
	mux.HandleFunc("/version", func(rw http.ResponseWriter, r *http.Request) {
		log.Println(version)
		rw.Write([]byte(version + "\n"))
	})
	server := http.Server{
		Handler: mux,
	}

	// 照常啓動 http server
	go func() {
		err := server.Serve(ln)
		if err != http.ErrServerClosed {
			log.Println("HTTP server:", err)
		}
	}()

	if err := upg.Ready(); err != nil {
		panic(err)
	}

	go func(upg *tableflip.Upgrader) {
		for {
			select {
			case <-upg.Exit():
				fmt.Println("Exit111111111111111111111111111")
				break
			}
		}
	}(upg)

	time.Sleep(10 * time.Hour)

	//<-upg.Exit()

}

@ljluestc
Copy link

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/cloudflare/tableflip"
)

const version = "v0.0.1"

func main() {
	upg, err := tableflip.New(tableflip.Options{})
	if err != nil {
		panic(err)
	}
	defer upg.Stop()

	time.Sleep(time.Second)
	log.SetPrefix(fmt.Sprintf("[PID: %d] ", os.Getpid()))

	go func() {
		sig := make(chan os.Signal, 1)
		signal.Notify(sig, syscall.SIGHUP)
		for range sig {
			err := upg.Upgrade()
			if err != nil {
				log.Println("Upgrade failed:", err)
			}
		}
	}()

	ln, err := upg.Listen("tcp", ":8080")
	if err != nil {
		log.Fatalln("Can't listen:", err)
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/version", func(rw http.ResponseWriter, r *http.Request) {
		log.Println(version)
		rw.Write([]byte(version + "\n"))
	})
	server := http.Server{
		Handler: mux,
	}

	go func() {
		err := server.Serve(ln)
		if err != http.ErrServerClosed {
			log.Println("HTTP server:", err)
		}
	}()

	if err := upg.Ready(); err != nil {
		panic(err)
	}

	go func() {
		<-upg.Exit() // Wait for the exit signal
		fmt.Println("Exit111111111111111111111111111")
	}()

	select {
	case <-upg.Exit():
		// Handle any cleanup or additional exit logic here
	}

	time.Sleep(10 * time.Hour)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants