Skip to content

Commit

Permalink
Fix Go runtime shutdown function ctx (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito authored Jun 18, 2024
1 parent d1b8c1f commit d8cc82f
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]
### Fixed
- Correctly wire Go runtime shutdown function context.

## [3.22.0] - 2024-06-09
### Added
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/heroiclabs/nakama-common v1.32.0
github.com/heroiclabs/nakama-common v1.32.1-0.20240617141901-09eb4ffdf9bf
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/heroiclabs/nakama-common v1.32.0 h1:aCWyYf9mQzifeVu3bXBiRRL9Z/dGBgwY/rgUWoYCnQM=
github.com/heroiclabs/nakama-common v1.32.0/go.mod h1:lPG64MVCs0/tEkh311Cd6oHX9NLx2vAPx7WW7QCJHQ0=
github.com/heroiclabs/nakama-common v1.32.1-0.20240617141901-09eb4ffdf9bf h1:PpB1MCfShscfwNG04LnloAnobajN/zlKeeQNaYJe7+I=
github.com/heroiclabs/nakama-common v1.32.1-0.20240617141901-09eb4ffdf9bf/go.mod h1:lPG64MVCs0/tEkh311Cd6oHX9NLx2vAPx7WW7QCJHQ0=
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a h1:tuL2ZPaeCbNw8rXmV9ywd00nXRv95V4/FmbIGKLQJAE=
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a/go.mod h1:hzCTPoEi/oml2BllVydJcNP63S7b56e5DzeQeLGvw1U=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
2 changes: 1 addition & 1 deletion server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
}
metrics = NewLocalMetrics(logger, logger, nil, cfg)
storageIdx, _ = NewLocalStorageIndex(logger, nil, &StorageConfig{DisableIndexOnly: false}, metrics)
_ = cfg.Validate(logger)
_ = ValidateConfig(logger, cfg)
)

type DummyMessageRouter struct{}
Expand Down
1 change: 1 addition & 0 deletions server/runtime_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,7 @@ func (ri *RuntimeGoInitializer) RegisterFleetManager(fleetManager runtime.FleetM

func (ri *RuntimeGoInitializer) RegisterShutdown(fn func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule)) error {
ri.shutdownFunction = func(ctx context.Context) {
ctx = NewRuntimeGoContext(ctx, ri.node, ri.version, ri.env, RuntimeExecutionModeShutdown, nil, nil, 0, "", "", nil, "", "", "", "")
fn(ctx, ri.logger.WithField("mode", RuntimeExecutionModeShutdown.String()), ri.db, ri.nk)
}

Expand Down
11 changes: 7 additions & 4 deletions server/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"github.com/heroiclabs/nakama-common/runtime"
"go.uber.org/zap"
"os"
"time"
Expand All @@ -14,18 +15,20 @@ func HandleShutdown(ctx context.Context, logger *zap.Logger, matchRegistry Match
runtimeShutdownFnDone := make(chan struct{}, 1)

if graceSeconds != 0 {
timer = time.NewTimer(time.Duration(graceSeconds) * time.Second)
timerCh = timer.C

graceDuration := time.Duration(graceSeconds) * time.Second
if shutdownFn != nil {
go func() {
shutdownFn(ctx)
shCtx, _ := context.WithTimeoutCause(context.WithoutCancel(ctx), graceDuration, runtime.ErrGracePeriodExpired)
shutdownFn(shCtx)
close(runtimeShutdownFnDone)
}()
} else {
close(runtimeShutdownFnDone)
}

timer = time.NewTimer(graceDuration)
timerCh = timer.C

logger.Info("Shutdown started - use CTRL^C to force stop server", zap.Int("grace_period_sec", graceSeconds))
} else {
// No grace period.
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/heroiclabs/nakama-common/runtime/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopena
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options
github.com/grpc-ecosystem/grpc-gateway/v2/runtime
github.com/grpc-ecosystem/grpc-gateway/v2/utilities
# github.com/heroiclabs/nakama-common v1.32.0
# github.com/heroiclabs/nakama-common v1.32.1-0.20240617141901-09eb4ffdf9bf
## explicit; go 1.19
github.com/heroiclabs/nakama-common/api
github.com/heroiclabs/nakama-common/rtapi
Expand Down

0 comments on commit d8cc82f

Please sign in to comment.