Skip to content

Commit

Permalink
chore(router): use automic's CAS to allow to shutdown the router conc…
Browse files Browse the repository at this point in the history
…urrently (#1343)

Co-authored-by: Dustin Deus <[email protected]>
  • Loading branch information
git-hulk and StarpTech authored Nov 12, 2024
1 parent 5c3f61e commit e1a4baa
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions router/core/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ type (
staticExecutionConfig *nodev1.RouterConfig
awsLambda bool
shutdown atomic.Bool
bootstrapped bool
bootstrapped atomic.Bool
ipAnonymization *IPAnonymizationConfig
listenAddr string
baseURL string
Expand Down Expand Up @@ -652,14 +652,12 @@ func (r *Router) NewServer(ctx context.Context) (Server, error) {
}

// bootstrap initializes the Router. It is called by Start() and NewServer().
// It should only be called once for a Router instance. Not safe for concurrent use.
// It should only be called once for a Router instance.
func (r *Router) bootstrap(ctx context.Context) error {
if r.bootstrapped {
if !r.bootstrapped.CompareAndSwap(false, true) {
return fmt.Errorf("router is already bootstrapped")
}

r.bootstrapped = true

cosmoCloudTracingEnabled := r.traceConfig.Enabled && rtrace.DefaultExporter(r.traceConfig) != nil
artInProductionEnabled := r.engineExecutionConfiguration.EnableRequestTracing && !r.developmentMode
needsRegistration := cosmoCloudTracingEnabled || artInProductionEnabled
Expand Down Expand Up @@ -1094,15 +1092,13 @@ func (r *Router) Start(ctx context.Context) error {
}

// Shutdown gracefully shuts down the router. It blocks until the server is shutdown.
// If the router is already shutdown, the method returns immediately without error. Not safe for concurrent use.
// If the router is already shutdown, the method returns immediately without error.
func (r *Router) Shutdown(ctx context.Context) (err error) {

if r.shutdown.Load() {
if !r.shutdown.CompareAndSwap(false, true) {
return nil
}

r.shutdown.Store(true)

// Respect grace period
if r.routerGracePeriod > 0 {
ctxWithTimer, cancel := context.WithTimeout(ctx, r.routerGracePeriod)
Expand Down

0 comments on commit e1a4baa

Please sign in to comment.