gpt4 book ai didi

go - falcore 热重启不会重新加载主代码

转载 作者:IT王子 更新时间:2023-10-29 02:17:37 25 4
gpt4 key购买 nike

我正在试验 falcore(go(lang) 框架),他们有一个很好的示例,允许您向进程发送 SIGHUP,之后它会重新启动客户端,移动连接并退出父进程。

因此,在我的示例中,我有一个 server.go(在最后发布),我默认在其中提供一个文件。

我运行服务器,然后编辑 .go 文件,kill -1 进程的 pid,应用程序按预期重启,但是没有加载新添加到 .go 文件的代码。

例如,我将提供的默认文件从 summary.xml 更改为 AppNexus-Interesting.txt,但它将继续为所有新请求提供 summary.xml 文件。

感谢任何帮助。

package main

import (
"flag"
"fmt"
"github.com/fitstar/falcore"
"github.com/fitstar/falcore/filter"
"net/http"
"os"
"os/signal"
"syscall"
)

// Command line options
var (
port = flag.Int("port", 8000, "the port to listen on")
path = flag.String("base", "./www", "the path to serve files from")
)

// very simple request filter
func Filter(req *falcore.Request) *http.Response {
pid := syscall.Getpid()
fmt.Println(pid, "GET", req.HttpRequest.URL.Path)

// return falcore.StringResponse(request.HttpRequest, 200, nil, "OK\n")
if req.HttpRequest.URL.Path == "/" {
req.HttpRequest.URL.Path = "AppNexus-Interesting.txt" //"/summary.xml"
}
return nil
}

// flag to accept a socket file descriptor
var socketFd = flag.Int("socket", -1, "Socket file descriptor")

func main() {
pid := syscall.Getpid()
flag.Parse()
fmt.Println("Falcore hot restart running with pid:", pid, "to hot restart, issue the kill -1", pid, "command")

// create the pipeline
pipeline := falcore.NewPipeline()

// upstream filters
pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))

// Serve files
pipeline.Upstream.PushBack(&filter.FileFilter{
BasePath: *path,
})

// downstream filters
pipeline.Downstream.PushBack(filter.NewCompressionFilter(nil))

// create the server with the pipeline
srv := falcore.NewServer(*port, pipeline)

// if passed the socket file descriptor, setup the listener that way
// if you don't have it, the default is to create the socket listener
// with the data passed to falcore.NewServer above (happens in ListenAndServer())
if *socketFd != -1 {
// I know I'm a child process if I get here so I can signal the parent when I'm ready to take over
go childReady(srv)
fmt.Printf("%v Got socket FD: %v\n", pid, *socketFd)
srv.FdListen(*socketFd)
}

// using signals to manage the restart lifecycle
go handleSignals(srv)

// start the server
// this is normally blocking forever unless you send lifecycle commands
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("%v Could not start server: %v", pid, err)
}
fmt.Printf("%v Exiting now\n", pid)
}

// blocks on the server ready and when ready, it sends
// a signal to the parent so that it knows it cna now exit
func childReady(srv *falcore.Server) {
pid := syscall.Getpid()
// wait for the ready signal
<-srv.AcceptReady
// grab the parent and send a signal that the child is ready
parent := syscall.Getppid()
fmt.Printf("%v Kill parent %v with SIGUSR1\n", pid, parent)
syscall.Kill(parent, syscall.SIGUSR1)
}

// setup and fork/exec myself. Make sure to keep open important FD's that won't get re-created by the child
// specifically, std* and your listen socket
func forker(srv *falcore.Server) (pid int, err error) {
fmt.Printf("Forking now with socket: %v\n", srv.SocketFd())
mypath := os.Args[0]
args := []string{mypath, "-socket", fmt.Sprintf("%v", srv.SocketFd())}
attr := new(syscall.ProcAttr)
attr.Files = append([]uintptr(nil), 0, 1, 2, uintptr(srv.SocketFd()))
pid, err = syscall.ForkExec(mypath, args, attr)
return
}

// Handle lifecycle events
func handleSignals(srv *falcore.Server) {
var sig os.Signal
var sigChan = make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM, syscall.SIGTSTP)
pid := syscall.Getpid()
for {
sig = <-sigChan
switch sig {
case syscall.SIGHUP:
// send this to the paraent process to initiate the restart
fmt.Println(pid, "Received SIGHUP. forking.")
cpid, err := forker(srv)
fmt.Println(pid, "Forked pid:", cpid, "errno:", err)
case syscall.SIGUSR1:
// child sends this back to the parent when it's ready to Accept
fmt.Println(pid, "Received SIGUSR1. Stopping accept.")
srv.StopAccepting()
case syscall.SIGINT:
fmt.Println(pid, "Received SIGINT. Shutting down.")
os.Exit(0)
case syscall.SIGTERM:
fmt.Println(pid, "Received SIGTERM. Terminating.")
os.Exit(0)
case syscall.SIGTSTP:
fmt.Println(pid, "Received SIGTSTP. Stopping.")
syscall.Kill(pid, syscall.SIGSTOP)
default:
fmt.Println(pid, "Received", sig, ": ignoring")
}
}
}

最佳答案

由于 go 不是脚本语言,您必须先将源代码编译成二进制文件(使用 go buildgo install),然后执行重启。

关于go - falcore 热重启不会重新加载主代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com