feat(grace): 测试多进程监听同一个 fd

This commit is contained in:
liuyx 2024-04-18 14:09:13 +08:00
parent 75cf193824
commit 39255faa9a
9 changed files with 172 additions and 7 deletions

25
.gitignore vendored Normal file
View File

@ -0,0 +1,25 @@
### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
.idea
_*

18
go.mod Normal file
View File

@ -0,0 +1,18 @@
module grace
go 1.22.1
require (
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 // indirect
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
)

18
go.sum Normal file
View File

@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9 h1:wWke/RUCl7VRjQhwPlR/v0glZXNYzBHdNUzf/Am2Nmg=
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9/go.mod h1:uPmAp6Sws4L7+Q/OokbWDAK1ibXYhB3PXFP1kol5hPg=
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 h1:mOp33BLbcbJ8fvTAmZacbBiOASfxN+MLcLxymZCIrGE=
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434/go.mod h1:KigFdumBXUPSwzLDbeuzyt0elrL7+CP7TKuhrhT4bcU=
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 h1:nXeeRHmgNgjLxi+7dY9l9aDvSS1uwVlNLqUWIY4Ath0=
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2/go.mod h1:TUV/fX3XrTtBQb5+ttSUJzcFgLNpILONFTKmBuk5RSw=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 h1:0YtRCqIZs2+Tz49QuH6cJVw/IFqzo39gEqZ0iYLxD2M=
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4/go.mod h1:vsJz7uE339KUCpBXx3JAJzSRH7Uk4iGGyJzR529qDIA=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=

View File

@ -5,11 +5,14 @@ package main
import (
"flag"
"fmt"
"grace/gracedemo/manifest"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/facebookgo/grace/gracehttp"
"grace/gracehttp"
)
var (
@ -21,16 +24,47 @@ var (
func main() {
flag.Parse()
gracehttp.Serve(
gracehttp.SetLogger(log.Default())
_ = os.WriteFile("./_pid", []byte(strconv.Itoa(os.Getpid())), os.ModePerm)
log.Println("server start:", os.Getppid(), os.Getpid(), getManifest())
err := gracehttp.Serve(
&http.Server{Addr: *address0, Handler: newHandler("Zero ")},
&http.Server{Addr: *address1, Handler: newHandler("First ")},
&http.Server{Addr: *address2, Handler: newHandler("Second")},
//&http.Server{Addr: *address1, Handler: newHandler("First ")},
//&http.Server{Addr: *address2, Handler: newHandler("Second")},
)
if err != nil {
log.Println(err)
}
log.Println("server shutdown:", os.Getppid(), os.Getpid(), getManifest())
}
func getManifest() string {
return fmt.Sprintf("Version: %s Branch: %s Commit: %s Static:%s", manifest.Version, manifest.Branch, manifest.Commit, manifest.Static)
}
func newHandler(name string) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/sleep/", func(w http.ResponseWriter, r *http.Request) {
duration, err := time.ParseDuration(r.FormValue("duration"))
//log.Println(r.URL)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
time.Sleep(duration)
fmt.Fprintf(
w,
"+ %s started at %s slept for %d nanoseconds from pid %d. %s\n",
name,
now,
duration.Nanoseconds(),
os.Getpid(),
getManifest(),
)
})
//syscall.SetsockoptInt()
mux.HandleFunc("/restart", func(w http.ResponseWriter, r *http.Request) {
duration, err := time.ParseDuration(r.FormValue("duration"))
if err != nil {
http.Error(w, err.Error(), 400)
@ -39,12 +73,13 @@ func newHandler(name string) http.Handler {
time.Sleep(duration)
fmt.Fprintf(
w,
"%s started at %s slept for %d nanoseconds from pid %d.\n",
"+ %s started at %s slept for %d nanoseconds from pid %d.\n",
name,
now,
duration.Nanoseconds(),
os.Getpid(),
)
})
return mux
}

28
gracedemo/dev.sh Executable file
View File

@ -0,0 +1,28 @@
set -xe
PROG=t.out
if which git >/dev/null 2>&1; then
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
# 获取最新的 commit ID
commit=$(git rev-parse --short=8 HEAD)
# 获取当前分支名
branch=$(git rev-parse --abbrev-ref HEAD)
version=$(git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
static=`date +%Y%m%d_%H:%M:%S%z`
echo "build git repo info version: $version branch: $branch commit: $commit static: $static"
fi
fi
go build \
-ldflags "-X grace/gracedemo/manifest.Version=$version -X grace/gracedemo/manifest.Commit=$commit -X grace/gracedemo/manifest.Branch=$branch -X grace/gracedemo/manifest.Static=$static" \
-o ${PROG}
#nohup ./${PROG} app.json 2>&1 &
./${PROG} > nohup.out

27
gracedemo/hot.sh Executable file
View File

@ -0,0 +1,27 @@
set -xe
PROG=t.out
if which git >/dev/null 2>&1; then
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
# 获取最新的 commit ID
commit=$(git rev-parse --short=8 HEAD)
# 获取当前分支名
branch=$(git rev-parse --abbrev-ref HEAD)
version=$(git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
static=`date +%Y%m%d_%H:%M:%S%z`
echo "build git repo info version: $version branch: $branch commit: $commit static: $static"
fi
fi
go build \
-ldflags "-X grace/gracedemo/manifest.Version=$version -X grace/gracedemo/manifest.Commit=$commit -X grace/gracedemo/manifest.Branch=$branch -X grace/gracedemo/manifest.Static=$static" \
-o ${PROG}
kill -HUG $(cat ./_pid)

View File

@ -0,0 +1,14 @@
package manifest
import "fmt"
var (
Branch = ""
Commit = ""
Version = "development"
Static = ""
)
func init() {
fmt.Printf("[AppInfo][Init] Version: %s Branch: %s Commit: %s Static:%s\n", Version, Branch, Commit, Static)
}

View File

@ -13,7 +13,7 @@ import (
"testing"
"time"
"github.com/facebookgo/grace/gracehttp"
"grace/gracehttp"
)
const preStartProcessEnv = "GRACEHTTP_PRE_START_PROCESS"