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

feat: add ProcConf to make SetTimeToForceQuit configurable #4446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions core/proc/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import (
"github.com/zeromicro/go-zero/core/threading"
)

const (
wrapUpTime = time.Second
// why we use 5500 milliseconds is because most of our queue are blocking mode with 5 seconds
waitTime = 5500 * time.Millisecond
)
type ProcConf struct {
WrapUpTime time.Duration `json:",default=1s"`
WaitTime time.Duration `json:",default=5.5s"`
}

var (
wrapUpListeners = new(listenerManager)
shutdownListeners = new(listenerManager)
delayTimeBeforeForceQuit = waitTime
wrapUpListeners = new(listenerManager)
shutdownListeners = new(listenerManager)
wrapUpTime = time.Second
// why we use 5500 milliseconds is because most of our queue are blocking mode with 5 seconds
delayTimeBeforeForceQuit = 5500 * time.Millisecond
)

// AddShutdownListener adds fn as a shutdown listener.
Expand All @@ -42,6 +43,11 @@ func SetTimeToForceQuit(duration time.Duration) {
delayTimeBeforeForceQuit = duration
}

func Setup(conf ProcConf) {
wrapUpTime = conf.WrapUpTime
delayTimeBeforeForceQuit = conf.WaitTime
}

// Shutdown calls the registered shutdown listeners, only for test purpose.
func Shutdown() {
shutdownListeners.notifyListeners()
Expand Down
9 changes: 9 additions & 0 deletions core/proc/shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ func TestNotifyMoreThanOnce(t *testing.T) {
t.Fatal("timeout, check error logs")
}
}

func TestSetup(t *testing.T) {
Setup(ProcConf{
WrapUpTime: time.Second * 2,
WaitTime: time.Second * 30,
})
assert.Equal(t, time.Second*2, wrapUpTime)
assert.Equal(t, time.Second*30, delayTimeBeforeForceQuit)
}
2 changes: 2 additions & 0 deletions core/service/serviceconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Prometheus prometheus.Config `json:",optional"`
Telemetry trace.Config `json:",optional"`
DevServer DevServerConfig `json:",optional"`
Proc proc.ProcConf `json:",optional"`

Check failure on line 40 in core/service/serviceconf.go

View workflow job for this annotation

GitHub Actions / Windows

undefined: proc.ProcConf
}
)

Expand All @@ -61,6 +62,7 @@
sc.Telemetry.Name = sc.Name
}
trace.StartAgent(sc.Telemetry)
proc.Setup(sc.Proc)

Check failure on line 65 in core/service/serviceconf.go

View workflow job for this annotation

GitHub Actions / Windows

undefined: proc.Setup
proc.AddShutdownListener(func() {
trace.StopAgent()
})
Expand Down